BxDolAclTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. use PHPUnit\Framework\Attributes\DataProvider;
  3. class BxDolAclTestDouble extends BxDolAcl
  4. {
  5. public $iLevelBit = 0;
  6. public function __construct()
  7. {
  8. }
  9. public function getMemberLevelBit($iProfileId = 0)
  10. {
  11. return $this->iLevelBit;
  12. }
  13. }
  14. /**
  15. * Membership bitsets and search-condition structure (no DB tracking).
  16. */
  17. class BxDolAclTest extends \PHPUnit\Framework\TestCase
  18. {
  19. #[DataProvider('providerForMemberLevelInSet')]
  20. public function testIsMemberLevelInSet($iLevelBit, $mixedPermissions, $bOut)
  21. {
  22. $o = new BxDolAclTestDouble();
  23. $o->iLevelBit = $iLevelBit;
  24. $this->assertSame($bOut, (bool)$o->isMemberLevelInSet($mixedPermissions));
  25. }
  26. static public function providerForMemberLevelInSet()
  27. {
  28. $iStandardBit = (int)pow(2, MEMBERSHIP_ID_STANDARD - 1);
  29. $iAdminBit = (int)pow(2, MEMBERSHIP_ID_ADMINISTRATOR - 1);
  30. return [
  31. [$iStandardBit, [MEMBERSHIP_ID_STANDARD], true],
  32. [$iStandardBit, [MEMBERSHIP_ID_ADMINISTRATOR], false],
  33. [$iStandardBit, $iStandardBit, true],
  34. [$iStandardBit, $iAdminBit, false],
  35. [$iStandardBit, [MEMBERSHIP_ID_STANDARD, MEMBERSHIP_ID_ADMINISTRATOR], true],
  36. [$iStandardBit, [], false],
  37. [$iStandardBit, 0, false],
  38. ];
  39. }
  40. public function testContentByLevelAsConditionUnconfirmed()
  41. {
  42. $o = new BxDolAclTestDouble();
  43. $a = $o->getContentByLevelAsCondition('author_id', MEMBERSHIP_ID_UNCONFIRMED);
  44. $this->assertStringContainsString('email_confirmed` = 0', $a['restriction_sql']);
  45. $this->assertSame([], $a['restriction']);
  46. $this->assertSame([], $a['join']);
  47. }
  48. public function testContentByLevelAsConditionUnconfirmedFromSingleElementArray()
  49. {
  50. $o = new BxDolAclTestDouble();
  51. $a = $o->getContentByLevelAsCondition('author_id', [MEMBERSHIP_ID_UNCONFIRMED]);
  52. $this->assertStringContainsString('email_confirmed` = 0', $a['restriction_sql']);
  53. $this->assertSame([], $a['join']);
  54. }
  55. public function testContentByLevelAsConditionStandard()
  56. {
  57. $o = new BxDolAclTestDouble();
  58. $a = $o->getContentByLevelAsCondition('author_id', MEMBERSHIP_ID_STANDARD);
  59. $this->assertStringContainsString('IDMember` IS NULL', $a['restriction_sql']);
  60. $this->assertStringContainsString('email_confirmed` != 0', $a['restriction_sql']);
  61. $this->assertSame('LEFT', $a['join']['acl_members']['type']);
  62. $this->assertSame('author_id', $a['join']['acl_members']['mainField']);
  63. $this->assertSame([], $a['restriction']);
  64. }
  65. public function testContentByLevelAsConditionCustom()
  66. {
  67. $o = new BxDolAclTestDouble();
  68. $a = $o->getContentByLevelAsCondition('author_id', MEMBERSHIP_ID_MODERATOR);
  69. $this->assertSame('INNER', $a['join']['acl_members']['type']);
  70. $this->assertSame('=', $a['restriction']['acl_members']['operator']);
  71. $this->assertSame(MEMBERSHIP_ID_MODERATOR, $a['restriction']['acl_members']['value']);
  72. $this->assertStringContainsString('email_confirmed` != 0', $a['restriction_sql']);
  73. }
  74. public function testContentByLevelAsConditionCustomListUsesIn()
  75. {
  76. $o = new BxDolAclTestDouble();
  77. $aLevels = [MEMBERSHIP_ID_MODERATOR, MEMBERSHIP_ID_ADMINISTRATOR];
  78. $a = $o->getContentByLevelAsCondition('author_id', $aLevels);
  79. $this->assertSame('in', $a['restriction']['acl_members']['operator']);
  80. $this->assertSame($aLevels, $a['restriction']['acl_members']['value']);
  81. $this->assertSame('INNER', $a['join']['acl_members']['type']);
  82. }
  83. }