Cleanup in event class
authorEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 6 Jun 2023 02:50:53 +0000 (14:50 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 6 Jun 2023 02:50:53 +0000 (14:50 +1200)
tests/phpunit/CRM/Event/BAO/EventPermissionsTest.php

index 82a4881dbeb5dff6867b7f6774b368fdcd966a95..a65878bc58fb9a94cb02e58e0ca76060f422daf0 100644 (file)
@@ -22,11 +22,6 @@ class CRM_Event_BAO_EventPermissionsTest extends CiviUnitTestCase {
    */
   private $contactID;
 
-  /**
-   * @var int
-   */
-  private $otherContactID;
-
   /**
    * @var int
    */
@@ -44,122 +39,136 @@ class CRM_Event_BAO_EventPermissionsTest extends CiviUnitTestCase {
     $this->createOtherEvent();
   }
 
-  public function createOwnEvent() {
+  public function createOwnEvent(): void {
     $event = $this->eventCreateUnpaid([
       'created_id' => $this->contactID,
     ]);
     $this->ownEventID = $event['id'];
   }
 
-  public function createOtherEvent() {
-    $this->otherContactID = $this->contactID + 1;
+  public function createOtherEvent(): void {
+    $otherContactID = $this->contactID + 1;
     $event = $this->eventCreateUnpaid([
-      'created_id' => $this->otherContactID,
-    ]);
+      'created_id' => $otherContactID,
+    ], 'other');
     $this->otherEventID = $event['id'];
   }
 
-  private function setViewOwnEventPermissions() {
+  private function setViewOwnEventPermissions(): void {
     CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM', 'access CiviEvent', 'view event info'];
+    unset(\Civi::$statics['CRM_Event_BAO_Event']['permissions']);
   }
 
-  private function setViewAllEventPermissions() {
+  private function setViewAllEventPermissions(): void {
     CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM', 'access CiviEvent', 'view event info', 'view event participants'];
+    unset(\Civi::$statics['CRM_Event_BAO_Event']['permissions']);
   }
 
-  private function setEditAllEventPermissions() {
+  private function setEditAllEventPermissions(): void {
     CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM', 'access CiviEvent', 'view event info', 'edit all events'];
+    unset(\Civi::$statics['CRM_Event_BAO_Event']['permissions']);
   }
 
-  private function setDeleteAllEventPermissions() {
+  private function setDeleteAllEventPermissions(): void {
     CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM', 'access CiviEvent', 'view event info', 'delete in CiviEvent'];
+    unset(\Civi::$statics['CRM_Event_BAO_Event']['permissions']);
   }
 
-  public function testViewOwnEvent() {
+  /**
+   * @throws \CRM_Core_Exception
+   */
+  public function testViewOwnEvent(): void {
     $this->setViewOwnEventPermissions();
-    unset(\Civi::$statics['CRM_Event_BAO_Event']['permissions']);
-    $permissions = CRM_Event_BAO_Event::checkPermission($this->ownEventID, CRM_Core_Permission::VIEW);
-    $this->assertTrue($permissions);
+    $this->assertTrue(CRM_Event_BAO_Event::checkPermission($this->ownEventID, CRM_Core_Permission::VIEW));
     // Now check that caching is actually working
     \Civi::$statics['CRM_Event_BAO_Event']['permission']['view'][$this->ownEventID] = FALSE;
     $permissions = CRM_Event_BAO_Event::checkPermission($this->ownEventID, CRM_Core_Permission::VIEW);
     $this->assertFalse($permissions);
   }
 
-  public function testEditOwnEvent() {
+  /**
+   * @throws \CRM_Core_Exception
+   */
+  public function testEditOwnEvent(): void {
     $this->setViewOwnEventPermissions();
-    unset(\Civi::$statics['CRM_Event_BAO_Event']['permissions']);
     $permissions = CRM_Event_BAO_Event::checkPermission($this->ownEventID, CRM_Core_Permission::EDIT);
     $this->assertTrue($permissions);
   }
 
   /**
    * This requires the same permissions as testDeleteOtherEvent()
+   *
+   * @throws \CRM_Core_Exception
    */
-  public function testDeleteOwnEvent() {
+  public function testDeleteOwnEvent(): void {
     // Check that you can't delete your own event without "Delete in CiviEvent" permission
     $this->setViewOwnEventPermissions();
-    unset(\Civi::$statics['CRM_Event_BAO_Event']['permissions']);
-    $permissions = CRM_Event_BAO_Event::checkPermission($this->ownEventID, CRM_Core_Permission::DELETE);
-    $this->assertFalse($permissions);
+    $this->assertFalse(CRM_Event_BAO_Event::checkPermission($this->ownEventID, CRM_Core_Permission::DELETE));
   }
 
-  public function testViewOtherEventDenied() {
-    self::setViewOwnEventPermissions();
-    unset(\Civi::$statics['CRM_Event_BAO_Event']['permissions']);
-    $permissions = CRM_Event_BAO_Event::checkPermission($this->otherEventID, CRM_Core_Permission::VIEW);
-    $this->assertFalse($permissions);
+  /**
+   * @throws \CRM_Core_Exception
+   */
+  public function testViewOtherEventDenied(): void {
+    $this->setViewOwnEventPermissions();
+    $this->assertFalse(CRM_Event_BAO_Event::checkPermission($this->otherEventID, CRM_Core_Permission::VIEW));
   }
 
-  public function testViewOtherEventAllowed() {
-    self::setViewAllEventPermissions();
-    unset(\Civi::$statics['CRM_Event_BAO_Event']['permissions']);
-    $permissions = CRM_Event_BAO_Event::checkPermission($this->otherEventID, CRM_Core_Permission::VIEW);
-    $this->assertTrue($permissions);
+  /**
+   * @throws \CRM_Core_Exception
+   */
+  public function testViewOtherEventAllowed(): void {
+    $this->setViewAllEventPermissions();
+    $this->assertTrue(CRM_Event_BAO_Event::checkPermission($this->otherEventID, CRM_Core_Permission::VIEW));
   }
 
   /**
    * Test that the contact can view an event with an ACL permitting everyone to view it.
    */
-  public function testViewAclEventAllowed() {
+  public function testViewAclEventAllowed(): void {
     $this->setupScenarioCoreACLEveryonePermittedToEvent();
     $permittedEventID = CRM_Core_Permission::event(CRM_Core_Permission::VIEW, $this->scenarioIDs['Event']['permitted_event']);
     $this->assertEquals($this->scenarioIDs['Event']['permitted_event'], $permittedEventID);
   }
 
-  public function testEditOtherEventDenied() {
+  /**
+   * @throws \CRM_Core_Exception
+   */
+  public function testEditOtherEventDenied(): void {
     $this->setViewAllEventPermissions();
-    unset(\Civi::$statics['CRM_Event_BAO_Event']['permissions']);
-    $permissions = CRM_Event_BAO_Event::checkPermission($this->otherEventID, CRM_Core_Permission::EDIT);
-    $this->assertFalse($permissions);
+    $this->assertFalse(CRM_Event_BAO_Event::checkPermission($this->otherEventID, CRM_Core_Permission::EDIT));
   }
 
-  public function testEditOtherEventAllowed() {
-    self::setEditAllEventPermissions();
-    unset(\Civi::$statics['CRM_Event_BAO_Event']['permissions']);
-    $permissions = CRM_Event_BAO_Event::checkPermission($this->otherEventID, CRM_Core_Permission::EDIT);
-    $this->assertTrue($permissions);
+  /**
+   * @throws \CRM_Core_Exception
+   */
+  public function testEditOtherEventAllowed(): void {
+    $this->setEditAllEventPermissions();
+    $this->assertTrue(CRM_Event_BAO_Event::checkPermission($this->otherEventID, CRM_Core_Permission::EDIT));
   }
 
-  public function testDeleteOtherEventAllowed() {
-    self::setDeleteAllEventPermissions();
-    unset(\Civi::$statics['CRM_Event_BAO_Event']['permissions']);
-    $permissions = CRM_Event_BAO_Event::checkPermission($this->otherEventID, CRM_Core_Permission::DELETE);
-    $this->assertTrue($permissions);
+  /**
+   * @throws \CRM_Core_Exception
+   */
+  public function testDeleteOtherEventAllowed(): void {
+    $this->setDeleteAllEventPermissions();
+    $this->assertTrue(CRM_Event_BAO_Event::checkPermission($this->otherEventID, CRM_Core_Permission::DELETE));
   }
 
-  public function testDeleteOtherEventDenied() {
-    // FIXME: This test could be improved, but for now it checks that we can't delete if we don't have "Delete in CiviEvent"
+  /**
+   * Test checks that we can't delete if we don't have "Delete in CiviEvent"
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testDeleteOtherEventDenied(): void {
     $this->setEditAllEventPermissions();
-    unset(\Civi::$statics['CRM_Event_BAO_Event']['permissions']);
-    $permissions = CRM_Event_BAO_Event::checkPermission($this->otherEventID, CRM_Core_Permission::DELETE);
-    $this->assertFalse($permissions);
+    $this->assertFalse(CRM_Event_BAO_Event::checkPermission($this->otherEventID, CRM_Core_Permission::DELETE));
   }
 
   /**
    * Test get complete info function returns all info for contacts with view all info.
    */
-  public function testGetCompleteInfo() {
+  public function testGetCompleteInfo(): void {
     $this->setupScenarioCoreACLEveryonePermittedToEvent();
     $info = CRM_Event_BAO_Event::getCompleteInfo('20000101');
     $this->assertEquals('Annual CiviCRM meet', $info[0]['title']);