BookTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2018/11/21
  6. * Time: 下午3:30
  7. */
  8. use app\api\library\ReplayTemplate;
  9. use app\common\library\Redis;
  10. use app\main\constants\BookConstants;
  11. use app\main\constants\CacheConstants;
  12. use app\main\constants\ErrorCodeConstants;
  13. use app\main\constants\MarkConstants;
  14. use app\main\model\object\ReturnObject;
  15. use app\main\service\AdminService;
  16. use app\main\service\BookService;
  17. use app\main\service\UrlService;
  18. use app\main\service\UserService;
  19. class BookTest extends TestInit
  20. {
  21. //测试获取章节列表
  22. public function testGetChapterList()
  23. {
  24. $data = model('Book')::getChapterInfo('11000000108', '120440');
  25. $data = model('Book')->BookInfo('11000000108');
  26. var_dump($data);
  27. return;
  28. // $data = BookService::instance()->getGuidChapterIdx('11000000051', '1067')->data;
  29. // var_dump($data);
  30. $user_id = 60063;
  31. $soruceReadLog = BookService::instance()->getUserRecentlyRead()->setConnect($user_id)->getRecentlyRead(0,
  32. 3, $user_id, true);
  33. if ($soruceReadLog['totalNum'] > 0) {
  34. $book_info = current($soruceReadLog['data']);
  35. $cacheKey = CacheConstants::getBookSubCache($user_id);
  36. //如果点下一章关注,阅读记录加一章
  37. if (Redis::instance()->get($cacheKey) == $book_info['book_id']) {
  38. $chapterInfo = BookService::instance()->getChapterInfo($book_info['book_id'], $book_info['chapter_id'] + 1)->data;
  39. BookService::instance()->setRecentlyRead($chapterInfo['name'], $chapterInfo['id'], $book_info['book_id'], $chapterInfo['idx'], $user_id);
  40. $book_info['chapter_name'] = $chapterInfo['name'];
  41. }
  42. $title = "欢迎关注,你上次看到了\r\n 《%s》 %s";
  43. $title = sprintf($title, $book_info['book_name'], $book_info['chapter_name']);
  44. $readlog = current($soruceReadLog['data']);
  45. }
  46. }
  47. //测试导粉章节数
  48. public function testGetGuidChapterIdx()
  49. {
  50. //推广链接
  51. $referral = UrlService::instance()->getReferralModel()->find();
  52. $referralReturn = BookService::instance()->getGuidChapterIdx(0, $referral->id);
  53. $this->assertTrue($referral['guide_chapter_idx'] == $referralReturn->data);
  54. //全局配置
  55. $noAdminIdReturn = BookService::instance()->getGuidChapterIdx();
  56. $this->assertTrue(\think\Config::get('site.book_guide_chapter_idx') == $noAdminIdReturn->data);
  57. //渠道商
  58. $adminId = 3;
  59. $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($adminId);
  60. $book_id = '11000000108';
  61. UserService::instance()->getRunTimeObject()->adminConfig = $adminConfig;
  62. UserService::instance()->getRunTimeObject()->adminId = $adminId;
  63. $channelReturn = BookService::instance()->getGuidChapterIdx($book_id);
  64. $db_data = BookService::instance()->getGuideModel()
  65. ->field('book_id,chapter_idx')
  66. ->where('book_id',$book_id)
  67. ->where('admin_id',$adminId)
  68. ->find();
  69. $this->assertEquals($channelReturn->data, $db_data['chapter_idx']);
  70. }
  71. //测试导粉二维码逻辑
  72. public function testGetGuidQrCode()
  73. {
  74. //渠道商
  75. $adminId = 3;
  76. $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($adminId);
  77. UserService::instance()->getRunTimeObject()->adminConfig = $adminConfig;
  78. $data = AdminService::instance()->getGuidQrCode();
  79. $this->assertInstanceOf(\app\main\model\object\ReturnObject::class, $data);
  80. }
  81. //测试用户编好
  82. public function testSetUserPreference()
  83. {
  84. $bookId = '11000000027';
  85. $book = BookService::instance()->getBookModel()->BookInfo($bookId);
  86. $category_id = $book['book_category_id'];
  87. $_COOKIE['user_id'] = 19335785;
  88. UserService::instance()->getRunTimeObject()->urlType = \app\main\constants\UrlConstants::REFERRAL_GROUND_PAGE_SPREAD;
  89. $keyUBC = 'U-BC:' . UserService::instance()->getUserInfo()->id;
  90. $cache = \app\common\library\Redis::instance()->get($keyUBC);
  91. $expect = explode(',', $cache);
  92. $expect[] = $category_id;
  93. $expect = array_unique($expect);
  94. sort($expect);
  95. UserService::instance()->addUserPreferences($bookId);
  96. $keyUBC = 'U-BC:' . UserService::instance()->getUserInfo()->id;
  97. $cache = \app\common\library\Redis::instance()->get($keyUBC);
  98. $this->assertEquals($cache, implode(',', $expect));
  99. }
  100. //测试获取书籍章节信息
  101. public function testGetBookChapterInfo()
  102. {
  103. $_COOKIE['user_id'] = 2;
  104. $path = BookConstants::getBookChapterDir();
  105. UserService::instance()->getRunTimeObject()->urlType = \app\main\constants\UrlConstants::REFERRAL_GROUND_PAGE_SPREAD;
  106. //参数错误
  107. $data = BookService::instance()->getBookChapterInfo('', '', '');
  108. $this->assertEquals('/', $data->data);
  109. $this->assertEquals(ErrorCodeConstants::REDIRECT, $data->code);
  110. //书籍不存在
  111. $data = BookService::instance()->getBookChapterInfo('-1', '', '');
  112. $this->assertEquals('public/nobook', $data->data['tpl']);
  113. $this->assertEquals(ErrorCodeConstants::REDIRECT_VIEW, $data->code);
  114. //正常书籍
  115. $book_list = BookService::instance()->getBookModel()->where('state', BookConstants::BOOK_STATE_ON_SALE)->limit(10)->select();
  116. foreach ($book_list as $book) {
  117. $book_id = $book['id'];
  118. $chapter_list = BookService::instance()->getChapterList($book_id)->data;
  119. if ($chapter_list['data']) {
  120. $chapter_id = $chapter_list['data'][0]['id'];
  121. $file = BookService::instance()->getChapterContentPath($path, $book_id, $chapter_id);
  122. $data = BookService::instance()->getBookChapterInfo($book_id, $chapter_list['data'][0]['id'], '');
  123. $this->assertInstanceOf(ReturnObject::class, $data);
  124. if (file_exists($file)) {
  125. $this->assertEquals(array_keys($data->data), ['chapter', 'book', 'pre_link', 'next_link']);
  126. } else {
  127. if(array_key_exists('next_link', $data->data['bind'])){
  128. //书籍内容丢失
  129. $this->assertEquals(ErrorCodeConstants::REDIRECT_VIEW, $data->code);
  130. $this->assertEquals('public/nocontent', $data->data['tpl']);
  131. echo "书籍章节丢失,无内容,有下章: $file\n";
  132. }else{
  133. //书籍内容丢失
  134. $this->assertEquals(ErrorCodeConstants::REDIRECT_VIEW, $data->code);
  135. $this->assertEquals('public/nobook', $data->data['tpl']);
  136. echo "书籍章节丢失,无内容,无下章: $file\n";
  137. }
  138. }
  139. }
  140. }
  141. //下架测试
  142. $book_off_sale = BookService::instance()->getBookModel()->where('state', BookConstants::BOOK_STATE_OFF_SALE)->find();
  143. $book_id = $book_off_sale['id'];
  144. $chapter_list = BookService::instance()->getChapterList($book_id)->data;
  145. if ($chapter_list['data']) {
  146. $data = BookService::instance()->getBookChapterInfo($book_id, $chapter_list['data'][0]['id'], '');
  147. $this->assertInstanceOf(ReturnObject::class, $data);
  148. $this->assertEquals(ErrorCodeConstants::REDIRECT_VIEW, $data->code);
  149. $this->assertEquals('public/forbiddenread', $data->data['tpl']);
  150. }
  151. }
  152. public function testBook()
  153. {
  154. for ($i = 0; $i < 20; $i++) {
  155. BookService::instance()->getBookModel()->getChapterInfo('11000000108', '120439');
  156. }
  157. $data = BookService::instance()->getChapterInfo('11000000108', '120439');
  158. var_dump($data);
  159. }
  160. public function testEditedChapter()
  161. {
  162. $cacheKey = \app\main\constants\CacheConstants::BOOK_EDITED_CHAPTER.'103264';
  163. $data = BookService::instance()->getChapterEditedModel()->getChapterFromDb($cacheKey);
  164. var_dump($data);
  165. }
  166. public function testGetChapter()
  167. {
  168. $list = BookService::instance()->getBookModel()
  169. ->where('cast(word_count as UNSIGNED) between 1 and 1000000')
  170. ->column('word_count');
  171. var_dump($list);
  172. }
  173. public function testReturnRecommand()
  174. {
  175. echo \think\Env::get('app.return_filter_recent_num', 100);
  176. // $book = BookService::instance()->getReturnRecommandBook();
  177. // var_dump($book);
  178. }
  179. }