BxPostsModulePermissionsTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
  3. use PHPUnit\Framework\Attributes\DataProvider;
  4. /**
  5. * Content-module permission and status checks via Posts.
  6. */
  7. #[AllowMockObjectsWithoutExpectations]
  8. class BxPostsModulePermissionsTest extends BxPostsTestCase
  9. {
  10. #[DataProvider('providerForEntryAuthor')]
  11. public function testIsEntryAuthor($iAuthor, $iViewer, $bOut)
  12. {
  13. $aPost = $this->bxSamplePost(['author' => $iAuthor]);
  14. $this->assertSame($bOut, $this->_oModule->isEntryAuthor($aPost, $iViewer));
  15. }
  16. static public function providerForEntryAuthor()
  17. {
  18. return [
  19. [10, 10, true],
  20. [-10, 10, true],
  21. [10, 11, false],
  22. [0, 10, false],
  23. ];
  24. }
  25. public function testIsEntryActiveForAuthorIgnoresStatus()
  26. {
  27. $iViewer = (int)bx_get_logged_profile_id();
  28. $aPost = $this->bxSamplePost([
  29. 'author' => $iViewer,
  30. 'status' => 'hidden',
  31. 'status_admin' => 'pending',
  32. ]);
  33. $this->assertTrue($this->_oModule->isEntryActive($aPost));
  34. }
  35. public function testIsEntryActiveHiddenForStranger()
  36. {
  37. if ($this->_oModule->_isModerator())
  38. $this->markTestSkipped('Moderator always sees hidden entries.');
  39. $aPost = $this->bxSamplePost(['author' => 10, 'status' => 'hidden']);
  40. if ($this->_oModule->isEntryAuthor($aPost, (int)bx_get_logged_profile_id()))
  41. $this->markTestSkipped('Logged profile is the sample author.');
  42. $this->assertFalse($this->_oModule->isEntryActive($aPost));
  43. }
  44. public function testIsEntryActivePendingAdminForStranger()
  45. {
  46. if ($this->_oModule->_isModerator())
  47. $this->markTestSkipped('Moderator always sees hidden entries.');
  48. $aPost = $this->bxSamplePost(['author' => 10, 'status_admin' => 'pending']);
  49. if ($this->_oModule->isEntryAuthor($aPost, (int)bx_get_logged_profile_id()))
  50. $this->markTestSkipped('Logged profile is the sample author.');
  51. $this->assertFalse($this->_oModule->isEntryActive($aPost));
  52. }
  53. public function testIsEntryActiveWhenPublished()
  54. {
  55. $this->assertTrue($this->_oModule->isEntryActive($this->bxSamplePost(['author' => 10])));
  56. }
  57. public function testCheckAllowedApproveRejectsEmptyAndNonPending()
  58. {
  59. $this->assertNotSame(CHECK_ACTION_RESULT_ALLOWED, $this->_oModule->checkAllowedApprove([]));
  60. $this->assertNotSame(CHECK_ACTION_RESULT_ALLOWED, $this->_oModule->checkAllowedApprove($this->bxSamplePost()));
  61. $this->assertFalse($this->_oModule->isAllowedApprove($this->bxSamplePost()));
  62. }
  63. public function testCheckAllowedApprovePendingWithoutModerator()
  64. {
  65. $this->bxSetProtected($this->_oModule, '_iProfileId', 99, BxBaseModGeneralModule::class);
  66. $aPost = $this->bxSamplePost(['status_admin' => BX_BASE_MOD_GENERAL_STATUS_PENDING]);
  67. $this->assertNotSame(CHECK_ACTION_RESULT_ALLOWED, $this->_oModule->checkAllowedApprove($aPost));
  68. }
  69. public function testCheckAllowedEditAllowsAuthor()
  70. {
  71. $this->bxSetProtected($this->_oModule, '_iProfileId', 10, BxBaseModGeneralModule::class);
  72. $this->assertSame(CHECK_ACTION_RESULT_ALLOWED, $this->_oModule->checkAllowedEdit($this->bxSamplePost()));
  73. }
  74. public function testCheckAllowedEditDeniesStranger()
  75. {
  76. $this->bxSetProtected($this->_oModule, '_iProfileId', 99, BxBaseModGeneralModule::class);
  77. $this->assertNotSame(CHECK_ACTION_RESULT_ALLOWED, $this->_oModule->checkAllowedEdit($this->bxSamplePost()));
  78. }
  79. public function testCheckAllowedBrowseDefaultAllowed()
  80. {
  81. $this->assertSame(CHECK_ACTION_RESULT_ALLOWED, $this->_oModule->checkAllowedBrowse());
  82. $this->assertSame(CHECK_ACTION_RESULT_ALLOWED, $this->_oModule->serviceCheckAllowed('Browse'));
  83. }
  84. public function testServiceCheckAllowedUnknownAction()
  85. {
  86. $s = $this->_oModule->serviceCheckAllowed('No Such Action');
  87. $this->assertNotSame(CHECK_ACTION_RESULT_ALLOWED, $s);
  88. $this->assertNotEmpty($s);
  89. }
  90. public function testCommentsDisabledShortCircuit()
  91. {
  92. $aPost = $this->bxSamplePost(['allow_comments' => 0]);
  93. $oDb = $this->createMock(BxPostsDb::class);
  94. $oDb->method('getContentInfoById')->willReturn($aPost);
  95. $this->bxReplaceDb($oDb);
  96. $this->assertFalse($this->_oModule->serviceCheckAllowedCommentsPost(15, 'bx_posts'));
  97. $this->assertFalse($this->_oModule->serviceCheckAllowedCommentsView(15, 'bx_posts'));
  98. }
  99. public function testCommentsAllowedForNegativeReportId()
  100. {
  101. $oDb = $this->createMock(BxPostsDb::class);
  102. $oDb->method('getContentInfoById')->willReturn(false);
  103. $this->bxReplaceDb($oDb);
  104. $this->assertSame(CHECK_ACTION_RESULT_ALLOWED, $this->_oModule->serviceCheckAllowedCommentsPost(-12, 'bx_posts'));
  105. $this->assertSame(CHECK_ACTION_RESULT_ALLOWED, $this->_oModule->serviceCheckAllowedCommentsView(-12, 'bx_posts'));
  106. }
  107. public function testServiceIsActive()
  108. {
  109. $oDb = $this->createMock(BxPostsDb::class);
  110. $oDb->method('getContentInfoById')->willReturnMap([
  111. [1, $this->bxSamplePost()],
  112. [2, $this->bxSamplePost(['status' => 'hidden'])],
  113. [3, $this->bxSamplePost(['status_admin' => 'pending'])],
  114. [4, false],
  115. ]);
  116. $this->bxReplaceDb($oDb);
  117. $this->assertTrue($this->_oModule->serviceIsActive(1));
  118. $this->assertFalse($this->_oModule->serviceIsActive(2));
  119. $this->assertFalse($this->_oModule->serviceIsActive(3));
  120. $this->assertFalse($this->_oModule->serviceIsActive(4));
  121. }
  122. public function testServiceCheckAllowedWithContentMissing()
  123. {
  124. $oDb = $this->createMock(BxPostsDb::class);
  125. $oDb->method('getContentInfoById')->willReturn(false);
  126. $this->bxReplaceDb($oDb);
  127. $this->assertNotSame(CHECK_ACTION_RESULT_ALLOWED, $this->_oModule->serviceCheckAllowedWithContent('View', 15));
  128. }
  129. }