ChadoFieldGetValuesListTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace Tests\tripal_chado\fields;
  3. use StatonLab\TripalTestSuite\DBTransaction;
  4. use StatonLab\TripalTestSuite\TripalTestCase;
  5. /**
  6. * Test ChadoField->getValueList() Method.
  7. */
  8. class ChadoFieldGetValuesListTest extends TripalTestCase {
  9. // Uncomment to auto start and rollback db transactions per test method.
  10. use DBTransaction;
  11. // Stores a list of fields to be tested including their storage method and instance info.
  12. private $field_list = NULL;
  13. /**
  14. * Test for fields based on columns in the base table.
  15. *
  16. * @group fields
  17. * @group getValueList
  18. */
  19. public function testBaseTableColumns() {
  20. include_once(drupal_get_path('tripal_chado', 'module') . '/includes/TripalFields/ChadoField.inc');
  21. // Retrieve a list of fields to test.
  22. // Note: this list is cached to improve performance.
  23. $fields = $this->retrieveFieldList();
  24. // Only iterate through fields that store their data in chado and
  25. // specifically, where the field stores it's data in the base table of the bundle
  26. // and is not a foreign key.
  27. foreach ($fields['field_chado_storage']['base'] as $key => $info) {
  28. $field_name = $info['field_name'];
  29. // Construct the Field instance we want the values for.
  30. // Specifying "ChadoField" here ensures we are only testing our
  31. // implementation of getValueList() and not the custom version for any
  32. // given field.
  33. // YOU SHOULD TEST CUSTOM FIELD IMPLEMENTATIONS SEPARATELY.
  34. $instance = new \ChadoField($info['field_info'], $info['instance_info']);
  35. // Retrieve the values.
  36. // $values will be an array containing the distinct set of values for this field instance.
  37. $values = $instance->getValueList(array('limit' => 5));
  38. // Ensure we have values returned!
  39. $this->assertTrue(
  40. is_array($values),
  41. t(
  42. 'No values returned for @field_name (bundle: @bundle_name, bundle base table: @bundle_base_table, chado table: @chado_table, chado column: @chado_column).',
  43. array(
  44. '@field_name' => $field_name,
  45. '@bundle_name' => $info['bundle_name'],
  46. '@bundle_base_table' => $info['bundle_base_table'],
  47. '@chado_table' => $info['instance_info']['settings']['chado_table'],
  48. '@chado_column' => $info['instance_info']['settings']['chado_column'],
  49. )
  50. )
  51. );
  52. // Ensure there are no more then 5 as specified in the limit above.
  53. $this->assertLessThanOrEqual(5, sizeof($values),
  54. t('Returned too many results for @field_name.', array('@field_name' => $field_name)));
  55. }
  56. }
  57. /**
  58. * Test for fields based on columns in the base table that are also foreign keys.
  59. *
  60. * @group fields
  61. * @group getValueList
  62. */
  63. public function testBaseTableForeignKey() {
  64. include_once(drupal_get_path('tripal_chado', 'module') . '/includes/TripalFields/ChadoField.inc');
  65. // Retrieve a list of fields to test.
  66. // Note: this list is cached to improve performance.
  67. $fields = $this->retrieveFieldList();
  68. // Only iterate through fields that store their data in chado and
  69. // specifically, where the field stores it's data in the base table of the bundle
  70. // and IS a foreign key.
  71. foreach ($fields['field_chado_storage']['foreign key'] as $key => $info) {
  72. $field_name = $info['field_name'];
  73. // Construct the Field instance we want the values for.
  74. // Specifying "ChadoField" here ensures we are only testing our
  75. // implementation of getValueList() and not the custom version for any
  76. // given field.
  77. // YOU SHOULD TEST CUSTOM FIELD IMPLEMENTATIONS SEPARATELY.
  78. $instance = new \ChadoField($info['field_info'], $info['instance_info']);
  79. // Retrieve the values using defaults.
  80. // $values will be an array containing the distinct set of values for this field instance.
  81. $values = $instance->getValueList(array('limit' => 5));
  82. // Ensure we have values returned!
  83. $this->assertTrue(
  84. is_array($values),
  85. t(
  86. '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).',
  87. array(
  88. '@field_name' => $field_name,
  89. '@bundle_name' => $info['bundle_name'],
  90. '@bundle_base_table' => $info['bundle_base_table'],
  91. '@chado_table' => $info['instance_info']['settings']['chado_table'],
  92. '@chado_column' => $info['instance_info']['settings']['chado_column'],
  93. )
  94. )
  95. );
  96. // Ensure there are no more then 5 as specified in the limit above.
  97. $this->assertLessThanOrEqual(5, sizeof($values),
  98. t('Returned too many results for @field_name.', array('@field_name' => $field_name)));
  99. // @todo Ensure it works with a label string set.
  100. }
  101. }
  102. /**
  103. * Returns a list of Fields sorted by their backend, etc. for use in tests.
  104. */
  105. private function retrieveFieldList() {
  106. if ($this->field_list === NULL) {
  107. $this->field_list = array();
  108. $bundles = field_info_instances('TripalEntity');
  109. foreach($bundles as $bundle_name => $fields) {
  110. $bundle = tripal_load_bundle_entity(array('name'=> $bundle_name));
  111. foreach ($fields as $field_name => $instance_info) {
  112. $bundle_base_table = $base_schema = NULL;
  113. // Load the field info.
  114. $field_info = field_info_field($field_name);
  115. $storage = $field_info['storage']['type'];
  116. // If this field stores it's data in chado...
  117. // Determine the relationship between this field and the bundle base table.
  118. $rel = NULL;
  119. if ($storage == 'field_chado_storage') {
  120. // We need to know the table this field stores it's data in.
  121. $bundle_base_table = $bundle->data_table;
  122. // and the schema for that table.
  123. $base_schema = chado_get_schema($bundle_base_table);
  124. // and the table this field stores it's data in.
  125. $field_table = $instance_info['settings']['chado_table'];
  126. $field_column = $instance_info['settings']['chado_column'];
  127. // By default we simply assume there is some relationship.
  128. $rel = 'referring';
  129. // If the field and bundle store their data in the same table
  130. // then it's either a "base" or "foreign key" relationship
  131. // based on the schema.
  132. if ($bundle_base_table == $field_table) {
  133. // We assume it's not a foreign key...
  134. $rel = 'base';
  135. // and then check the schema to see if we're wrong :-)
  136. foreach ($base_schema['foreign keys'] as $schema_info) {
  137. if (isset($schema_info['columns'][ $field_column ])) { $rel = 'foreign key'; }
  138. }
  139. }
  140. }
  141. $info = array(
  142. 'field_name' => $field_name,
  143. 'bundle_name' => $bundle_name,
  144. 'bundle' => $bundle,
  145. 'bundle_base_table' => $bundle_base_table,
  146. 'base_schema' => $base_schema,
  147. 'field_info' => $field_info,
  148. 'instance_info' => $instance_info,
  149. );
  150. $key = $bundle_name . '--' . $field_name;
  151. if ($rel) {
  152. $this->field_list[$storage][$rel][$key] = $info;
  153. }
  154. else {
  155. $this->field_list[$storage][$key] = $info;
  156. }
  157. }
  158. }
  159. }
  160. return $this->field_list;
  161. }
  162. }