ChadoFieldGetValuesListTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. * Returns a list of Fields sorted by their backend, etc. for use in tests.
  59. */
  60. private function retrieveFieldList() {
  61. if ($this->field_list === NULL) {
  62. $this->field_list = array();
  63. $bundles = field_info_instances('TripalEntity');
  64. foreach($bundles as $bundle_name => $fields) {
  65. $bundle = tripal_load_bundle_entity(array('name'=> $bundle_name));
  66. foreach ($fields as $field_name => $instance_info) {
  67. $bundle_base_table = $base_schema = NULL;
  68. // Load the field info.
  69. $field_info = field_info_field($field_name);
  70. $storage = $field_info['storage']['type'];
  71. // If this field stores it's data in chado...
  72. // Determine the relationship between this field and the bundle base table.
  73. $rel = NULL;
  74. if ($storage == 'field_chado_storage') {
  75. // We need to know the table this field stores it's data in.
  76. $bundle_base_table = $bundle->data_table;
  77. // and the schema for that table.
  78. $base_schema = chado_get_schema($bundle_base_table);
  79. // and the table this field stores it's data in.
  80. $field_table = $instance_info['settings']['chado_table'];
  81. $field_column = $instance_info['settings']['chado_column'];
  82. // By default we simply assume there is some relationship.
  83. $rel = 'referring';
  84. // If the field and bundle store their data in the same table
  85. // then it's either a "base" or "foreign key" relationship
  86. // based on the schema.
  87. if ($bundle_base_table == $field_table) {
  88. // We assume it's not a foreign key...
  89. $rel = 'base';
  90. // and then check the schema to see if we're wrong :-)
  91. foreach ($base_schema['foreign keys'] as $schema_info) {
  92. if (isset($schema_info['columns'][ $field_column ])) { $rel = 'foreign key'; }
  93. }
  94. }
  95. }
  96. $info = array(
  97. 'field_name' => $field_name,
  98. 'bundle_name' => $bundle_name,
  99. 'bundle' => $bundle,
  100. 'bundle_base_table' => $bundle_base_table,
  101. 'base_schema' => $base_schema,
  102. 'field_info' => $field_info,
  103. 'instance_info' => $instance_info,
  104. );
  105. $key = $bundle_name . '--' . $field_name;
  106. if ($rel) {
  107. $this->field_list[$storage][$rel][$key] = $info;
  108. }
  109. else {
  110. $this->field_list[$storage][$key] = $info;
  111. }
  112. }
  113. }
  114. }
  115. return $this->field_list;
  116. }
  117. }