BxPostsModuleServicesTest.php 5.3 KB

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