|
@@ -0,0 +1,442 @@
|
|
|
+<?php
|
|
|
+namespace Tests\tripal_chado\fields;
|
|
|
+
|
|
|
+use StatonLab\TripalTestSuite\DBTransaction;
|
|
|
+use StatonLab\TripalTestSuite\TripalTestCase;
|
|
|
+use StatonLab\TripalTestSuite\Database\Factory;
|
|
|
+
|
|
|
+
|
|
|
+ * Test ChadoField->getValueList() Method.
|
|
|
+ */
|
|
|
+class ChadoFieldGetValuesListTest extends TripalTestCase {
|
|
|
+
|
|
|
+ use DBTransaction;
|
|
|
+
|
|
|
+
|
|
|
+ private $field_list = NULL;
|
|
|
+
|
|
|
+
|
|
|
+ * Test getValueList for fields based on columns in the base table.
|
|
|
+ *
|
|
|
+ * @dataProvider getBaseFields
|
|
|
+ *
|
|
|
+ * @group fields
|
|
|
+ * @group getValueList
|
|
|
+ */
|
|
|
+ public function testBaseTableColumns($field_name, $bundle_name, $info) {
|
|
|
+ include_once(drupal_get_path('tripal_chado', 'module') . '/includes/TripalFields/ChadoField.inc');
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $instance = new \ChadoField($info['field_info'], $info['instance_info']);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $values = $instance->getValueList(array('limit' => 5));
|
|
|
+
|
|
|
+
|
|
|
+ $this->assertTrue(
|
|
|
+ is_array($values),
|
|
|
+ t(
|
|
|
+ 'No values returned for @field_name (bundle: @bundle_name, bundle base table: @bundle_base_table, chado table: @chado_table, chado column: @chado_column).',
|
|
|
+ array(
|
|
|
+ '@field_name' => $field_name,
|
|
|
+ '@bundle_name' => $bundle_name,
|
|
|
+ '@bundle_base_table' => $info['bundle_base_table'],
|
|
|
+ '@chado_table' => $info['instance_info']['settings']['chado_table'],
|
|
|
+ '@chado_column' => $info['instance_info']['settings']['chado_column'],
|
|
|
+ )
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+
|
|
|
+ $this->assertLessThanOrEqual(5, sizeof($values),
|
|
|
+ t('Returned too many results for @field_name.', array('@field_name' => $field_name)));
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $fake_value = $this->generateFakeData($info['bundle_base_table'], $info['base_schema'], $info['instance_info']['settings']['chado_column']);
|
|
|
+ if ($fake_value !== FALSE) {
|
|
|
+
|
|
|
+
|
|
|
+ $values = $instance->getValueList(array('limit' => 200));
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if (sizeof($values) < 200) {
|
|
|
+ $this->assertContains($fake_value, $values, "\nThe following array should but does not contain our fake value ('$fake_value'): '" . implode("', '", $values) . '.');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * DataProvider: a list of fields who store their data in the base table of a bundle.
|
|
|
+ *
|
|
|
+ * Each element describes a field instance and consists of:
|
|
|
+ * - the machine name of the field (e.g. obi__organism).
|
|
|
+ * - the machine name of the bundle (e.g. bio_data_17).
|
|
|
+ * - an array of additional information including:
|
|
|
+ * - instance_info: information about the field instance.
|
|
|
+ * - field_info: information about the field.
|
|
|
+ * - bundle: the TripalBundle object.
|
|
|
+ * - bundle_base_table: if applicable, the chado base table the bundle stores it's data in.
|
|
|
+ * - base_schema: the Tripal Schema array for the bundle_base_table.
|
|
|
+ */
|
|
|
+ public function getBaseFields() {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $fields = $this->retrieveFieldList();
|
|
|
+
|
|
|
+ return $fields['field_chado_storage']['base'];
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * Test for fields based on columns in the base table that are also foreign keys.
|
|
|
+ *
|
|
|
+ * @dataProvider getBaseFkFields
|
|
|
+ * @group current
|
|
|
+ * @group fields
|
|
|
+ * @group getValueList
|
|
|
+ */
|
|
|
+ public function testBaseTableForeignKey($field_name, $bundle_name, $info) {
|
|
|
+ include_once(drupal_get_path('tripal_chado', 'module') . '/includes/TripalFields/ChadoField.inc');
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $instance = new \ChadoField($info['field_info'], $info['instance_info']);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $values = $instance->getValueList(array('limit' => 5));
|
|
|
+
|
|
|
+
|
|
|
+ $this->assertTrue(
|
|
|
+ is_array($values),
|
|
|
+ t(
|
|
|
+ 'No values returned for @field_name with no label string set (bundle: @bundle_name, bundle base table: @bundle_base_table, chado table: @chado_table, chado column: @chado_column).',
|
|
|
+ array(
|
|
|
+ '@field_name' => $field_name,
|
|
|
+ '@bundle_name' => $bundle_name,
|
|
|
+ '@bundle_base_table' => $info['bundle_base_table'],
|
|
|
+ '@chado_table' => $info['instance_info']['settings']['chado_table'],
|
|
|
+ '@chado_column' => $info['instance_info']['settings']['chado_column'],
|
|
|
+ )
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+
|
|
|
+ $this->assertLessThanOrEqual(5, sizeof($values),
|
|
|
+ t('Returned too many results for @field_name.', array('@field_name' => $field_name)));
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $fake_fk_record = $this->generateFakeData($info['bundle_base_table'], $info['base_schema'], $info['instance_info']['settings']['chado_column'], $info['fk_table']);
|
|
|
+ if ($fake_fk_record !== FALSE) {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $schema = chado_get_schema($info['fk_table']);
|
|
|
+ $fk_table_fields = array_keys($schema['fields']);
|
|
|
+ $use_in_label = array_rand($fk_table_fields, 2);
|
|
|
+ $column1 = $fk_table_fields[$use_in_label[0]];
|
|
|
+ $column2 = $fk_table_fields[$use_in_label[1]];
|
|
|
+
|
|
|
+ $label_string = '['.$column2.'] (['.$column1.'])';
|
|
|
+
|
|
|
+
|
|
|
+ $values = $instance->getValueList(array('limit' => 200, 'label_string' => $label_string));
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if (sizeof($values) < 200) {
|
|
|
+ $fixed_key = $fake_fk_record->{$info['fk_table'].'_id'};
|
|
|
+ $this->assertArrayHasKey($fixed_key, $values, "\nThe following array should but does not contain our fake record: " . print_r($fake_fk_record, TRUE));
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $expected_label = $fake_fk_record->{$column2} . ' (' . $fake_fk_record->{$column1} . ')';
|
|
|
+ $this->assertEquals($expected_label, $values[$fixed_key], "\nThe label should have been '$label_string' with the column values filled in.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * DataProvider: a list of fields who store their data in the base table of a bundle.
|
|
|
+ *
|
|
|
+ * Each element describes a field instance and consists of:
|
|
|
+ * - the machine name of the field (e.g. obi__organism).
|
|
|
+ * - the machine name of the bundle (e.g. bio_data_17).
|
|
|
+ * - an array of additional information including:
|
|
|
+ * - instance_info: information about the field instance.
|
|
|
+ * - field_info: information about the field.
|
|
|
+ * - bundle: the TripalBundle object.
|
|
|
+ * - bundle_base_table: if applicable, the chado base table the bundle stores it's data in.
|
|
|
+ * - base_schema: the Tripal Schema array for the bundle_base_table.
|
|
|
+ */
|
|
|
+ public function getBaseFkFields() {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $fields = $this->retrieveFieldList();
|
|
|
+
|
|
|
+ return $fields['field_chado_storage']['foreign key'];
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * Test for fields based on tables besides the base one for the bundle.
|
|
|
+ * CURRENTLY RETRIEVING VALUES FOR THESE TABLES IS NOT SUPPORTED.
|
|
|
+ *
|
|
|
+ * @dataProvider getNonBaseFields
|
|
|
+ *
|
|
|
+ * @group fields
|
|
|
+ * @group getValueList
|
|
|
+ */
|
|
|
+ public function testNonBaseTable($field_name, $bundle_name, $info) {
|
|
|
+ include_once(drupal_get_path('tripal_chado', 'module') . '/includes/TripalFields/ChadoField.inc');
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $instance = new \ChadoField($info['field_info'], $info['instance_info']);
|
|
|
+
|
|
|
+
|
|
|
+ putenv("TRIPAL_SUPPRESS_ERRORS=TRUE");
|
|
|
+ ob_start();
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $values = $instance->getValueList(array('limit' => 5));
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ } catch (Exception $e) {
|
|
|
+ $this->fail("Although we don't support values lists for $field_name, it still shouldn't produce an exception!");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ ob_end_clean();
|
|
|
+ putenv("TRIPAL_SUPPRESS_ERRORS");
|
|
|
+
|
|
|
+ $this->assertFalse($values, "We don't support retrieving values for $field_name since it doesn't store data in the base table.");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * DataProvider: a list of fields who store their data in the base table of a bundle.
|
|
|
+ *
|
|
|
+ * Each element describes a field instance and consists of:
|
|
|
+ * - the machine name of the field (e.g. obi__organism).
|
|
|
+ * - the machine name of the bundle (e.g. bio_data_17).
|
|
|
+ * - an array of additional information including:
|
|
|
+ * - instance_info: information about the field instance.
|
|
|
+ * - field_info: information about the field.
|
|
|
+ * - bundle: the TripalBundle object.
|
|
|
+ * - bundle_base_table: if applicable, the chado base table the bundle stores it's data in.
|
|
|
+ * - base_schema: the Tripal Schema array for the bundle_base_table.
|
|
|
+ */
|
|
|
+ public function getNonBaseFields() {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $fields = $this->retrieveFieldList();
|
|
|
+
|
|
|
+ return $fields['field_chado_storage']['referring'];
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * Returns a list of Fields sorted by their backend, etc. for use in tests.
|
|
|
+ */
|
|
|
+ private function retrieveFieldList() {
|
|
|
+ if ($this->field_list === NULL) {
|
|
|
+
|
|
|
+ $this->field_list = array();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $bundles = field_info_instances('TripalEntity');
|
|
|
+ foreach($bundles as $bundle_name => $fields) {
|
|
|
+
|
|
|
+
|
|
|
+ $bundle = tripal_load_bundle_entity(array('name'=> $bundle_name));
|
|
|
+
|
|
|
+
|
|
|
+ foreach ($fields as $field_name => $instance_info) {
|
|
|
+ $bundle_base_table = $base_schema = NULL;
|
|
|
+
|
|
|
+
|
|
|
+ $field_info = field_info_field($field_name);
|
|
|
+
|
|
|
+
|
|
|
+ $storage = $field_info['storage']['type'];
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $rel = NULL;
|
|
|
+ if ($storage == 'field_chado_storage') {
|
|
|
+
|
|
|
+
|
|
|
+ $bundle_base_table = $bundle->data_table;
|
|
|
+
|
|
|
+ $base_schema = chado_get_schema($bundle_base_table);
|
|
|
+
|
|
|
+ $field_table = $instance_info['settings']['chado_table'];
|
|
|
+ $field_column = $instance_info['settings']['chado_column'];
|
|
|
+
|
|
|
+
|
|
|
+ $rel = 'referring';
|
|
|
+ $rel_table = NULL;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if ($bundle_base_table == $field_table) {
|
|
|
+
|
|
|
+
|
|
|
+ $rel = 'base';
|
|
|
+
|
|
|
+ foreach ($base_schema['foreign keys'] as $schema_info) {
|
|
|
+ if (isset($schema_info['columns'][ $field_column ])) {
|
|
|
+ $rel = 'foreign key';
|
|
|
+ $rel_table = $schema_info['table'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ $info = array(
|
|
|
+ 'field_name' => $field_name,
|
|
|
+ 'bundle_name' => $bundle_name,
|
|
|
+ 'bundle' => $bundle,
|
|
|
+ 'bundle_base_table' => $bundle_base_table,
|
|
|
+ 'base_schema' => $base_schema,
|
|
|
+ 'field_info' => $field_info,
|
|
|
+ 'instance_info' => $instance_info,
|
|
|
+ 'fk_table' => $rel_table,
|
|
|
+ );
|
|
|
+
|
|
|
+
|
|
|
+ $key = $bundle_name . '--' . $field_name;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if ($rel) {
|
|
|
+ $this->field_list[$storage][$rel][$key] = array(
|
|
|
+ $field_name,
|
|
|
+ $bundle_name,
|
|
|
+ $info
|
|
|
+ );
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ $this->field_list[$storage][$key] = array(
|
|
|
+ $field_name,
|
|
|
+ $bundle_name,
|
|
|
+ $info
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->field_list;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * Generate fake data for a given bundle.
|
|
|
+ *
|
|
|
+ * If only the first parameter is provided this function adds fake data to the indicated
|
|
|
+ * chado table. If the third parameter is provided the generated fake data will
|
|
|
+ * have a fixed value for the indicated column.
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ * Returns FALSE if it was unable to create fake data.
|
|
|
+ */
|
|
|
+ private function generateFakeData($chado_table, $schema, $fixed_column = FALSE, $fk_table = FALSE) {
|
|
|
+ $faker = \Faker\Factory::create();
|
|
|
+
|
|
|
+
|
|
|
+ if (!Factory::exists('chado.'.$chado_table)) {
|
|
|
+ return FALSE;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if ($fixed_column === FALSE) {
|
|
|
+ factory('chado.'.$chado_table, 50)->create();
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $fake_value = NULL;
|
|
|
+
|
|
|
+
|
|
|
+ if ($fk_table === FALSE) {
|
|
|
+ $column_type = $schema[$fixed_column]['type'];
|
|
|
+ if (($column_type == 'int')) {
|
|
|
+ $fake_value = $faker->randomNumber();
|
|
|
+ }
|
|
|
+ elseif (($column_type == 'varchar') OR ($column_type == 'text')) {
|
|
|
+ $fake_value = $faker->words(2,TRUE);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($fake_value !== NULL) {
|
|
|
+ factory('chado.'.$chado_table)->create(array(
|
|
|
+ $fixed_column => $fake_value,
|
|
|
+ ));
|
|
|
+ return $fake_value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ else {
|
|
|
+
|
|
|
+ $fake_table_record = factory('chado.'.$fk_table)->create();
|
|
|
+
|
|
|
+
|
|
|
+ if (!Factory::exists('chado.'.$fk_table)) {
|
|
|
+ return FALSE;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (isset($fake_table_record->{$fk_table.'_id'})) {
|
|
|
+ factory('chado.'.$chado_table)->create(array(
|
|
|
+ $fixed_column => $fake_table_record->{$fk_table.'_id'},
|
|
|
+ ));
|
|
|
+
|
|
|
+ return $fake_table_record;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return FALSE;
|
|
|
+
|
|
|
+ }
|
|
|
+}
|