[NFC] Add in unit test to lock in the fix for the is_deleted in where clause issue
authorSeamus Lee <seamuslee001@gmail.com>
Wed, 30 Jun 2021 21:54:05 +0000 (07:54 +1000)
committerSeamus Lee <seamuslee001@gmail.com>
Mon, 5 Jul 2021 03:17:07 +0000 (13:17 +1000)
Move Unit test into its own class and use normal apiv4 wrapper rather than internal class as per Coleman

tests/phpunit/api/v4/Action/ContactIsDeletedTest.php [new file with mode: 0644]

diff --git a/tests/phpunit/api/v4/Action/ContactIsDeletedTest.php b/tests/phpunit/api/v4/Action/ContactIsDeletedTest.php
new file mode 100644 (file)
index 0000000..7892f43
--- /dev/null
@@ -0,0 +1,81 @@
+<?php
+
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC https://civicrm.org/licensing
+ */
+
+
+namespace api\v4\Action;
+
+use api\v4\UnitTestCase;
+
+/**
+ * @group headless
+ */
+class ContactIsDeletedTest extends UnitTestCase {
+
+  public function setUpHeadless() {
+    $relatedTables = [
+      'civicrm_address',
+      'civicrm_email',
+      'civicrm_phone',
+      'civicrm_openid',
+      'civicrm_im',
+      'civicrm_website',
+      'civicrm_activity',
+      'civicrm_activity_contact',
+    ];
+    $this->cleanup(['tablesToTruncate' => $relatedTables]);
+    $displayNameFormat = '{contact.first_name}{ }{contact.last_name}';
+    \Civi::settings()->set('display_name_format', $displayNameFormat);
+
+    return parent::setUpHeadless();
+  }
+
+  /**
+   * This locks in a fix to ensure that if a user doesn't have permission to view the is_deleted field that doesn't hard fail if that field happens to be in an APIv4 call.
+   */
+  public function testIsDeletedPermission(): void {
+    $contact = $this->createLoggedInUser();
+    \CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM', 'view all contacts'];
+    $originalQuery = civicrm_api4('Contact', 'get', [
+      'checkPermissions' => TRUE,
+      'select' => ['id', 'display_name', 'is_deleted'],
+      'where' => [['first_name', '=', 'phoney']],
+    ]);
+
+    try {
+      $isDeletedQuery = civicrm_api4('Contact', 'get', [
+        'checkPermissions' => TRUE,
+        'select' => ['id', 'display_name'],
+        'where' => [['first_name', '=', 'phoney'], ['is_deleted', '=', 0]],
+      ]);
+      $this->assertEquals(count($originalQuery), count($isDeletedQuery));
+    }
+    catch (\API_Exception $e) {
+      $this->fail('An Exception Should not have been raised');
+    }
+    try {
+      $isDeletedJoinTest = civicrm_api4('Email', 'get', [
+        'checkPermissions' => TRUE,
+        'where' => [['contact_id.first_name', '=', 'phoney'], ['contact_id.is_deleted', '=', 0]],
+      ]);
+    }
+    catch (\API_Exception $e) {
+      $this->fail('An Exception Should not have been raised');
+    }
+  }
+
+}