ChadoFieldGetValuesListTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. * Test for fields based on tables besides the base one for the bundle.
  104. *
  105. * CURRENTLY RETRIEVING VALUES FOR THESE TABLES IS NOT SUPPORTED.
  106. *
  107. * @group fields
  108. * @group getValueList
  109. */
  110. public function testNonBaseTable() {
  111. include_once(drupal_get_path('tripal_chado', 'module') . '/includes/TripalFields/ChadoField.inc');
  112. // Retrieve a list of fields to test.
  113. // Note: this list is cached to improve performance.
  114. $fields = $this->retrieveFieldList();
  115. // Only iterate through fields that store their data in chado and
  116. // specifically, where the field stores it's data in the base table of the bundle
  117. // and is not a foreign key.
  118. foreach ($fields['field_chado_storage']['referring'] as $key => $info) {
  119. $field_name = $info['field_name'];
  120. // Construct the Field instance we want the values for.
  121. // Specifying "ChadoField" here ensures we are only testing our
  122. // implementation of getValueList() and not the custom version for any
  123. // given field.
  124. // YOU SHOULD TEST CUSTOM FIELD IMPLEMENTATIONS SEPARATELY.
  125. $instance = new \ChadoField($info['field_info'], $info['instance_info']);
  126. // Supress tripal errors
  127. putenv("TRIPAL_SUPPRESS_ERRORS=TRUE");
  128. ob_start();
  129. try {
  130. // Retrieve the values.
  131. // $values will be an array containing the distinct set of values for this field instance.
  132. $values = $instance->getValueList(array('limit' => 5));
  133. // @todo Check that we got the correct warning message.
  134. // Currently we can't check this because we need to supress the error in order to keep it from printing
  135. // but once we do, we can't access it ;-P
  136. } catch (Exception $e) {
  137. $this->fail("Although we don't support values lists for $field_name, it still shouldn't produce an exception!");
  138. }
  139. // Clean the buffer and unset tripal errors suppression
  140. ob_end_clean();
  141. putenv("TRIPAL_SUPPRESS_ERRORS");
  142. $this->assertFalse($values, "We don't support retrieving values for $field_name since it doesn't store data in the base table.");
  143. }
  144. }
  145. /**
  146. * Returns a list of Fields sorted by their backend, etc. for use in tests.
  147. */
  148. private function retrieveFieldList() {
  149. if ($this->field_list === NULL) {
  150. $this->field_list = array();
  151. $bundles = field_info_instances('TripalEntity');
  152. foreach($bundles as $bundle_name => $fields) {
  153. $bundle = tripal_load_bundle_entity(array('name'=> $bundle_name));
  154. foreach ($fields as $field_name => $instance_info) {
  155. $bundle_base_table = $base_schema = NULL;
  156. // Load the field info.
  157. $field_info = field_info_field($field_name);
  158. $storage = $field_info['storage']['type'];
  159. // If this field stores it's data in chado...
  160. // Determine the relationship between this field and the bundle base table.
  161. $rel = NULL;
  162. if ($storage == 'field_chado_storage') {
  163. // We need to know the table this field stores it's data in.
  164. $bundle_base_table = $bundle->data_table;
  165. // and the schema for that table.
  166. $base_schema = chado_get_schema($bundle_base_table);
  167. // and the table this field stores it's data in.
  168. $field_table = $instance_info['settings']['chado_table'];
  169. $field_column = $instance_info['settings']['chado_column'];
  170. // By default we simply assume there is some relationship.
  171. $rel = 'referring';
  172. // If the field and bundle store their data in the same table
  173. // then it's either a "base" or "foreign key" relationship
  174. // based on the schema.
  175. if ($bundle_base_table == $field_table) {
  176. // We assume it's not a foreign key...
  177. $rel = 'base';
  178. // and then check the schema to see if we're wrong :-)
  179. foreach ($base_schema['foreign keys'] as $schema_info) {
  180. if (isset($schema_info['columns'][ $field_column ])) { $rel = 'foreign key'; }
  181. }
  182. }
  183. }
  184. $info = array(
  185. 'field_name' => $field_name,
  186. 'bundle_name' => $bundle_name,
  187. 'bundle' => $bundle,
  188. 'bundle_base_table' => $bundle_base_table,
  189. 'base_schema' => $base_schema,
  190. 'field_info' => $field_info,
  191. 'instance_info' => $instance_info,
  192. );
  193. $key = $bundle_name . '--' . $field_name;
  194. if ($rel) {
  195. $this->field_list[$storage][$rel][$key] = $info;
  196. }
  197. else {
  198. $this->field_list[$storage][$key] = $info;
  199. }
  200. }
  201. }
  202. }
  203. return $this->field_list;
  204. }
  205. }