BxPostsModuleServicesTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
  3. /**
  4. * Content-module services, alerts, and timeline helpers via Posts.
  5. */
  6. #[AllowMockObjectsWithoutExpectations]
  7. class BxPostsModuleServicesTest extends BxPostsTestCase
  8. {
  9. public function testSafeServicesIncludeTextAndGeneralEndpoints()
  10. {
  11. $a = $this->_oModule->serviceGetSafeServices();
  12. foreach (['GetLink', 'Browse', 'EntityCreate', 'BrowsePublic', 'BrowseAuthor', 'GetMenuAddonManageToolsProfileStats'] as $sName)
  13. $this->assertArrayHasKey($sName, $a);
  14. }
  15. public function testModuleIconAndSample()
  16. {
  17. $this->assertSame('file-alt', $this->_oModule->serviceModuleIcon());
  18. $this->assertNotEmpty($this->_oModule->serviceModuleSample());
  19. $this->assertTrue($this->_oModule->serviceIsAllowedPostInContext());
  20. }
  21. public function testFormsHelper()
  22. {
  23. $this->assertInstanceOf(BxPostsFormsEntryHelper::class, $this->_oModule->getFormsHelper());
  24. $this->assertInstanceOf(BxPostsFormsEntryHelper::class, $this->_oModule->serviceFormsHelper());
  25. }
  26. public function testBuildRssParams()
  27. {
  28. $this->assertSame(['author' => 7], $this->bxCallProtected($this->_oModule, '_buildRssParams', 'author', [7]));
  29. $this->assertSame(['author' => ''], $this->bxCallProtected($this->_oModule, '_buildRssParams', 'author', []));
  30. $this->assertSame([], $this->bxCallProtected($this->_oModule, '_buildRssParams', 'public', [1]));
  31. }
  32. public function testPrepareBrowsingFiltersParamsGet()
  33. {
  34. $this->assertSame(['mode' => 'public'], $this->bxCallProtected(
  35. $this->_oModule,
  36. '_prepareBrowsingFiltersParamsGet',
  37. ['mode' => 'public', 'ignored' => 1]
  38. ));
  39. $this->assertSame([], $this->bxCallProtected($this->_oModule, '_prepareBrowsingFiltersParamsGet', []));
  40. }
  41. public function testAlertParams()
  42. {
  43. $aPost = $this->bxSamplePost(['allow_view_to' => 5, 'cf' => 2, 'status' => 'awaiting']);
  44. $a = $this->bxCallProtected($this->_oModule, '_alertParams', $aPost);
  45. $this->assertSame('awaiting', $a['status']);
  46. $this->assertSame('active', $a['status_admin']);
  47. $this->assertSame(5, $a['privacy_view']);
  48. $this->assertSame(2, $a['cf']);
  49. }
  50. public function testAlertParamsAddIncludesTimelineGroup()
  51. {
  52. $aPost = $this->bxSamplePost(['author' => -10, 'id' => 15]);
  53. $a = $this->bxCallProtected($this->_oModule, '_alertParamsAdd', $aPost);
  54. $this->assertSame('bx_posts_10_15', $a['timeline_group']['by']);
  55. $this->assertSame('owner_id', $a['timeline_group']['field']);
  56. }
  57. public function testTimelinePostMissingOrInactive()
  58. {
  59. $oDb = $this->createMock(BxPostsDb::class);
  60. $oDb->method('getContentInfoById')->willReturnOnConsecutiveCalls(
  61. false,
  62. $this->bxSamplePost(['status' => 'awaiting']),
  63. $this->bxSamplePost(['status_admin' => 'pending'])
  64. );
  65. $this->bxReplaceDb($oDb);
  66. $aEvent = ['object_id' => 15, 'owner_id' => 10];
  67. $this->assertFalse($this->_oModule->serviceGetTimelinePost($aEvent));
  68. $this->assertFalse($this->_oModule->serviceGetTimelinePost($aEvent));
  69. $this->assertFalse($this->_oModule->serviceGetTimelinePost($aEvent));
  70. }
  71. public function testTimelinePostHidesAnonymousFromNonAuthor()
  72. {
  73. $oDb = $this->createMock(BxPostsDb::class);
  74. $oDb->method('getContentInfoById')->willReturn($this->bxSamplePost(['author' => -10]));
  75. $this->bxReplaceDb($oDb);
  76. $this->assertFalse($this->_oModule->serviceGetTimelinePost([
  77. 'object_id' => 15,
  78. 'owner_id' => 10,
  79. ]));
  80. }
  81. public function testTimelinePostUsesLaterPublishedDate()
  82. {
  83. $aPost = $this->bxSamplePost([
  84. 'added' => 100,
  85. 'published' => 250,
  86. ]);
  87. $oDb = $this->createMock(BxPostsDb::class);
  88. $oDb->method('getContentInfoById')->willReturn($aPost);
  89. $this->bxReplaceDb($oDb);
  90. $aResult = $this->_oModule->serviceGetTimelinePost([
  91. 'object_id' => 15,
  92. 'owner_id' => 10,
  93. ]);
  94. if ($aResult === false)
  95. $this->markTestSkipped('Timeline post assembly requires optional content objects.');
  96. $this->assertIsArray($aResult);
  97. $this->assertSame(250, $aResult['date']);
  98. $this->assertSame('file-alt', $aResult['icon']);
  99. }
  100. public function testGetContentOwnerProfileIdWithoutContentUsesViewer()
  101. {
  102. $this->assertSame(bx_get_logged_profile_id(), $this->_oModule->serviceGetContentOwnerProfileId(0));
  103. }
  104. public function testDecodeDataApiPrivateContent()
  105. {
  106. $o = new class($this->_oModule->_aModule) extends BxPostsModule {
  107. public $mixedViewResult = 'denied';
  108. public function checkAllowedView($aDataEntry, $isPerformAction = false)
  109. {
  110. return $this->mixedViewResult;
  111. }
  112. };
  113. $a = $o->decodeDataAPI($this->bxSamplePost());
  114. $this->assertSame(15, $a['id']);
  115. $this->assertSame('sys_private', $a['module']);
  116. $this->assertSame('bx_posts', $a['module_src']);
  117. $this->assertArrayNotHasKey('text', $a);
  118. }
  119. public function testServiceIsAllowedAddContentToContextZero()
  120. {
  121. $this->assertFalse($this->_oModule->serviceIsAllowedAddContentToContext(0));
  122. }
  123. }