BxPostsSearchResultTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Content-module search/browse modes via Posts SearchResult.
  4. */
  5. class BxPostsSearchResultTest extends BxPostsTestCase
  6. {
  7. public function testPublicMode()
  8. {
  9. $o = new BxPostsSearchResult('public');
  10. $this->assertFalse((bool)$o->isError);
  11. $this->assertSame('bx_posts', $o->aCurrent['module_name']);
  12. $this->assertSame('bx_posts_posts', $o->aCurrent['table']);
  13. $this->assertSame('last', $o->aCurrent['sorting']);
  14. $this->assertSame('active', $o->aCurrent['restriction']['status']['value']);
  15. $this->assertSame('active', $o->aCurrent['restriction']['statusAdmin']['value']);
  16. $this->assertSame('modules/?r=posts/rss/public', $o->aCurrent['rss']['link']);
  17. $this->assertArrayHasKey('statusAuthor', $o->aCurrent['restriction']);
  18. }
  19. public function testFeaturedPopularTopUpdatedModes()
  20. {
  21. $oFeatured = new BxPostsSearchResult('featured');
  22. $this->assertFalse((bool)$oFeatured->isError);
  23. $this->assertSame('featured', $oFeatured->aCurrent['sorting']);
  24. $this->assertSame('0', $oFeatured->aCurrent['restriction']['featured']['value']);
  25. $oPopular = new BxPostsSearchResult('popular');
  26. $this->assertSame('popular', $oPopular->aCurrent['sorting']);
  27. $oTop = new BxPostsSearchResult('top');
  28. $this->assertSame('top', $oTop->aCurrent['sorting']);
  29. $oUpdated = new BxPostsSearchResult('updated');
  30. $this->assertSame('updated', $oUpdated->aCurrent['sorting']);
  31. $this->assertSame('modules/?r=posts/rss/updated', $oUpdated->aCurrent['rss']['link']);
  32. }
  33. public function testSearchResultsModeDropsPaginationAndRss()
  34. {
  35. $o = new BxPostsSearchResult('');
  36. $this->assertFalse((bool)$o->isError);
  37. $this->assertArrayNotHasKey('perPage', $o->aCurrent['paginate']);
  38. $this->assertArrayNotHasKey('rss', $o->aCurrent);
  39. }
  40. public function testUnknownModeIsError()
  41. {
  42. $o = new BxPostsSearchResult('not-a-mode');
  43. $this->assertTrue((bool)$o->isError);
  44. }
  45. public function testAuthorAndContextWithoutProfileAreErrors()
  46. {
  47. $oAuthor = new BxPostsSearchResult('author', ['author' => 0]);
  48. $this->assertTrue((bool)$oAuthor->isError);
  49. $oContext = new BxPostsSearchResult('context', []);
  50. $this->assertTrue((bool)$oContext->isError);
  51. $oFollowed = new BxPostsSearchResult('followed_contexts', ['author' => 0]);
  52. $this->assertTrue((bool)$oFollowed->isError);
  53. $oFavorite = new BxPostsSearchResult('favorite', ['user' => 0]);
  54. $this->assertTrue((bool)$oFavorite->isError);
  55. }
  56. public function testAlterOrder()
  57. {
  58. $o = new BxPostsSearchResult('public');
  59. $o->aCurrent['sorting'] = 'last';
  60. $this->assertStringContainsString('`added` DESC', $o->getAlterOrder()['order']);
  61. $o->aCurrent['sorting'] = 'updated';
  62. $this->assertStringContainsString('`changed` DESC', $o->getAlterOrder()['order']);
  63. $o->aCurrent['sorting'] = 'featured';
  64. $this->assertStringContainsString('`featured` DESC', $o->getAlterOrder()['order']);
  65. $o->aCurrent['sorting'] = 'popular';
  66. $this->assertStringContainsString('`views` DESC', $o->getAlterOrder()['order']);
  67. $o->aCurrent['sorting'] = 'top';
  68. $aTop = $o->getAlterOrder();
  69. $this->assertArrayHasKey('order', $aTop);
  70. $this->assertNotSame('', $aTop['order']);
  71. }
  72. public function testAddConditionsForFilter()
  73. {
  74. $o = new BxPostsSearchResult('public');
  75. $CNF = $this->_oModule->_oConfig->CNF;
  76. $this->bxCallProtected($o, 'addConditionsForFilter', $CNF, 'public', [
  77. 'filter' => ['field' => 'cat', 'value' => 4, 'operator' => 'in', 'table' => 'table'],
  78. ]);
  79. $this->assertSame(4, $o->aCurrent['restriction']['filter']['value']);
  80. $this->assertSame('cat', $o->aCurrent['restriction']['filter']['field']);
  81. $this->assertSame('in', $o->aCurrent['restriction']['filter']['operator']);
  82. $this->assertSame('bx_posts_posts', $o->aCurrent['restriction']['filter']['table']);
  83. }
  84. public function testGalleryUnitViewSwitchesTemplate()
  85. {
  86. $sPrev = bx_get('unit_view');
  87. $_GET['unit_view'] = 'gallery';
  88. try {
  89. $o = new BxPostsSearchResult('public');
  90. $oTemplate = new ReflectionProperty($o, 'sUnitTemplate');
  91. $oTemplate->setAccessible(true);
  92. $this->assertSame('unit_gallery.html', $oTemplate->getValue($o));
  93. } finally {
  94. if ($sPrev === false)
  95. unset($_GET['unit_view']);
  96. else
  97. $_GET['unit_view'] = $sPrev;
  98. }
  99. }
  100. }