summaryrefslogtreecommitdiff
path: root/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Query
diff options
context:
space:
mode:
Diffstat (limited to 'www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Query')
-rw-r--r--www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Query/Api4SelectQueryComplexJoinTest.php85
-rw-r--r--www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Query/Api4SelectQueryTest.php91
-rw-r--r--www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Query/OneToOneJoinTest.php46
-rw-r--r--www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Query/OptionValueJoinTest.php46
-rw-r--r--www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Query/SelectQueryMultiJoinTest.php74
5 files changed, 342 insertions, 0 deletions
diff --git a/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Query/Api4SelectQueryComplexJoinTest.php b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Query/Api4SelectQueryComplexJoinTest.php
new file mode 100644
index 00000000..0e68843a
--- /dev/null
+++ b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Query/Api4SelectQueryComplexJoinTest.php
@@ -0,0 +1,85 @@
+<?php
+
+namespace Civi\Test\Api4\Query;
+
+use Civi\Api4\Query\Api4SelectQuery;
+use Civi\Test\Api4\UnitTestCase;
+
+/**
+ * @group headless
+ */
+class Api4SelectQueryComplexJoinTest 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]);
+ $this->loadDataSet('SingleContact');
+ return parent::setUpHeadless();
+ }
+
+ public function testWithComplexRelatedEntitySelect() {
+ $query = new Api4SelectQuery('Contact', FALSE);
+ $query->select[] = 'id';
+ $query->select[] = 'display_name';
+ $query->select[] = 'phones.phone';
+ $query->select[] = 'emails.email';
+ $query->select[] = 'emails.location_type.name';
+ $query->select[] = 'created_activities.contact_id';
+ $query->select[] = 'created_activities.activity.subject';
+ $query->select[] = 'created_activities.activity.activity_type.name';
+ $query->where[] = ['first_name', '=', 'Single'];
+ $query->where[] = ['id', '=', $this->getReference('test_contact_1')['id']];
+ $results = $query->run();
+
+ $testActivities = [
+ $this->getReference('test_activity_1'),
+ $this->getReference('test_activity_2'),
+ ];
+ $activitySubjects = array_column($testActivities, 'subject');
+
+ $this->assertCount(1, $results);
+ $firstResult = array_shift($results);
+ $this->assertArrayHasKey('created_activities', $firstResult);
+ $firstCreatedActivity = array_shift($firstResult['created_activities']);
+ $this->assertArrayHasKey('activity', $firstCreatedActivity);
+ $firstActivity = $firstCreatedActivity['activity'];
+ $this->assertContains($firstActivity['subject'], $activitySubjects);
+ $this->assertArrayHasKey('activity_type', $firstActivity);
+ $activityType = $firstActivity['activity_type'];
+ $this->assertArrayHasKey('name', $activityType);
+ }
+
+ public function testWithSelectOfOrphanDeepValues() {
+ $query = new Api4SelectQuery('Contact', FALSE);
+ $query->select[] = 'id';
+ $query->select[] = 'first_name';
+ $query->select[] = 'emails.location_type.name'; // emails not selected
+ $results = $query->run();
+ $firstResult = array_shift($results);
+
+ $this->assertEmpty($firstResult['emails']);
+ }
+
+ public function testOrderDoesNotMatter() {
+ $query = new Api4SelectQuery('Contact', FALSE);
+ $query->select[] = 'id';
+ $query->select[] = 'first_name';
+ $query->select[] = 'emails.location_type.name'; // before emails selection
+ $query->select[] = 'emails.email';
+ $query->where[] = ['emails.email', 'IS NOT NULL'];
+ $results = $query->run();
+ $firstResult = array_shift($results);
+
+ $this->assertNotEmpty($firstResult['emails'][0]['location_type']['name']);
+ }
+
+}
diff --git a/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Query/Api4SelectQueryTest.php b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Query/Api4SelectQueryTest.php
new file mode 100644
index 00000000..ca9a5851
--- /dev/null
+++ b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Query/Api4SelectQueryTest.php
@@ -0,0 +1,91 @@
+<?php
+
+namespace Civi\Test\Api4\Query;
+
+use Civi\Api4\Query\Api4SelectQuery;
+use Civi\Test\Api4\UnitTestCase;
+
+/**
+ * @group headless
+ */
+class Api4SelectQueryTest 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]);
+ $this->loadDataSet('DefaultDataSet');
+ $displayNameFormat = '{contact.first_name}{ }{contact.last_name}';
+ \Civi::settings()->set('display_name_format', $displayNameFormat);
+
+ return parent::setUpHeadless();
+ }
+
+ public function testWithSingleWhereJoin() {
+ $phoneNum = $this->getReference('test_phone_1')['phone'];
+
+ $query = new Api4SelectQuery('Contact', FALSE);
+ $query->where[] = ['phones.phone', '=', $phoneNum];
+ $results = $query->run();
+
+ $this->assertCount(1, $results);
+ }
+
+ public function testOneToManyJoin() {
+ $phoneNum = $this->getReference('test_phone_1')['phone'];
+
+ $query = new Api4SelectQuery('Contact', FALSE);
+ $query->select[] = 'id';
+ $query->select[] = 'first_name';
+ $query->select[] = 'phones.phone';
+ $query->where[] = ['phones.phone', '=', $phoneNum];
+ $results = $query->run();
+
+ $this->assertCount(1, $results);
+ $firstResult = array_shift($results);
+ $this->assertArrayHasKey('phones', $firstResult);
+ $firstPhone = array_shift($firstResult['phones']);
+ $this->assertEquals($phoneNum, $firstPhone['phone']);
+ }
+
+ public function testManyToOneJoin() {
+ $phoneNum = $this->getReference('test_phone_1')['phone'];
+ $contact = $this->getReference('test_contact_1');
+
+ $query = new Api4SelectQuery('Phone', FALSE);
+ $query->select[] = 'id';
+ $query->select[] = 'phone';
+ $query->select[] = 'contact.display_name';
+ $query->select[] = 'contact.first_name';
+ $query->where[] = ['phone', '=', $phoneNum];
+ $results = $query->run();
+
+ $this->assertCount(1, $results);
+ $firstResult = array_shift($results);
+ $this->assertArrayHasKey('contact', $firstResult);
+ $resultContact = $firstResult['contact'];
+ $this->assertEquals($contact['display_name'], $resultContact['display_name']);
+ }
+
+ public function testOneToManyMultipleJoin() {
+ $query = new Api4SelectQuery('Contact', FALSE);
+ $query->select[] = 'id';
+ $query->select[] = 'first_name';
+ $query->select[] = 'phones.phone';
+ $query->where[] = ['first_name', '=', 'Phoney'];
+ $results = $query->run();
+ $result = array_pop($results);
+
+ $this->assertEquals('Phoney', $result['first_name']);
+ $this->assertCount(2, $result['phones']);
+ }
+
+}
diff --git a/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Query/OneToOneJoinTest.php b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Query/OneToOneJoinTest.php
new file mode 100644
index 00000000..ef05f657
--- /dev/null
+++ b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Query/OneToOneJoinTest.php
@@ -0,0 +1,46 @@
+<?php
+
+namespace Civi\Test\Api4\Query;
+
+use Civi\Api4\Contact;
+use Civi\Api4\OptionGroup;
+use Civi\Api4\OptionValue;
+use Civi\Test\Api4\UnitTestCase;
+
+/**
+ * Class OneToOneJoinTest
+ * @package Civi\Test\Api4\Query
+ * @group headless
+ */
+class OneToOneJoinTest extends UnitTestCase {
+
+ public function testOneToOneJoin() {
+ $armenianContact = Contact::create()
+ ->addValue('first_name', 'Contact')
+ ->addValue('last_name', 'One')
+ ->addValue('contact_type', 'Individual')
+ ->addValue('preferred_language', 'hy_AM')
+ ->execute()
+ ->first();
+
+ $basqueContact = Contact::create()
+ ->addValue('first_name', 'Contact')
+ ->addValue('last_name', 'Two')
+ ->addValue('contact_type', 'Individual')
+ ->addValue('preferred_language', 'eu_ES')
+ ->execute()
+ ->first();
+
+ $contacts = Contact::get()
+ ->addWhere('id', 'IN', [$armenianContact['id'], $basqueContact['id']])
+ ->addSelect('preferred_language.label')
+ ->addSelect('last_name')
+ ->execute()
+ ->indexBy('last_name')
+ ->getArrayCopy();
+
+ $this->assertEquals($contacts['One']['preferred_language']['label'], 'Armenian');
+ $this->assertEquals($contacts['Two']['preferred_language']['label'], 'Basque');
+ }
+
+}
diff --git a/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Query/OptionValueJoinTest.php b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Query/OptionValueJoinTest.php
new file mode 100644
index 00000000..75d4b977
--- /dev/null
+++ b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Query/OptionValueJoinTest.php
@@ -0,0 +1,46 @@
+<?php
+
+namespace Civi\Test\Api4\Query;
+
+use Civi\Api4\Query\Api4SelectQuery;
+use Civi\Test\Api4\UnitTestCase;
+
+/**
+ * @group headless
+ */
+class OptionValueJoinTest 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]);
+ $this->loadDataSet('SingleContact');
+
+ return parent::setUpHeadless();
+ }
+
+ public function testCommunicationMethodJoin() {
+ $query = new Api4SelectQuery('Contact', FALSE);
+ $query->select[] = 'first_name';
+ $query->select[] = 'preferred_communication_method.label';
+ $query->where[] = ['preferred_communication_method', 'IS NOT NULL'];
+ $results = $query->run();
+ $first = array_shift($results);
+ $firstPreferredMethod = array_shift($first['preferred_communication_method']);
+
+ $this->assertEquals(
+ 'Phone',
+ $firstPreferredMethod['label']
+ );
+ }
+
+}
diff --git a/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Query/SelectQueryMultiJoinTest.php b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Query/SelectQueryMultiJoinTest.php
new file mode 100644
index 00000000..860bd785
--- /dev/null
+++ b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Query/SelectQueryMultiJoinTest.php
@@ -0,0 +1,74 @@
+<?php
+
+namespace Civi\Test\Api4\Query;
+
+use Civi\Api4\Contact;
+use Civi\Api4\Email;
+use Civi\Test\Api4\UnitTestCase;
+
+/**
+ * Class SelectQueryMultiJoinTest
+ * @package Civi\Test\Api4\Query
+ * @group headless
+ */
+class SelectQueryMultiJoinTest extends UnitTestCase {
+ public function setUpHeadless() {
+ $this->cleanup(['tablesToTruncate' => ['civicrm_contact', 'civicrm_email']]);
+ $this->loadDataSet('MultiContactMultiEmail');
+ return parent::setUpHeadless();
+ }
+
+ public function testOneToManySelect() {
+ $results = Contact::get()
+ ->addSelect('emails.email')
+ ->execute()
+ ->indexBy('id')
+ ->getArrayCopy();
+
+ $firstContactId = $this->getReference('test_contact_1')['id'];
+ $secondContactId = $this->getReference('test_contact_2')['id'];
+
+ $firstContact = $results[$firstContactId];
+ $secondContact = $results[$secondContactId];
+ $firstContactEmails = array_column($firstContact['emails'], 'email');
+ $secondContactEmails = array_column($secondContact['emails'], 'email');
+
+ $expectedFirstEmails = [
+ 'test_contact_one_home@fakedomain.com',
+ 'test_contact_one_work@fakedomain.com',
+ ];
+ $expectedSecondEmails = [
+ 'test_contact_two_home@fakedomain.com',
+ 'test_contact_two_work@fakedomain.com',
+ ];
+
+ $this->assertEquals($expectedFirstEmails, $firstContactEmails);
+ $this->assertEquals($expectedSecondEmails, $secondContactEmails);
+ }
+
+ public function testManyToOneSelect() {
+ $results = Email::get()
+ ->addSelect('contact.display_name')
+ ->execute()
+ ->indexBy('id')
+ ->getArrayCopy();
+
+ $firstEmail = $this->getReference('test_email_1');
+ $secondEmail = $this->getReference('test_email_2');
+ $thirdEmail = $this->getReference('test_email_3');
+ $fourthEmail = $this->getReference('test_email_4');
+ $firstContactEmailIds = [$firstEmail['id'], $secondEmail['id']];
+ $secondContactEmailIds = [$thirdEmail['id'], $fourthEmail['id']];
+
+ foreach ($results as $id => $email) {
+ $displayName = $email['contact']['display_name'];
+ if (in_array($id, $firstContactEmailIds)) {
+ $this->assertEquals('First Contact', $displayName);
+ }
+ elseif (in_array($id, $secondContactEmailIds)) {
+ $this->assertEquals('Second Contact', $displayName);
+ }
+ }
+ }
+
+}