diff options
Diffstat (limited to 'www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Spec')
3 files changed, 228 insertions, 0 deletions
diff --git a/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Spec/RequestSpecTest.php b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Spec/RequestSpecTest.php new file mode 100644 index 00000000..45aa41b2 --- /dev/null +++ b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Spec/RequestSpecTest.php @@ -0,0 +1,42 @@ +<?php + +namespace Civi\Test\Api4\Spec; + +use Civi\Api4\Service\Spec\FieldSpec; +use Civi\Api4\Service\Spec\RequestSpec; +use Civi\Test\Api4\UnitTestCase; + +/** + * @group headless + */ +class RequestSpecTest extends UnitTestCase { + + public function testRequiredFieldFetching() { + $spec = new RequestSpec('Contact', 'get'); + $requiredField = new FieldSpec('name', 'Contact'); + $requiredField->setRequired(TRUE); + $nonRequiredField = new FieldSpec('age', 'Contact', 'Integer'); + $nonRequiredField->setRequired(FALSE); + $spec->addFieldSpec($requiredField); + $spec->addFieldSpec($nonRequiredField); + + $requiredFields = $spec->getRequiredFields(); + + $this->assertCount(1, $requiredFields); + $this->assertEquals('name', array_shift($requiredFields)->getName()); + } + + public function testGettingFieldNames() { + $spec = new RequestSpec('Contact', 'get'); + $nameField = new FieldSpec('name', 'Contact'); + $ageField = new FieldSpec('age', 'Contact', 'Integer'); + $spec->addFieldSpec($nameField); + $spec->addFieldSpec($ageField); + + $fieldNames = $spec->getFieldNames(); + + $this->assertCount(2, $fieldNames); + $this->assertEquals(['name', 'age'], $fieldNames); + } + +} diff --git a/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Spec/SpecFormatterTest.php b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Spec/SpecFormatterTest.php new file mode 100644 index 00000000..bdb1c459 --- /dev/null +++ b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Spec/SpecFormatterTest.php @@ -0,0 +1,90 @@ +<?php + +namespace Civi\Test\Api4\Spec; + +use Civi\Api4\Service\Spec\CustomFieldSpec; +use Civi\Api4\Service\Spec\FieldSpec; +use Civi\Api4\Service\Spec\RequestSpec; +use Civi\Api4\Service\Spec\SpecFormatter; +use Civi\Test\Api4\UnitTestCase; + +/** + * @group headless + */ +class SpecFormatterTest extends UnitTestCase { + + public function testSpecToArray() { + $spec = new RequestSpec('Contact', 'get'); + $fieldName = 'last_name'; + $field = new FieldSpec($fieldName, 'Contact'); + $spec->addFieldSpec($field); + $arraySpec = SpecFormatter::specToArray($spec->getFields()); + + $this->assertEquals('String', $arraySpec[$fieldName]['data_type']); + } + + /** + * @dataProvider arrayFieldSpecProvider + * + * @param array $fieldData + * @param string $expectedName + * @param string $expectedType + */ + public function testArrayToField($fieldData, $expectedName, $expectedType) { + $field = SpecFormatter::arrayToField($fieldData, 'TestEntity'); + + $this->assertEquals($expectedName, $field->getName()); + $this->assertEquals($expectedType, $field->getDataType()); + } + + public function testCustomFieldWillBeReturned() { + $customGroupId = 1432; + $customFieldId = 3333; + $name = 'MyFancyField'; + + $data = [ + 'custom_group_id' => $customGroupId, + 'custom_group' => ['name' => 'my_group'], + 'id' => $customFieldId, + 'name' => $name, + 'data_type' => 'String', + 'html_type' => 'MultiSelect', + ]; + + /** @var CustomFieldSpec $field */ + $field = SpecFormatter::arrayToField($data, 'TestEntity'); + + $this->assertInstanceOf(CustomFieldSpec::class, $field); + $this->assertEquals('my_group', $field->getCustomGroupName()); + $this->assertEquals($customFieldId, $field->getCustomFieldId()); + $this->assertEquals(\CRM_Core_DAO::SERIALIZE_SEPARATOR_BOOKEND, $field->getSerialize()); + } + + /** + * @return array + */ + public function arrayFieldSpecProvider() { + return [ + [ + [ + 'name' => 'Foo', + 'title' => 'Bar', + 'type' => \CRM_Utils_Type::T_STRING + ], + 'Foo', + 'String' + ], + [ + [ + 'name' => 'MyField', + 'title' => 'Bar', + 'type' => \CRM_Utils_Type::T_STRING, + 'data_type' => 'Boolean' // this should take precedence + ], + 'MyField', + 'Boolean' + ], + ]; + } + +} diff --git a/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Spec/SpecGathererTest.php b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Spec/SpecGathererTest.php new file mode 100644 index 00000000..bf5b92b9 --- /dev/null +++ b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/tests/phpunit/Spec/SpecGathererTest.php @@ -0,0 +1,96 @@ +<?php + +namespace Civi\Test\Api4\Spec; + +use Civi\Api4\Service\Spec\FieldSpec; +use Civi\Api4\Service\Spec\Provider\SpecProviderInterface; +use Civi\Api4\Service\Spec\RequestSpec; +use Civi\Api4\Service\Spec\SpecGatherer; +use Civi\Test\Api4\Traits\OptionCleanupTrait; +use Civi\Test\Api4\UnitTestCase; +use Civi\Api4\CustomField; +use Civi\Api4\CustomGroup; +use Civi\Test\Api4\Traits\TableDropperTrait; +use Prophecy\Argument; + +/** + * @group headless + */ +class SpecGathererTest extends UnitTestCase { + + use TableDropperTrait; + use OptionCleanupTrait; + + public function setUpHeadless() { + $this->dropByPrefix('civicrm_value_favorite'); + $this->cleanup([ + 'tablesToTruncate' => [ + 'civicrm_custom_group', + 'civicrm_custom_field' + ], + ]); + return parent::setUpHeadless(); + } + + public function testBasicFieldsGathering() { + $gatherer = new SpecGatherer(); + $specs = $gatherer->getSpec('Contact', 'get', FALSE); + $contactDAO = _civicrm_api3_get_DAO('Contact'); + $contactFields = $contactDAO::fields(); + $specFieldNames = $specs->getFieldNames(); + $contactFieldNames = array_column($contactFields, 'name'); + + $this->assertEmpty(array_diff_key($contactFieldNames, $specFieldNames)); + } + + public function testWithSpecProvider() { + $gather = new SpecGatherer(); + + $provider = $this->prophesize(SpecProviderInterface::class); + $provider->applies('Contact', 'create')->willReturn(TRUE); + $provider->modifySpec(Argument::any())->will(function ($args) { + /** @var RequestSpec $spec */ + $spec = $args[0]; + $spec->addFieldSpec(new FieldSpec('foo', 'Contact')); + }); + $gather->addSpecProvider($provider->reveal()); + + $spec = $gather->getSpec('Contact', 'create', FALSE); + $fieldNames = $spec->getFieldNames(); + + $this->assertContains('foo', $fieldNames); + } + + public function testPseudoConstantOptionsWillBeAdded() { + $customGroupId = CustomGroup::create() + ->setCheckPermissions(FALSE) + ->addValue('name', 'FavoriteThings') + ->addValue('extends', 'Contact') + ->execute() + ->first()['id']; + + $options = ['r' => 'Red', 'g' => 'Green', 'p' => 'Pink']; + + CustomField::create() + ->setCheckPermissions(FALSE) + ->addValue('label', 'FavColor') + ->addValue('custom_group_id', $customGroupId) + ->addValue('options', $options) + ->addValue('html_type', 'Select') + ->addValue('data_type', 'String') + ->execute(); + + $gatherer = new SpecGatherer(); + $spec = $gatherer->getSpec('Contact', 'get', TRUE); + + $regularField = $spec->getFieldByName('contact_type'); + $this->assertNotEmpty($regularField->getOptions()); + $this->assertContains('Individual', $regularField->getOptions()); + + $customField = $spec->getFieldByName('FavoriteThings.FavColor'); + $this->assertNotEmpty($customField->getOptions()); + $this->assertContains('Green', $customField->getOptions()); + $this->assertEquals('Pink', $customField->getOptions()['p']); + } + +} |