123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2018/11/21
- * Time: 下午3:30
- */
- use app\api\library\ReplayTemplate;
- use app\common\library\Redis;
- use app\main\constants\BookConstants;
- use app\main\constants\CacheConstants;
- use app\main\constants\ErrorCodeConstants;
- use app\main\constants\MarkConstants;
- use app\main\model\object\ReturnObject;
- use app\main\service\AdminService;
- use app\main\service\BookService;
- use app\main\service\UrlService;
- use app\main\service\UserService;
- class BookTest extends TestInit
- {
- //测试获取章节列表
- public function testGetChapterList()
- {
- $data = model('Book')::getChapterInfo('11000000108', '120440');
- $data = model('Book')->BookInfo('11000000108');
- var_dump($data);
- return;
- // $data = BookService::instance()->getGuidChapterIdx('11000000051', '1067')->data;
- // var_dump($data);
- $user_id = 60063;
- $soruceReadLog = BookService::instance()->getUserRecentlyRead()->setConnect($user_id)->getRecentlyRead(0,
- 3, $user_id, true);
- if ($soruceReadLog['totalNum'] > 0) {
- $book_info = current($soruceReadLog['data']);
- $cacheKey = CacheConstants::getBookSubCache($user_id);
- //如果点下一章关注,阅读记录加一章
- if (Redis::instance()->get($cacheKey) == $book_info['book_id']) {
- $chapterInfo = BookService::instance()->getChapterInfo($book_info['book_id'], $book_info['chapter_id'] + 1)->data;
- BookService::instance()->setRecentlyRead($chapterInfo['name'], $chapterInfo['id'], $book_info['book_id'], $chapterInfo['idx'], $user_id);
- $book_info['chapter_name'] = $chapterInfo['name'];
- }
- $title = "欢迎关注,你上次看到了\r\n 《%s》 %s";
- $title = sprintf($title, $book_info['book_name'], $book_info['chapter_name']);
- $readlog = current($soruceReadLog['data']);
- }
- }
- //测试导粉章节数
- public function testGetGuidChapterIdx()
- {
- //推广链接
- $referral = UrlService::instance()->getReferralModel()->find();
- $referralReturn = BookService::instance()->getGuidChapterIdx(0, $referral->id);
- $this->assertTrue($referral['guide_chapter_idx'] == $referralReturn->data);
- //全局配置
- $noAdminIdReturn = BookService::instance()->getGuidChapterIdx();
- $this->assertTrue(\think\Config::get('site.book_guide_chapter_idx') == $noAdminIdReturn->data);
- //渠道商
- $adminId = 3;
- $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($adminId);
- $book_id = '11000000108';
- UserService::instance()->getRunTimeObject()->adminConfig = $adminConfig;
- UserService::instance()->getRunTimeObject()->adminId = $adminId;
- $channelReturn = BookService::instance()->getGuidChapterIdx($book_id);
- $db_data = BookService::instance()->getGuideModel()
- ->field('book_id,chapter_idx')
- ->where('book_id',$book_id)
- ->where('admin_id',$adminId)
- ->find();
- $this->assertEquals($channelReturn->data, $db_data['chapter_idx']);
- }
- //测试导粉二维码逻辑
- public function testGetGuidQrCode()
- {
- //渠道商
- $adminId = 3;
- $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($adminId);
- UserService::instance()->getRunTimeObject()->adminConfig = $adminConfig;
- $data = AdminService::instance()->getGuidQrCode();
- $this->assertInstanceOf(\app\main\model\object\ReturnObject::class, $data);
- }
- //测试用户编好
- public function testSetUserPreference()
- {
- $bookId = '11000000027';
- $book = BookService::instance()->getBookModel()->BookInfo($bookId);
- $category_id = $book['book_category_id'];
- $_COOKIE['user_id'] = 19335785;
- UserService::instance()->getRunTimeObject()->urlType = \app\main\constants\UrlConstants::REFERRAL_GROUND_PAGE_SPREAD;
- $keyUBC = 'U-BC:' . UserService::instance()->getUserInfo()->id;
- $cache = \app\common\library\Redis::instance()->get($keyUBC);
- $expect = explode(',', $cache);
- $expect[] = $category_id;
- $expect = array_unique($expect);
- sort($expect);
- UserService::instance()->addUserPreferences($bookId);
- $keyUBC = 'U-BC:' . UserService::instance()->getUserInfo()->id;
- $cache = \app\common\library\Redis::instance()->get($keyUBC);
- $this->assertEquals($cache, implode(',', $expect));
- }
- //测试获取书籍章节信息
- public function testGetBookChapterInfo()
- {
- $_COOKIE['user_id'] = 2;
- $path = BookConstants::getBookChapterDir();
- UserService::instance()->getRunTimeObject()->urlType = \app\main\constants\UrlConstants::REFERRAL_GROUND_PAGE_SPREAD;
- //参数错误
- $data = BookService::instance()->getBookChapterInfo('', '', '');
- $this->assertEquals('/', $data->data);
- $this->assertEquals(ErrorCodeConstants::REDIRECT, $data->code);
- //书籍不存在
- $data = BookService::instance()->getBookChapterInfo('-1', '', '');
- $this->assertEquals('public/nobook', $data->data['tpl']);
- $this->assertEquals(ErrorCodeConstants::REDIRECT_VIEW, $data->code);
- //正常书籍
- $book_list = BookService::instance()->getBookModel()->where('state', BookConstants::BOOK_STATE_ON_SALE)->limit(10)->select();
- foreach ($book_list as $book) {
- $book_id = $book['id'];
- $chapter_list = BookService::instance()->getChapterList($book_id)->data;
- if ($chapter_list['data']) {
- $chapter_id = $chapter_list['data'][0]['id'];
- $file = BookService::instance()->getChapterContentPath($path, $book_id, $chapter_id);
- $data = BookService::instance()->getBookChapterInfo($book_id, $chapter_list['data'][0]['id'], '');
- $this->assertInstanceOf(ReturnObject::class, $data);
- if (file_exists($file)) {
- $this->assertEquals(array_keys($data->data), ['chapter', 'book', 'pre_link', 'next_link']);
- } else {
- if(array_key_exists('next_link', $data->data['bind'])){
- //书籍内容丢失
- $this->assertEquals(ErrorCodeConstants::REDIRECT_VIEW, $data->code);
- $this->assertEquals('public/nocontent', $data->data['tpl']);
- echo "书籍章节丢失,无内容,有下章: $file\n";
- }else{
- //书籍内容丢失
- $this->assertEquals(ErrorCodeConstants::REDIRECT_VIEW, $data->code);
- $this->assertEquals('public/nobook', $data->data['tpl']);
- echo "书籍章节丢失,无内容,无下章: $file\n";
- }
- }
- }
- }
- //下架测试
- $book_off_sale = BookService::instance()->getBookModel()->where('state', BookConstants::BOOK_STATE_OFF_SALE)->find();
- $book_id = $book_off_sale['id'];
- $chapter_list = BookService::instance()->getChapterList($book_id)->data;
- if ($chapter_list['data']) {
- $data = BookService::instance()->getBookChapterInfo($book_id, $chapter_list['data'][0]['id'], '');
- $this->assertInstanceOf(ReturnObject::class, $data);
- $this->assertEquals(ErrorCodeConstants::REDIRECT_VIEW, $data->code);
- $this->assertEquals('public/forbiddenread', $data->data['tpl']);
- }
- }
- public function testBook()
- {
- for ($i = 0; $i < 20; $i++) {
- BookService::instance()->getBookModel()->getChapterInfo('11000000108', '120439');
- }
- $data = BookService::instance()->getChapterInfo('11000000108', '120439');
- var_dump($data);
- }
- public function testEditedChapter()
- {
- $cacheKey = \app\main\constants\CacheConstants::BOOK_EDITED_CHAPTER.'103264';
- $data = BookService::instance()->getChapterEditedModel()->getChapterFromDb($cacheKey);
- var_dump($data);
- }
- public function testGetChapter()
- {
- $list = BookService::instance()->getBookModel()
- ->where('cast(word_count as UNSIGNED) between 1 and 1000000')
- ->column('word_count');
- var_dump($list);
- }
- public function testReturnRecommand()
- {
- echo \think\Env::get('app.return_filter_recent_num', 100);
- // $book = BookService::instance()->getReturnRecommandBook();
- // var_dump($book);
- }
- }
|