summaryrefslogtreecommitdiff
path: root/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Service
diff options
context:
space:
mode:
Diffstat (limited to 'www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Service')
-rw-r--r--www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Service/Schema/SchemaMapRealTableTest.php22
-rw-r--r--www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Service/Schema/SchemaMapperTest.php90
-rw-r--r--www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Service/TestCreationParameterProvider.php148
3 files changed, 260 insertions, 0 deletions
diff --git a/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Service/Schema/SchemaMapRealTableTest.php b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Service/Schema/SchemaMapRealTableTest.php
new file mode 100644
index 00000000..183d34dc
--- /dev/null
+++ b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Service/Schema/SchemaMapRealTableTest.php
@@ -0,0 +1,22 @@
+<?php
+
+namespace Civi\Test\Api4\Service\Schema;
+
+use Civi\Test\Api4\UnitTestCase;
+
+/**
+ * @group headless
+ */
+class SchemaMapRealTableTest extends UnitTestCase {
+ public function testAutoloadWillPopulateTablesByDefault() {
+ $map = \Civi::container()->get('schema_map');
+ $this->assertNotEmpty($map->getTables());
+ }
+
+ public function testSimplePathWillExist() {
+ $map = \Civi::container()->get('schema_map');
+ $path = $map->getPath('civicrm_contact', 'emails');
+ $this->assertCount(1, $path);
+ }
+
+}
diff --git a/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Service/Schema/SchemaMapperTest.php b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Service/Schema/SchemaMapperTest.php
new file mode 100644
index 00000000..04952f7b
--- /dev/null
+++ b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Service/Schema/SchemaMapperTest.php
@@ -0,0 +1,90 @@
+<?php
+
+namespace Civi\Test\Api4\Service\Schema;
+
+use Civi\Api4\Service\Schema\Joinable\Joinable;
+use Civi\Api4\Service\Schema\SchemaMap;
+use Civi\Api4\Service\Schema\Table;
+use Civi\Test\Api4\UnitTestCase;
+
+/**
+ * @group headless
+ */
+class SchemaMapperTest extends UnitTestCase {
+
+ public function testWillHaveNoPathWithNoTables() {
+ $map = new SchemaMap();
+ $this->assertEmpty($map->getPath('foo', 'bar'));
+ }
+
+ public function testWillHavePathWithSingleJump() {
+ $phoneTable = new Table('civicrm_phone');
+ $locationTable = new Table('civicrm_location_type');
+ $link = new Joinable('civicrm_location_type', 'id', 'location');
+ $phoneTable->addTableLink('location_type_id', $link);
+
+ $map = new SchemaMap();
+ $map->addTables([$phoneTable, $locationTable]);
+
+ $this->assertNotEmpty($map->getPath('civicrm_phone', 'location'));
+ }
+
+ public function testWillHavePathWithDoubleJump() {
+ $activity = new Table('activity');
+ $activityContact = new Table('activity_contact');
+ $middleLink = new Joinable('activity_contact', 'activity_id');
+ $contactLink = new Joinable('contact', 'id');
+ $activity->addTableLink('id', $middleLink);
+ $activityContact->addTableLink('contact_id', $contactLink);
+
+ $map = new SchemaMap();
+ $map->addTables([$activity, $activityContact]);
+
+ $this->assertNotEmpty($map->getPath('activity', 'contact'));
+ }
+
+ public function testPathWithTripleJoin() {
+ $first = new Table('first');
+ $second = new Table('second');
+ $third = new Table('third');
+ $first->addTableLink('id', new Joinable('second', 'id'));
+ $second->addTableLink('id', new Joinable('third', 'id'));
+ $third->addTableLink('id', new Joinable('fourth', 'id'));
+
+ $map = new SchemaMap();
+ $map->addTables([$first, $second, $third]);
+
+ $this->assertNotEmpty($map->getPath('first', 'fourth'));
+ }
+
+ public function testCircularReferenceWillNotBreakIt() {
+ $contactTable = new Table('contact');
+ $carTable = new Table('car');
+ $carLink = new Joinable('car', 'id');
+ $ownerLink = new Joinable('contact', 'id');
+ $contactTable->addTableLink('car_id', $carLink);
+ $carTable->addTableLink('owner_id', $ownerLink);
+
+ $map = new SchemaMap();
+ $map->addTables([$contactTable, $carTable]);
+
+ $this->assertEmpty($map->getPath('contact', 'foo'));
+ }
+
+ public function testCannotGoOverJoinLimit() {
+ $first = new Table('first');
+ $second = new Table('second');
+ $third = new Table('third');
+ $fourth = new Table('fourth');
+ $first->addTableLink('id', new Joinable('second', 'id'));
+ $second->addTableLink('id', new Joinable('third', 'id'));
+ $third->addTableLink('id', new Joinable('fourth', 'id'));
+ $fourth->addTableLink('id', new Joinable('fifth', 'id'));
+
+ $map = new SchemaMap();
+ $map->addTables([$first, $second, $third, $fourth]);
+
+ $this->assertEmpty($map->getPath('first', 'fifth'));
+ }
+
+}
diff --git a/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Service/TestCreationParameterProvider.php b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Service/TestCreationParameterProvider.php
new file mode 100644
index 00000000..ef10cea9
--- /dev/null
+++ b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Service/TestCreationParameterProvider.php
@@ -0,0 +1,148 @@
+<?php
+
+namespace Civi\Test\Api4\Service;
+
+use Civi\Api4\Service\Spec\FieldSpec;
+use Civi\Api4\Service\Spec\SpecGatherer;
+use \CRM_Utils_String as StringHelper;
+
+class TestCreationParameterProvider {
+
+ /**
+ * @var SpecGatherer
+ */
+ protected $gatherer;
+
+ /**
+ * @param SpecGatherer $gatherer
+ */
+ public function __construct(SpecGatherer $gatherer) {
+ $this->gatherer = $gatherer;
+ }
+
+ /**
+ * @param $entity
+ *
+ * @return array
+ */
+ public function getRequired($entity) {
+ $createSpec = $this->gatherer->getSpec($entity, 'create', FALSE);
+ $requiredFields = array_merge($createSpec->getRequiredFields(), $createSpec->getConditionalRequiredFields());
+
+ if ($entity === 'Contact') {
+ $requiredFields[] = $createSpec->getFieldByName('first_name');
+ $requiredFields[] = $createSpec->getFieldByName('last_name');
+ }
+
+ $requiredParams = [];
+ foreach ($requiredFields as $requiredField) {
+ $value = $this->getRequiredValue($requiredField);
+ $requiredParams[$requiredField->getName()] = $value;
+ }
+
+ unset($requiredParams['id']);
+
+ return $requiredParams;
+ }
+
+ /**
+ * Attempt to get a value using field option, defaults, FKEntity, or a random
+ * value based on the data type.
+ *
+ * @param FieldSpec $field
+ *
+ * @return mixed
+ * @throws \Exception
+ */
+ private function getRequiredValue(FieldSpec $field) {
+
+ if ($field->getOptions()) {
+ return $this->getOption($field);
+ }
+ elseif ($field->getDefaultValue()) {
+ return $field->getDefaultValue();
+ }
+ elseif ($field->getFkEntity()) {
+ return $this->getFkID($field);
+ }
+
+ $randomValue = $this->getRandomValue($field->getDataType());
+
+ if ($randomValue) {
+ return $randomValue;
+ }
+
+ throw new \Exception('Could not provide default value');
+ }
+
+ /**
+ * @param FieldSpec $field
+ *
+ * @return mixed
+ */
+ private function getOption(FieldSpec $field) {
+ $options = $field->getOptions();
+ $useKeyNames = ['data_type', 'html_type'];
+ $shouldUseKey = in_array($field->getName(), $useKeyNames);
+ $isIdField = substr($field->getName(), -3) === '_id';
+
+ if ($isIdField || $shouldUseKey) {
+ return array_rand($options); // return key (ID)
+ }
+ else {
+ return $options[array_rand($options)];
+ }
+ }
+
+ /**
+ * @param FieldSpec $field
+ *
+ * @return mixed
+ * @throws \Exception
+ */
+ private function getFkID(FieldSpec $field) {
+ $fkEntity = $field->getFkEntity();
+ $params = ['checkPermissions' => FALSE];
+ // Be predictable about what type of contact we select
+ if ($fkEntity === 'Contact') {
+ $params['where'] = [['contact_type', '=', 'Individual']];
+ }
+ $entityList = civicrm_api4($fkEntity, 'get', $params);
+ if ($entityList->count() < 1) {
+ $msg = sprintf('At least one %s is required in test', $fkEntity);
+ throw new \Exception($msg);
+ }
+
+ return $entityList->last()['id'];
+ }
+
+ /**
+ * @param $dataType
+ *
+ * @return int|null|string
+ */
+ private function getRandomValue($dataType) {
+ switch ($dataType) {
+ case 'Boolean':
+ return TRUE;
+
+ case 'Integer':
+ return rand(1, 2000);
+
+ case 'String':
+ return StringHelper::createRandom(10, implode('', range('a', 'z')));
+
+ case 'Text':
+ return StringHelper::createRandom(100, implode('', range('a', 'z')));
+
+ case 'Money':
+ return sprintf('%d.%2d', rand(0, 2000), rand(1, 99));
+
+ case 'Date':
+ return '20100102';
+ }
+
+ return NULL;
+ }
+
+}