From 531982df3c78f259914bf035de442c8edc41063e Mon Sep 17 00:00:00 2001 From: Tom Zeller Date: Fri, 8 Mar 2019 11:17:50 -0600 Subject: [PATCH 1/6] Initial PHPUnit configuration file (CO-1716) --- app/phpunit.xml.dist | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 app/phpunit.xml.dist diff --git a/app/phpunit.xml.dist b/app/phpunit.xml.dist new file mode 100644 index 000000000..21c97db96 --- /dev/null +++ b/app/phpunit.xml.dist @@ -0,0 +1,41 @@ + + + + + + + + + + tests/TestCase/ + + tests/TestCase/Controller/PagesControllerTest.php + + + + + + + + + + + + + + + + src/ + plugins/*/src/ + + src/Console/Installer.php + + + + From b40c35df5e66f38fb70c39d95fdff8f2ffbd0262 Mon Sep 17 00:00:00 2001 From: Tom Zeller Date: Fri, 8 Mar 2019 11:43:18 -0600 Subject: [PATCH 2/6] Fixtures for table unit tests (CO-1716) --- app/tests/Fixture/ApiUsersFixture.php | 34 ++++++++++++++++++++ app/tests/Fixture/AttributeGroupsFixture.php | 34 ++++++++++++++++++++ app/tests/Fixture/AttributesFixture.php | 34 ++++++++++++++++++++ app/tests/Fixture/MatchgridsFixture.php | 34 ++++++++++++++++++++ app/tests/Fixture/MetaFixture.php | 34 ++++++++++++++++++++ app/tests/Fixture/PermissionsFixture.php | 34 ++++++++++++++++++++ app/tests/Fixture/RuleAttributesFixture.php | 34 ++++++++++++++++++++ app/tests/Fixture/RulesFixture.php | 34 ++++++++++++++++++++ app/tests/Fixture/SystemsOfRecordFixture.php | 34 ++++++++++++++++++++ app/tests/Fixture/empty | 0 10 files changed, 306 insertions(+) create mode 100644 app/tests/Fixture/ApiUsersFixture.php create mode 100644 app/tests/Fixture/AttributeGroupsFixture.php create mode 100644 app/tests/Fixture/AttributesFixture.php create mode 100644 app/tests/Fixture/MatchgridsFixture.php create mode 100644 app/tests/Fixture/MetaFixture.php create mode 100644 app/tests/Fixture/PermissionsFixture.php create mode 100644 app/tests/Fixture/RuleAttributesFixture.php create mode 100644 app/tests/Fixture/RulesFixture.php create mode 100644 app/tests/Fixture/SystemsOfRecordFixture.php delete mode 100644 app/tests/Fixture/empty diff --git a/app/tests/Fixture/ApiUsersFixture.php b/app/tests/Fixture/ApiUsersFixture.php new file mode 100644 index 000000000..97cd3df57 --- /dev/null +++ b/app/tests/Fixture/ApiUsersFixture.php @@ -0,0 +1,34 @@ + 'api_users']; +} \ No newline at end of file diff --git a/app/tests/Fixture/AttributeGroupsFixture.php b/app/tests/Fixture/AttributeGroupsFixture.php new file mode 100644 index 000000000..07e3f5e18 --- /dev/null +++ b/app/tests/Fixture/AttributeGroupsFixture.php @@ -0,0 +1,34 @@ + 'attribute_groups']; +} \ No newline at end of file diff --git a/app/tests/Fixture/AttributesFixture.php b/app/tests/Fixture/AttributesFixture.php new file mode 100644 index 000000000..819f8f56f --- /dev/null +++ b/app/tests/Fixture/AttributesFixture.php @@ -0,0 +1,34 @@ + 'attributes']; +} \ No newline at end of file diff --git a/app/tests/Fixture/MatchgridsFixture.php b/app/tests/Fixture/MatchgridsFixture.php new file mode 100644 index 000000000..9f4ce55bc --- /dev/null +++ b/app/tests/Fixture/MatchgridsFixture.php @@ -0,0 +1,34 @@ + 'matchgrids', 'records' => true]; +} \ No newline at end of file diff --git a/app/tests/Fixture/MetaFixture.php b/app/tests/Fixture/MetaFixture.php new file mode 100644 index 000000000..5f2a848ff --- /dev/null +++ b/app/tests/Fixture/MetaFixture.php @@ -0,0 +1,34 @@ + 'meta']; +} \ No newline at end of file diff --git a/app/tests/Fixture/PermissionsFixture.php b/app/tests/Fixture/PermissionsFixture.php new file mode 100644 index 000000000..456d2de4c --- /dev/null +++ b/app/tests/Fixture/PermissionsFixture.php @@ -0,0 +1,34 @@ + 'permissions']; +} \ No newline at end of file diff --git a/app/tests/Fixture/RuleAttributesFixture.php b/app/tests/Fixture/RuleAttributesFixture.php new file mode 100644 index 000000000..c05f20b2a --- /dev/null +++ b/app/tests/Fixture/RuleAttributesFixture.php @@ -0,0 +1,34 @@ + 'rule_attributes']; +} \ No newline at end of file diff --git a/app/tests/Fixture/RulesFixture.php b/app/tests/Fixture/RulesFixture.php new file mode 100644 index 000000000..9838dd5d6 --- /dev/null +++ b/app/tests/Fixture/RulesFixture.php @@ -0,0 +1,34 @@ + 'rules']; +} \ No newline at end of file diff --git a/app/tests/Fixture/SystemsOfRecordFixture.php b/app/tests/Fixture/SystemsOfRecordFixture.php new file mode 100644 index 000000000..da14d802c --- /dev/null +++ b/app/tests/Fixture/SystemsOfRecordFixture.php @@ -0,0 +1,34 @@ + 'systems_of_record']; +} \ No newline at end of file diff --git a/app/tests/Fixture/empty b/app/tests/Fixture/empty deleted file mode 100644 index e69de29bb..000000000 From 876449632e7d8194e18704dc373950dc99ed8f63 Mon Sep 17 00:00:00 2001 From: Tom Zeller Date: Fri, 8 Mar 2019 11:43:57 -0600 Subject: [PATCH 3/6] Table unit tests (CO-1716) --- .../Model/Table/AbstractTableTestCase.php | 327 +++++++++++++++++ .../Model/Table/ApiUsersTableTest.php | 161 +++++++++ .../Model/Table/AttributeGroupsTableTest.php | 86 +++++ .../Model/Table/AttributesTableTest.php | 204 +++++++++++ .../Model/Table/MatchgridsTableTest.php | 333 ++++++++++++++++++ .../TestCase/Model/Table/MetaTableTest.php | 58 +++ .../Model/Table/PermissionsTableTest.php | 111 ++++++ .../Model/Table/RuleAttributesTableTest.php | 96 +++++ .../TestCase/Model/Table/RulesTableTest.php | 144 ++++++++ .../Model/Table/SystemsOfRecordTableTest.php | 111 ++++++ 10 files changed, 1631 insertions(+) create mode 100644 app/tests/TestCase/Model/Table/AbstractTableTestCase.php create mode 100644 app/tests/TestCase/Model/Table/ApiUsersTableTest.php create mode 100644 app/tests/TestCase/Model/Table/AttributeGroupsTableTest.php create mode 100644 app/tests/TestCase/Model/Table/AttributesTableTest.php create mode 100644 app/tests/TestCase/Model/Table/MatchgridsTableTest.php create mode 100644 app/tests/TestCase/Model/Table/MetaTableTest.php create mode 100644 app/tests/TestCase/Model/Table/PermissionsTableTest.php create mode 100644 app/tests/TestCase/Model/Table/RuleAttributesTableTest.php create mode 100644 app/tests/TestCase/Model/Table/RulesTableTest.php create mode 100644 app/tests/TestCase/Model/Table/SystemsOfRecordTableTest.php diff --git a/app/tests/TestCase/Model/Table/AbstractTableTestCase.php b/app/tests/TestCase/Model/Table/AbstractTableTestCase.php new file mode 100644 index 000000000..0708e27d7 --- /dev/null +++ b/app/tests/TestCase/Model/Table/AbstractTableTestCase.php @@ -0,0 +1,327 @@ +getTable()->getAssociation($association); + $this->assertInstanceOf($classname, $association->getTarget()); + $this->assertEquals(Association::MANY_TO_ONE, $association->type()); + } + + /** + * Assert that the table being tested has many associations to another table. + * + * @param string $classname Class name of table with many associations to. + * @param string $association Association name. + */ + public function assertHasMany(string $classname, string $association) { + $association = $this->getTable()->getAssociation($association); + $this->assertInstanceOf($classname, $association->getTarget()); + $this->assertEquals(Association::ONE_TO_MANY, $association->type()); + } + + /** + * Assert that a field must be a boolean. + * + * @param string $field Name of field. + */ + public function assertFieldBoolean(string $field) { + $data = [ + $field => '0', + ]; + $this->assertValidationSuccess($data); + + $data = [ + $field => '1', + ]; + $this->assertValidationSuccess($data); + + $data = [ + $field => 0, + ]; + $this->assertValidationSuccess($data); + + $data = [ + $field => 1, + ]; + $this->assertValidationSuccess($data); + + $data = [ + $field => 'true', + ]; + $this->assertValidationFailure($data); + + $data = [ + $field => 'false', + ]; + $this->assertValidationFailure($data); + } + + /** + * Assert that a field must be an integer. + * + * @param string $field Name of field. + */ + public function assertFieldInteger(string $field) { + $data = [ + $field => 'not-an-integer', + ]; + $this->assertValidationFailure($data); + + $data = [ + $field => 0.5, + ]; + $this->assertValidationFailure($data); + + $data = [ + $field => '1', + ]; + $this->assertValidationSuccess($data); + + $data = [ + $field => 1, + ]; + $this->assertValidationSuccess($data); + } + + /** + * Assert that a field has an allowed value. + * + * @param string $field Name of field. + * @param array $list List of allowed values. + */ + public function assertFieldInList(string $field, array $list) { + $this->assertTrue(is_array($list)); + + foreach ($list as $value) { + $data = [ + $field => $value + ]; + $this->assertValidationSuccess($data); + } + + $data = [ + $field => 'not-in-list' + ]; + $this->assertValidationFailure($data); + } + + /** + * Assert that a field is allowed to be empty. + * + * @param string $field Name of field. + */ + public function assertFieldAllowEmpty(string $field) { + $data = [ + $field => '', + ]; + $this->assertValidationSuccess($data); + } + + /** + * Assert that a field is not allowed to be empty. + * + * @param string $field Name of field. + */ + public function assertFieldNotEmpty(string $field) { + $data = [ + $field => '', + ]; + $this->assertValidationFailure($data); + } + + /** + * Assert that a field has a maximum length. + * + * @param string $field Name of field. + * @param int $maxLength Maximum length of field. + */ + public function assertFieldMaxLength(string $field, int $maxLength) { + $data = [ + $field => str_repeat('x', $maxLength), + ]; + $this->assertValidationSuccess($data); + + $data = [ + $field => str_repeat('x', $maxLength + 1), + ]; + $this->assertValidationFailure($data); + } + + /** + * Assert that a numeric field is within a range. + * + * @param string $field Name of field. + * @param int $lower Lower value of range. + * @param int|null $upper Upper value of range. + */ + public function assertFieldRange(string $field, int $lower, int $upper = null) { + $data = [ + $field => $lower - 1, + ]; + $this->assertValidationFailure($data); + + foreach (range($lower, $upper) as $i) { + $data = [ + $field => $i, + ]; + $this->assertValidationSuccess($data); + } + + $data = [ + $field => $upper + 1, + ]; + $this->assertValidationFailure($data); + } + + /** + * Assert that a numeric field is within a range whose upper limit is large. + * + * The upper limit is 64 * 1024 * 1024, which fits the PHP default memory_limit of 128M. + * + * @param string $field Name of field. + * @param int $lower Lower value of range. + */ + public function assertFieldRangeUnbounded(string $field, int $lower) { + $data = [ + $field => $lower - 1, + ]; + $this->assertValidationFailure($data); + + $data = [ + $field => $lower, + ]; + $this->assertValidationSuccess($data); + + $data = [ + $field => 64 * 1024 * 1024, + ]; + $this->assertValidationSuccess($data); + } + + /** + * Assert that a field has values which match a regex. + * + * @param string $field Name of field. + * @param string $regex The pattern of allowed values. + */ + public function assertFieldRegex(string $field, string $regex) { + for ($i = 0; $i < 256; $i++) { + $chr = chr($i); + $data = [ + $field => $chr, + ]; + if (preg_match($regex, $chr)) { + $this->assertValidationSuccess($data); + } else { + $this->assertValidationFailure($data); + } + } + } + + /** + * Assert that validation fails. + * + * @param array $data + */ + protected function assertValidationFailure(array $data) { + if ($this->debug) { + Log::debug('assertValidationFailure $data ' . var_export($data, true)); + } + $table = $this->getTable()->newEntity($data); + if ($this->debug) { + Log::debug('assertValidationFailure error ' . var_export($table->getErrors(), true)); + } + $this->assertNotEmpty($table->getErrors()); + } + + /** + * Assert that validation is successful. + * + * @param array $data + */ + protected function assertValidationSuccess(array $data) { + if ($this->debug) { + Log::debug('assertValidationSuccess $data ' . var_export($data, true)); + } + $table = $this->getTable()->newEntity($data); + if ($this->debug) { + Log::debug('assertValidationSuccess error ' . var_export($table->getErrors(), true)); + } + $this->assertEmpty($table->getErrors()); + } + + /** + * Create test matchgrid. + * + * @return \Cake\Datasource\EntityInterface|false + */ + public function createTestMatchgrid() { + $data = [ + 'table_name' => 'testmatchgrid', + 'description' => 'Test Matchgrid', + 'status' => 'A', + 'referenceid_method' => 'S', + 'referenceid_start' => '1001', + 'referenceid_prefix' => 'prefix' + ]; + $matchgrids = TableRegistry::getTableLocator()->get('Matchgrids'); + $matchgrid = $matchgrids->newEntity($data); + return $matchgrids->save($matchgrid); + } +} \ No newline at end of file diff --git a/app/tests/TestCase/Model/Table/ApiUsersTableTest.php b/app/tests/TestCase/Model/Table/ApiUsersTableTest.php new file mode 100644 index 000000000..a79a1e813 --- /dev/null +++ b/app/tests/TestCase/Model/Table/ApiUsersTableTest.php @@ -0,0 +1,161 @@ +get('ApiUsers'); + } + + /** + * Test table initialization. + * + * @return void + */ + public function testInitialize() { + $table = $this->getTable(); + $this->assertInstanceOf('App\Model\Table\ApiUsersTable', $table); + + $this->assertTrue($table->hasBehavior('Timestamp')); + + $this->assertBelongsTo('App\Model\Table\MatchgridsTable', 'Matchgrids'); + $this->assertBelongsTo('App\Model\Table\SystemsOfRecordTable', 'SystemsOfRecord'); + $this->assertEquals('system_of_record_id', $table->getAssociation('SystemsOfRecord')->getForeignKey()); + $this->assertEquals('system_of_record', $table->getAssociation('SystemsOfRecord')->getProperty()); + + $this->assertEquals('username', $table->getDisplayField()); + + $this->assertEquals('matchgrid_id', $table->getPrimaryLink()); + $this->assertTrue($table->requiresMatchgrid()); + $this->assertTrue($table->allowEmptyMatchgrid()); + $this->assertTrue($table->allowEmptyPrimaryLink()); + + $expected = ['systemsOfRecord' => [ + 'type' => 'select', + 'model' => 'SystemsOfRecord', + 'find' => 'filterPrimaryLink' + ]]; + $this->assertEquals($expected, $table->getAutoViewVars()); + } + + /** + * Test username field validation. + * + * @return void + */ + public function testValidationUsername() { + $this->assertFieldMaxLength('username', 128); + $this->assertFieldNotEmpty('username'); + } + + /** + * Test password field validation. + * + * @return void + */ + public function testValidationPassword() { + $this->assertFieldMaxLength('password', 80); + $this->assertFieldNotEmpty('password'); + } + + /** + * Test matchgrid_id field validation. + * + * @return void + */ + public function testValidationMatchgridId() { + $this->assertFieldInteger('matchgrid_id'); + $this->assertFieldAllowEmpty('matchgrid_id'); + } + + /** + * Test system_of_record_id field validation. + * + * @return void + */ + public function testValidationSystemOfRecordId() { + $this->assertFieldInteger('system_of_record_id'); + $this->assertFieldAllowEmpty('system_of_record_id'); + } + + /** + * Test username rules validation for Matchgrid API users. + * + * @return void + */ + public function testRulesCheckUsernameMatchgridAPI() { + $this->createTestMatchgrid(); + + // Should not be able to create a Matchgrid API user whose name is not of the 'matchgrid name'.'username' format + $entityNoDots = new ApiUser([ + 'username' => 'MatchgridApiUserNoDot', + 'matchgrid_id' => '1', + ]); + $this->assertFalse($this->getTable()->save($entityNoDots)); + + // Should be able to create a Matchgrid API user whose name is of the 'matchgrid name'.'username' format + $entityWithDots = new ApiUser([ + 'username' => 'testmatchgrid.withDot', + 'matchgrid_id' => '1', + ]); + $this->assertInstanceOf('App\Model\Entity\ApiUser', $this->getTable()->save($entityWithDots)); + } + + /** + * Test username rules validation for Platform API users. + * + * @return void + */ + public function testRulesCheckUsernamePlatformAPI() { + // Should be able to create a Platform API user whose name does not contain a dot. + $entityNoDots = new ApiUser([ + 'username' => 'platformApiUserNoDot' + ]); + $this->assertInstanceOf('App\Model\Entity\ApiUser', $this->getTable()->save($entityNoDots)); + + // Should not be able to create a Platform API user whose name contains a dot. + $entityWithDots = new ApiUser([ + 'username' => 'platformApiUser.withDot' + ]); + $this->assertFalse($this->getTable()->save($entityWithDots)); + } + + // TODO findAuthorization ? +} diff --git a/app/tests/TestCase/Model/Table/AttributeGroupsTableTest.php b/app/tests/TestCase/Model/Table/AttributeGroupsTableTest.php new file mode 100644 index 000000000..9c7566ec5 --- /dev/null +++ b/app/tests/TestCase/Model/Table/AttributeGroupsTableTest.php @@ -0,0 +1,86 @@ +get('AttributeGroups'); + } + + /** + * Test table initialization. + * + * @return void + */ + public function testInitialize() { + $table = $this->getTable(); + $this->assertInstanceOf('App\Model\Table\AttributeGroupsTable', $table); + + $this->assertTrue($table->hasBehavior('Timestamp')); + + $this->assertBelongsTo('App\Model\Table\MatchgridsTable', 'Matchgrids'); + $this->assertHasMany('App\Model\Table\AttributesTable', 'Attributes'); + + $this->assertEquals('name', $table->getDisplayField()); + + $this->assertEquals('matchgrid_id', $table->getPrimaryLink()); + $this->assertTrue($table->requiresMatchgrid()); + } + + /** + * Test name field validation. + * + * @return void + */ + public function testValidationName() { + $this->assertFieldMaxLength('name', 32); + $this->assertFieldNotEmpty('name'); + } + + /** + * Test matchgrid_id field validation. + * + * @return void + */ + public function testValidationMatchgridId() { + $this->assertFieldInteger('matchgrid_id'); + $this->assertFieldNotEmpty('matchgrid_id'); + } + +} diff --git a/app/tests/TestCase/Model/Table/AttributesTableTest.php b/app/tests/TestCase/Model/Table/AttributesTableTest.php new file mode 100644 index 000000000..551dfb5c7 --- /dev/null +++ b/app/tests/TestCase/Model/Table/AttributesTableTest.php @@ -0,0 +1,204 @@ +get('Attributes'); + } + + /** + * Test table initialization. + * + * @return void + */ + public function testInitialize() { + $table = $this->getTable(); + $this->assertInstanceOf('App\Model\Table\AttributesTable', $table); + + $this->assertTrue($table->hasBehavior('Timestamp')); + + $this->assertBelongsTo('App\Model\Table\AttributeGroupsTable', 'AttributeGroups'); + $this->assertBelongsTo('App\Model\Table\MatchgridsTable', 'Matchgrids'); + $this->assertHasMany('App\Model\Table\RuleAttributesTable', 'RuleAttributes'); + $this->assertTrue($table->getAssociation('RuleAttributes')->getDependent()); + + $this->assertEquals('name', $table->getDisplayField()); + + $this->assertEquals('matchgrid_id', $table->getPrimaryLink()); + $this->assertTrue($table->requiresMatchgrid()); + + $expected = ['attributeGroups' => [ + 'type' => 'select', + 'model' => 'AttributeGroups', + 'find' => 'filterPrimaryLink' + ]]; + $this->assertEquals($expected, $table->getAutoViewVars()); + } + + /** + * Test name field validation. + * + * @return void + */ + public function testValidationName() { + $this->assertFieldMaxLength('name', 128); + $this->assertFieldNotEmpty('name'); // TODO username or name ? + } + + /** + * Test matchgrid_id field validation. + * + * @return void + */ + public function testValidationMatchgridId() { + $this->assertFieldInteger('matchgrid_id'); + $this->assertFieldNotEmpty('matchgrid_id'); + } + + /** + * Test description field validation. + * + * @return void + */ + public function testValidationDescription() { + $this->assertFieldMaxLength('description', 128); + $this->assertFieldAllowEmpty('description'); + } + + /** + * Test api_name field validation. + * + * @return void + */ + public function testValidationApiName() { + $this->assertFieldMaxLength('api_name', 128); + $this->assertFieldNotEmpty('api_name'); + } + + /** + * Test attribute_group_id field validation. + * + * @return void + */ + public function testValidationAttributeGroupId() { + $this->assertFieldInteger('attribute_group_id'); + $this->assertFieldAllowEmpty('attribute_group_id'); + } + + /** + * Test alphanumeric field validation. + * + * @return void + */ + public function testValidationAlphanumeric() { + $this->assertFieldBoolean('alphanumeric'); + $this->assertFieldNotEmpty('alphanumeric'); + } + + /** + * Test case_sensitive field validation. + * + * @return void + */ + public function testValidationCaseSensitive() { + $this->assertFieldBoolean('case_sensitive'); + $this->assertFieldNotEmpty('case_sensitive'); + } + + /** + * Test invalidates field validation. + * + * @return void + */ + public function testValidationInvalidates() { + $this->assertFieldBoolean('invalidates'); + $this->assertFieldNotEmpty('invalidates'); + } + + /** + * Test null_equivalents field validation. + * + * @return void + */ + public function testValidationNullEquivalents() { + $this->assertFieldBoolean('null_equivalents'); + $this->assertFieldNotEmpty('null_equivalents'); + } + + /** + * Test required field validation. + * + * @return void + */ + public function testValidationRequired() { + $this->assertFieldBoolean('required'); + $this->assertFieldNotEmpty('required'); + } + + /** + * Test search_distance field validation. + * + * @return void + */ + public function testValidationSearchDistance() { + $this->assertFieldRange('search_distance', 1, 9); + $this->assertFieldAllowEmpty('search_distance'); + } + + /** + * Test search_exact field validation. + * + * @return void + */ + public function testValidationSearchExact() { + $this->assertFieldBoolean('search_exact'); + $this->assertFieldNotEmpty('search_exact'); + } + + /** + * Test search_substr_from field validation. + * + * @return void + */ + public function testValidationSearchSubstrFrom() { + $this->assertFieldRange('search_substr_from', 0, 128); + $this->assertFieldAllowEmpty('search_substr_from'); + } +} diff --git a/app/tests/TestCase/Model/Table/MatchgridsTableTest.php b/app/tests/TestCase/Model/Table/MatchgridsTableTest.php new file mode 100644 index 000000000..9eac8edff --- /dev/null +++ b/app/tests/TestCase/Model/Table/MatchgridsTableTest.php @@ -0,0 +1,333 @@ +get('Matchgrids'); + } + + /** + * Test table initialization. + * + * @return void + */ + public function testInitialize() { + $table = $this->getTable(); + + $this->assertInstanceOf('App\Model\Table\MatchgridsTable', $table); + $this->assertTrue($table->hasBehavior('Timestamp')); + + $this->assertHasMany('App\Model\Table\ApiUsersTable', 'ApiUsers'); + $this->assertTrue($table->getAssociation('ApiUsers')->getDependent()); + + $this->assertHasMany('App\Model\Table\AttributesTable', 'Attributes'); + $this->assertTrue($table->getAssociation('Attributes')->getDependent()); + $this->assertTrue($table->getAssociation('Attributes')->getCascadeCallbacks()); + + $this->assertHasMany('App\Model\Table\AttributeGroupsTable', 'AttributeGroups'); + $this->assertTrue($table->getAssociation('AttributeGroups')->getDependent()); + + $this->assertHasMany('App\Model\Table\PermissionsTable', 'Permissions'); + $this->assertTrue($table->getAssociation('Permissions')->getDependent()); + + $this->assertHasMany('App\Model\Table\RulesTable', 'Rules'); + $this->assertTrue($table->getAssociation('Rules')->getDependent()); + $this->assertTrue($table->getAssociation('Rules')->getCascadeCallbacks()); + + $this->assertHasMany('App\Model\Table\SystemsOfRecordTable', 'SystemsOfRecord'); + $this->assertTrue($table->getAssociation('SystemsOfRecord')->getDependent()); + + $this->assertHasMany('App\Model\Table\RulesTable', 'CanonicalRules'); + $this->assertEquals(['confidence_mode' => ConfidenceModeEnum::Canonical], $table->getAssociation('CanonicalRules')->getConditions()); + + $this->assertHasMany('App\Model\Table\RulesTable', 'PotentialRules'); + $this->assertEquals(['confidence_mode' => ConfidenceModeEnum::Potential], $table->getAssociation('PotentialRules')->getConditions()); + + $this->assertEquals('table_name', $table->getDisplayField()); + $expectedAutoViewVars = [ + 'referenceidMethods' => [ + 'type' => 'enum', + 'class' => 'ReferenceIdEnum' + ], + 'statuses' => [ + 'type' => 'enum', + 'class' => 'StatusEnum' + ] + ]; + $this->assertEquals($expectedAutoViewVars, $table->getAutoViewVars()); + } + + /** + * Test matchgrid building. + * + * @return void + */ + public function testBuild() { + // Create test matchgrid table + $this->createTestMatchgrid(); + + // Build + $this->getTable()->build(1); + + // Connect to test database + $db = ConnectionManager::get('test'); + + // Get schema of test matchgrid table + $schema = $db->getSchemaCollection()->describe('mg_testmatchgrid'); + + // Test columns + $expected = ['id', 'sor', 'sorid', 'referenceid', 'request_time', 'resolution_time']; + $this->assertEquals($expected, $schema->columns()); + + $result = $schema->getColumn('id'); + $expected = [ + 'type' => 'integer', + 'length' => 10, + 'autoIncrement' => true, + 'default' => NULL, + 'null' => false, + 'comment' => NULL, + 'precision' => NULL, + 'unsigned' => NULL, + ]; + $this->assertEquals($expected, $result); + + $result = $schema->getColumn('sor'); + $expected = [ + 'type' => 'string', + 'length' => 64, + 'default' => NULL, + 'null' => true, + 'collate' => NULL, + 'comment' => NULL, + 'precision' => NULL, + 'fixed' => NULL, + ]; + $this->assertEquals($expected, $result); + + $result = $schema->getColumn('sorid'); + $this->assertEquals($expected, $result); + + $result = $schema->getColumn('referenceid'); + $this->assertEquals($expected, $result); + + $result = $schema->getColumn('request_time'); + // Log::debug('$result '.var_export($result, true)); + $expected = [ + 'type' => 'timestamp', + 'length' => NULL, + 'default' => NULL, + 'null' => true, + 'comment' => NULL, + 'precision' => NULL, + ]; + $this->assertEquals($expected, $result); + + $result = $schema->getColumn('resolution_time'); + $this->assertEquals($expected, $result); + + // Test constraints + $expected = ['primary', 'matchgrid_i3']; + $this->assertEquals($expected, $schema->constraints()); + + $constraint = $schema->getConstraint('primary'); + $expected = [ + 'type' => 'primary', + 'columns' => [ + 'id', + ], + 'length' => [], + ]; + $this->assertEquals($expected, $constraint); + + $constraint = $schema->getConstraint('matchgrid_i3'); + $expected = [ + 'type' => 'unique', + 'columns' => [ + 'sor', + 'sorid' + ], + 'length' => [], + ]; + $this->assertEquals($expected, $constraint); + + // Test indexes + $expected = ['matchgrid_i1', 'matchgrid_i2', 'matchgrid_i4', 'matchgrid_i5']; + $this->assertEquals($expected, $schema->indexes()); + + $index = $schema->getIndex('matchgrid_i1'); + $expected = [ + 'type' => 'index', + 'columns' => [ + 'sor' + ], + 'length' => [], + ]; + $this->assertEquals($expected, $index); + + $index = $schema->getIndex('matchgrid_i2'); + $expected = [ + 'type' => 'index', + 'columns' => [ + 'sorid' + ], + 'length' => [], + ]; + $this->assertEquals($expected, $index); + + $index = $schema->getIndex('matchgrid_i4'); + $expected = [ + 'type' => 'index', + 'columns' => [ + 'referenceid' + ], + 'length' => [], + ]; + $this->assertEquals($expected, $index); + + $index = $schema->getIndex('matchgrid_i5'); + $expected = [ + 'type' => 'index', + 'columns' => [ + 'resolution_time' + ], + 'length' => [], + ]; + $this->assertEquals($expected, $index); + + // Cleanup. + $db->execute('DROP TABLE mg_testmatchgrid'); + } + + /** + * Test calculation of matchgrid id. + * + * @return void + */ + public function testCalculateMatchgridId() { + $this->createTestMatchgrid(); + $id = $this->getTable()->calculateMatchgridId(1); + $this->assertEquals(1, $id); + } + + // TODO public function findActiveMatchgrids() {} + + public function tearDown() { + parent::tearDown(); + + // TODO drop mg_testmatchgrid table ? + } + + /** + * Test table_name field validation. + * + * @return void + */ + public function testValidationTableName() { + $this->assertFieldMaxLength('table_name', 128); + $this->assertFieldRegex('table_name', '/[a-zA-Z0-9_$]/'); + $this->assertFieldNotEmpty('table_name'); + } + + /** + * Test description field validation. + * + * @return void + */ + public function testValidationDescription() { + $this->assertFieldMaxLength('description', 128); + $this->assertFieldAllowEmpty('description'); + } + + /** + * Test status field validation. + * + * @return void + */ + public function testValidationStatus() { + $list = [ + StatusEnum::Active, + StatusEnum::Suspended + ]; + $this->assertFieldInList('status', $list); + $this->assertFieldNotEmpty('status'); + } + + /** + * Test referenceid_method field validation. + * + * @return void + */ + public function testValidationReferenceidMethod() { + $list = [ + ReferenceIdEnum::Sequence, + ReferenceIdEnum::UUID + ]; + $this->assertFieldInList('referenceid_method', $list); + $this->assertFieldNotEmpty('referenceid_method'); + } + + /** + * Test referenceid_start field validation. + * + * @return void + */ + public function testValidationReferenceidStart() { + $this->assertFieldRangeUnbounded('referenceid_start', 1); // TODO fix in MatchgridsTable null -> PHP_INT_MAX + $this->assertFieldAllowEmpty('referenceid_start'); + } + + /** + * Test referenceid_prefix field validation. + * + * @return void + */ + public function testValidationReferenceidPrefix() { + $this->assertFieldMaxLength('referenceid_prefix', 32); + $this->assertFieldAllowEmpty('referenceid_prefix'); // TODO fix in table class + } + + +} \ No newline at end of file diff --git a/app/tests/TestCase/Model/Table/MetaTableTest.php b/app/tests/TestCase/Model/Table/MetaTableTest.php new file mode 100644 index 000000000..3a0d54410 --- /dev/null +++ b/app/tests/TestCase/Model/Table/MetaTableTest.php @@ -0,0 +1,58 @@ +get('Meta'); + } + + /** + * Test field validation. + * + * @return void + */ + public function testValidation() { + $this->assertFieldMaxLength('upgrade_version', 16); + $this->assertFieldNotEmpty('upgrade_version'); + } + + // TODO public function testSetUpgradeVersion() {} +} \ No newline at end of file diff --git a/app/tests/TestCase/Model/Table/PermissionsTableTest.php b/app/tests/TestCase/Model/Table/PermissionsTableTest.php new file mode 100644 index 000000000..287b12d9c --- /dev/null +++ b/app/tests/TestCase/Model/Table/PermissionsTableTest.php @@ -0,0 +1,111 @@ +get('Permissions'); + } + + /** + * Test table initialization. + * + * @return void + */ + public function testInitialize() { + $table = $this->getTable(); + $this->assertInstanceOf('App\Model\Table\PermissionsTable', $table); + $this->assertTrue($table->hasBehavior('Timestamp')); + $this->assertBelongsTo('App\Model\Table\MatchgridsTable', 'Matchgrids'); + $this->assertEquals('username', $table->getDisplayField()); + $expected = [ + 'matchgrids' => [ + 'type' => 'select', + 'model' => 'Matchgrids', + 'find' => 'activeMatchgrids' + ], + 'permissions' => [ + 'type' => 'enum', + 'class' => 'PermissionEnum' + ] + ]; + $this->assertEquals($expected, $table->getAutoViewVars()); + } + + /** + * Test username field validation. + * + * @return void + */ + public function testValidationUsername() { + $this->assertFieldMaxLength('username', 128); + $this->assertFieldNotEmpty('username'); + } + + /** + * Test matchgrid_id field validation. + * + * @return void + */ + public function testValidationMatchgridId() { + $this->assertFieldInteger('matchgrid_id'); + $this->assertFieldAllowEmpty('matchgrid_id'); + } + + /** + * Test permission field validation. + * + * @return void + */ + public function testValidationPermission() { + $list = [ + PermissionEnum::MatchgridAdmin, + PermissionEnum::PlatformAdmin, + PermissionEnum::ReconciliationManager, + PermissionEnum::ReconciliationSupport, + PermissionEnum::None + ]; + $this->assertFieldInList('permission', $list); + $this->assertFieldNotEmpty('permission'); + } + + // TODO test public function testFindForUser() {} +} \ No newline at end of file diff --git a/app/tests/TestCase/Model/Table/RuleAttributesTableTest.php b/app/tests/TestCase/Model/Table/RuleAttributesTableTest.php new file mode 100644 index 000000000..b8c9ff5cd --- /dev/null +++ b/app/tests/TestCase/Model/Table/RuleAttributesTableTest.php @@ -0,0 +1,96 @@ +get('rule_attributes'); + } + + /** + * Test table initialization. + * + * @return void + */ + public function testInitialize() { + $table = $this->getTable(); + $this->assertInstanceOf('App\Model\Table\RuleAttributesTable', $table); + $this->assertTrue($table->hasBehavior('Timestamp')); + $this->assertBelongsTo('App\Model\Table\AttributesTable', 'Attributes'); + $this->assertBelongsTo('App\Model\Table\RulesTable', 'Rules'); + } + + /** + * Test rule_id field validation. + * + * @return void + */ + public function testValidationRuleId() { + $this->assertFieldInteger('rule_id'); + $this->assertFieldNotEmpty('rule_id'); + } + + /** + * Test attribute_id field validation. + * + * @return void + */ + public function testValidationAttributeId() { + $this->assertFieldInteger('attribute_id'); + $this->assertFieldNotEmpty('attribute_id'); + } + + /** + * Test search_type field validation. + * + * @return void + */ + public function testValidationSearchType() { + $list = [ + SearchTypeEnum::Distance, + SearchTypeEnum::Exact, + SearchTypeEnum::Skip, + SearchTypeEnum::Substring + ]; + $this->assertFieldInList('search_type', $list); + $this->assertFieldNotEmpty('search_type'); + } +} \ No newline at end of file diff --git a/app/tests/TestCase/Model/Table/RulesTableTest.php b/app/tests/TestCase/Model/Table/RulesTableTest.php new file mode 100644 index 000000000..27f95ff31 --- /dev/null +++ b/app/tests/TestCase/Model/Table/RulesTableTest.php @@ -0,0 +1,144 @@ +get('rules'); + } + + /** + * Test table initialization. + * + * @return void + */ + public function testInitialize() { + $table = $this->getTable(); + $this->assertInstanceOf('App\Model\Table\RulesTable', $table); + + $this->assertTrue($table->hasBehavior('Timestamp')); + + $this->assertBelongsTo('App\Model\Table\MatchgridsTable', 'Matchgrids'); + + $this->assertHasMany('App\Model\Table\RuleAttributesTable', 'RuleAttributes'); + $this->assertTrue($table->getAssociation('RuleAttributes')->getDependent()); + + $this->assertEquals('name', $table->getDisplayField()); + + $this->assertEquals(['RuleAttributes'], $table->getEditContains()); + $this->assertEquals(['RuleAttributes'], $table->getPatchAssociated()); + + $this->assertEquals('matchgrid_id', $table->getPrimaryLink()); + $this->assertTrue($table->requiresMatchgrid()); + + $expected = [ + 'attributes' => [ + 'type' => 'auxiliary', + 'model' => 'Attributes', + 'find' => 'filterPrimaryLink' + ], + 'confidenceModes' => [ + 'type' => 'enum', + 'class' => 'ConfidenceModeEnum' + ], + 'searchTypes' => [ + 'type' => 'enum', + 'class' => 'SearchTypeEnum' + ] + ]; + $this->assertEquals($expected, $table->getAutoViewVars()); + } + + /** + * Test name field validation. + * + * @return void + */ + public function testValidationName() { + $this->assertFieldMaxLength('name', 32); + $this->assertFieldNotEmpty('name'); + } + + /** + * Test matchgrid_id field validation. + * + * @return void + */ + public function testValidationMatchgridId() { + $this->assertFieldInteger('matchgrid_id'); + $this->assertFieldNotEmpty('matchgrid_id'); // TODO fix allowEmpty('ordr') in RulesTable + } + + /** + * Test description field validation. + * + * @return void + */ + public function testValidationDescription() { + $this->assertFieldMaxLength('description', 128); + $this->assertFieldAllowEmpty('description'); + } + + /** + * Test confidence_mode field validation. + * + * @return void + */ + public function testValidationConfidenceMode() { + $list = [ + ConfidenceModeEnum::Canonical, + ConfidenceModeEnum::Potential + ]; + $this->assertFieldInList('confidence_mode', $list); + $this->assertFieldNotEmpty('confidence_mode'); + } + + /** + * Test ordr field validation. + * + * @return void + */ + public function testValidationOrdr() { + $this->assertFieldInteger('ordr'); + $this->assertFieldAllowEmpty('ordr'); // TODO fix allowEmpty('ordr') in RulesTable + } + +} \ No newline at end of file diff --git a/app/tests/TestCase/Model/Table/SystemsOfRecordTableTest.php b/app/tests/TestCase/Model/Table/SystemsOfRecordTableTest.php new file mode 100644 index 000000000..ace91d4b4 --- /dev/null +++ b/app/tests/TestCase/Model/Table/SystemsOfRecordTableTest.php @@ -0,0 +1,111 @@ +get('systems_of_record'); + } + + /** + * Test table initialization. + * + * @return void + */ + public function testInitialize() { + $table = $this->getTable(); + + $this->assertInstanceOf('App\Model\Table\SystemsOfRecordTable', $table); + + $this->assertTrue($table->hasBehavior('Timestamp')); + + $this->assertHasMany('App\Model\Table\ApiUsersTable', 'ApiUsers'); + + $this->assertBelongsTo('App\Model\Table\MatchgridsTable', 'Matchgrids'); + + $this->assertEquals('label', $table->getDisplayField()); + + $this->assertEquals('matchgrid_id', $table->getPrimaryLink()); + $this->assertTrue($table->requiresMatchgrid()); + + $expected = [ + 'resolutionModes' => [ + 'type' => 'enum', + 'class' => 'ResolutionModeEnum' + ] + ]; + $this->assertEquals($expected, $table->getAutoViewVars()); + } + + /** + * Test label field validation. + * + * @return void + */ + public function testValidationLabel() { + $this->assertFieldMaxLength('label', 80); + $this->assertFieldNotEmpty('label'); + } + + /** + * Test matchgrid_id field validation. + * + * @return void + */ + public function testValidationMatchgridId() { + $this->assertFieldInteger('matchgrid_id'); + $this->assertFieldNotEmpty('matchgrid_id'); + } + + /** + * Test resolution_mode field validation. + * + * @return void + */ + public function testValidationResolutionMode() { + $list = [ + ResolutionModeEnum::External, + ResolutionModeEnum::Interactive + ]; + $this->assertFieldInList('resolution_mode', $list); + $this->assertFieldNotEmpty('resolution_mode'); + } +} \ No newline at end of file From d1383b0db6f6a493857043dc98779a340ae85f50 Mon Sep 17 00:00:00 2001 From: Tom Zeller Date: Fri, 8 Mar 2019 12:27:44 -0600 Subject: [PATCH 4/6] Fix copy-paste error in validator (CO-1716) --- app/src/Model/Table/MatchgridsTable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/Model/Table/MatchgridsTable.php b/app/src/Model/Table/MatchgridsTable.php index 463ab52ab..647850f8c 100644 --- a/app/src/Model/Table/MatchgridsTable.php +++ b/app/src/Model/Table/MatchgridsTable.php @@ -218,7 +218,7 @@ public function validationDefault(Validator $validator) { 'content', [ 'rule' => [ 'maxLength', 32 ] ] ); - $validator->allowEmpty('referenceid_start'); + $validator->allowEmpty('referenceid_prefix'); return $validator; } From e2ccede78a222cb651a69ba6fa02af3a8eb5b4f7 Mon Sep 17 00:00:00 2001 From: Tom Zeller Date: Fri, 8 Mar 2019 12:28:17 -0600 Subject: [PATCH 5/6] Remove TODOs (CO-1716) --- app/tests/TestCase/Model/Table/MatchgridsTableTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/tests/TestCase/Model/Table/MatchgridsTableTest.php b/app/tests/TestCase/Model/Table/MatchgridsTableTest.php index 9eac8edff..4a6a45049 100644 --- a/app/tests/TestCase/Model/Table/MatchgridsTableTest.php +++ b/app/tests/TestCase/Model/Table/MatchgridsTableTest.php @@ -315,7 +315,7 @@ public function testValidationReferenceidMethod() { * @return void */ public function testValidationReferenceidStart() { - $this->assertFieldRangeUnbounded('referenceid_start', 1); // TODO fix in MatchgridsTable null -> PHP_INT_MAX + $this->assertFieldRangeUnbounded('referenceid_start', 1); $this->assertFieldAllowEmpty('referenceid_start'); } @@ -326,7 +326,7 @@ public function testValidationReferenceidStart() { */ public function testValidationReferenceidPrefix() { $this->assertFieldMaxLength('referenceid_prefix', 32); - $this->assertFieldAllowEmpty('referenceid_prefix'); // TODO fix in table class + $this->assertFieldAllowEmpty('referenceid_prefix'); } From 0c59988fff16ae70efaf59829fc2f2f313fcea17 Mon Sep 17 00:00:00 2001 From: Tom Zeller Date: Fri, 8 Mar 2019 12:30:10 -0600 Subject: [PATCH 6/6] Fix copy-paste error in validator (CO-1716) --- app/src/Model/Table/RulesTable.php | 2 +- app/tests/TestCase/Model/Table/RulesTableTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/Model/Table/RulesTable.php b/app/src/Model/Table/RulesTable.php index cb86d1564..20d422191 100644 --- a/app/src/Model/Table/RulesTable.php +++ b/app/src/Model/Table/RulesTable.php @@ -126,7 +126,7 @@ public function validationDefault(Validator $validator) { 'content', [ 'rule' => 'isInteger' ] ); - $validator->allowEmpty('matchgrid_id'); + $validator->allowEmpty('ordr'); return $validator; } diff --git a/app/tests/TestCase/Model/Table/RulesTableTest.php b/app/tests/TestCase/Model/Table/RulesTableTest.php index 27f95ff31..b92ce9856 100644 --- a/app/tests/TestCase/Model/Table/RulesTableTest.php +++ b/app/tests/TestCase/Model/Table/RulesTableTest.php @@ -104,7 +104,7 @@ public function testValidationName() { */ public function testValidationMatchgridId() { $this->assertFieldInteger('matchgrid_id'); - $this->assertFieldNotEmpty('matchgrid_id'); // TODO fix allowEmpty('ordr') in RulesTable + $this->assertFieldNotEmpty('matchgrid_id'); } /** @@ -138,7 +138,7 @@ public function testValidationConfidenceMode() { */ public function testValidationOrdr() { $this->assertFieldInteger('ordr'); - $this->assertFieldAllowEmpty('ordr'); // TODO fix allowEmpty('ordr') in RulesTable + $this->assertFieldAllowEmpty('ordr'); } } \ No newline at end of file