Browse Source

Use data provider to test 3 very different relationship tables.

Lacey Sanderson 6 years ago
parent
commit
87ac168837
1 changed files with 51 additions and 11 deletions
  1. 51 11
      tests/tripal_chado/fields/sbo__relationship_widgetTest.php

+ 51 - 11
tests/tripal_chado/fields/sbo__relationship_widgetTest.php

@@ -6,15 +6,14 @@ use StatonLab\TripalTestSuite\TripalTestCase;
 
 class sbo__relationship_widgetTest extends TripalTestCase {
   // Uncomment to auto start and rollback db transactions per test method.
-  // use DBTransaction;
+  use DBTransaction;
 
   /**
    * Create a fake sbo__relationship_widget field?
    */
   private function initializeWidgetClass($bundle_name, $field_name, $widget_name, $entity_id) {
     $vars = [];
-    $vars['bundle'] = $vars['field_info'] = $vars['instance_info'] = NULL;
-    $vars['widget_class'] = $vars['entity'] = NULL;
+    $vars['widget_class'] = $vars['bundle'] = $vars['entity'] = NULL;
 
     // First include the appropriate class.
     $widget_class_path = DRUPAL_ROOT . '/' . drupal_get_path('module', 'tripal_chado')
@@ -27,7 +26,7 @@ class sbo__relationship_widgetTest extends TripalTestCase {
       $vars['instance_info'] = field_info_instance('TripalEntity', $field_name, $bundle_name);
 
       // Create an instance of the widget class.
-      $vars['widget_class'] = new \sbo__relationship_widget($this->field_info, $this->instance_info);
+      $vars['widget_class'] = new \sbo__relationship_widget($vars['field_info'], $vars['instance_info']);
 
       // load an entity to pretend the widget is modifying.
       $vars['entity'] = entity_load('TripalEntity', [$entity_id]);
@@ -36,13 +35,56 @@ class sbo__relationship_widgetTest extends TripalTestCase {
     return $vars;
   }
 
+  /**
+   * Data Provider: provides entities matching important test cases.
+   *
+   * Specifically, we will cover three relationship tables, which represent
+   * the diversity in the chado schema v1.3:
+   *  organism_relationship: subject_id, type_id, object_id,
+   *  stock_relationship: subject_id, type_id, object_id, value, rank,
+   *  project_relationship: project_subject_id, type_id, project_object_id, rank
+   *
+   * @returns
+   *   Returns an array where each item to be tested has the paramaters 
+   *   needed for initializeWidgetClass(). Specfically, $bundle_name, 
+   *   $field_name, $widget_name, $entity_id.
+   */
+  public function provideEntities() {
+     $data = [];
+
+     foreach (['organism', 'stock', 'project'] as $base_table) {
+
+       $field_name = 'sbo__relationship';
+       $widget_name = 'sbo__relationship_widget';
+
+       // find a bundle which stores it's data in the given base table.
+       // This will work on Travis since Tripal creates matching bundles by default.
+       // @todo ideally we would create a fake bundle here.
+       $bundle_id = db_query('SELECT bundle_id FROM chado_bundle WHERE data_table=:table LIMIT 1',
+         array(':table' => $base_table))->fetchField();
+       $bundle_name = 'bio_data_'.$bundle_id;
+
+       // Find an entity from the above bundle.
+       // @todo find a way to create a fake entity for use here.
+       $entity_id = db_query('SELECT id FROM tripal_entity WHERE bundle=:bundle LIMIT 1',
+         array(':bundle' => $bundle_name))->fetchField();
+
+       $data[] = [$bundle_name, $field_name, $widget_name, $entity_id];
+     }
+     return $data;
+  }
+
   /**
    * Test that we can initialize the widget properly.
+   *
+   * @dataProvider provideEntities()
+   *
+   * @group lacey
    */
-  public function testWidgetClassInitialization() {
+  public function testWidgetClassInitialization($bundle_name, $field_name, $widget_name, $entity_id) {
 
     // Initialize our variables.
-    $vars = $this->initializeWidgetClass('bio_data_1', 'sbo__relationship', 'sbo__relationship_widget', 8);
+    $vars = $this->initializeWidgetClass($bundle_name, $field_name, $widget_name, $entity_id);
 
     // Check we have the variables we initialized.
     $this->assertNotEmpty($vars['bundle'], "Could not load the bundle.");
@@ -56,14 +98,12 @@ class sbo__relationship_widgetTest extends TripalTestCase {
   /**
    * Test the widget Form.
    *
+   * @dataProvider provideEntities()
+   *
    * @group lacey
    */
-  public function testWidgetForm() {
+  public function testWidgetForm($bundle_name, $field_name, $widget_name, $entity_id) {
 
-    $field_name = 'sbo__relationship';
-    $bundle_name = 'bio_data_1';
-    $widget_name = 'sbo__relationship_widget';
-    $entity_id = 8;
     $vars = $this->initializeWidgetClass($bundle_name, $field_name, $widget_name, $entity_id);
 
     // Stub out a fake $widget object.