Kaynağa Gözat

First stab at extracting into a class -tests failing :-(

Lacey Sanderson 6 yıl önce
ebeveyn
işleme
511723641a

+ 143 - 0
tests/TripalFieldTestHelper.php

@@ -0,0 +1,143 @@
+<?php
+/**
+ * This class can be included at the top of your TripalTestCase to facillitate testing
+ * fields, widgets and formatters.
+ */
+class TripalFieldTestHelper {
+
+  // This is for the initialized field, widget or formatter as indicated by
+  // $machine_names in the constructor.
+  public $initialized_class;
+
+  // The name of the class being initialized.
+  public $class_name;
+
+  // Information for the field.
+  public $field_info;
+
+  // Information for the field instance.
+  public $instance_info;
+
+  // The loaded bundle the field is attached to.
+  public $bundle;
+
+  // The entity the field is attached to.
+  public $entity;
+
+  // The type of class we are initializing.
+  // One of 'field', 'widget', or 'formatter'.
+  public $type;
+
+  /**
+   * Create an instance of TripalFieldTestHelper.
+   *
+   * Specifcally, initialize the widget class and save it for further testing.
+   *
+   * @param $bundle_name
+   *   The name of the bundle the field should be part of. This bundle must already exist.
+   * @param $machine_names
+   *   An array of machine names including:
+   *    - field_name: the name of the field (REQUIRED)
+   *    - widget_name: the name of the widget (Only required for widget testing)
+   *    - formatter_name: the name of the formatter (Only required for formatter testing)
+   * @param $field_info (OPTIONAL)
+   *   The Drupal information for the field you want to test. @see getFieldInfo
+   * @param $instance_info (OPTIONAL)
+   *   The Drupal information for the field instance you want to test. @see getInstanceInfo
+   */
+  public function __construct($bundle_name, $machine_names, $entity, $field_info = NULL, $instance_info = NULL) {
+
+    // @debug print "BUNDLE: " .$bundle_name."\n";
+
+    // Save the field information.
+    if (!$field_info) {
+      $field_info = $this->getFieldInfo($machine_names['field_name']);
+    }
+    $this->field_info = $field_info;
+
+    // Save the field instance information.
+    if (!$instance_info) {
+      $instance_info = $this->getFieldInfo($bundle_name, $machine_names['field_name']);
+    }
+    $this->instance_info = $instance_info;
+
+    // Load the bundle.
+    $this->bundle = tripal_load_bundle_entity(array('name'=> $bundle_name));
+
+    // What type of class are we initializing?
+    $this->type = 'field';
+    $this->class_name = $machine_names['field_name'];
+    if (isset($machine_names['widget_name'])) {
+      $this->type = 'widget';
+      $this->class_name = $machine_names['widget_name'];
+    }
+    elseif (isset($machine_names['formatter_name'])) {
+      $this->type = 'formatter';
+      $this->class_name = $machine_names['formatter_name'];
+    }
+
+    // The entity from the specified bundle that the field should be attached to.
+    $this->entity = $entity;
+
+    // @debug print_r($instance_info);
+
+    // Initialize the class.
+    $class_name = '\\' . $this->class_name;
+    $class_path = DRUPAL_ROOT . '/' . drupal_get_path('module', 'tripal_chado')
+      . '/includes/TripalFields/'.$machine_names['field_name'].'/'.$this->class_name.'.inc';
+    if ((include_once($class_path)) == TRUE) {
+      $this->initialized_class = new $class_name($this->field_info, $this->instance_info);
+    }
+
+  }
+
+  /**
+   * Retrieve the initialized class for testing!
+   */
+  public function getInitializedClass() {
+    return $this->initialized_class;
+  }
+
+  /**
+   * Retrieve the field information for a fiven field.
+   * @see https://api.drupal.org/api/drupal/modules%21field%21field.info.inc/function/field_info_field/7.x
+   *
+   * @param $field_name
+   *   The name of the field to retrieve. $field_name can only refer to a
+   *   non-deleted, active field.
+   * @return
+   *   The field array as returned by field_info_field() and used when initializing
+   *   this class.
+   */
+  public function getFieldInfo($field_name) {
+
+    if (empty($this->field_info)) {
+      $this->field_info = field_info_field($field_name);
+    }
+
+    return $this->field_info;
+  }
+
+  /**
+   * Retrieve the field instance information for a fiven field.
+   * @see https://api.drupal.org/api/drupal/modules%21field%21field.info.inc/function/field_info_instance/7.x
+   *
+   * @param $bundle_name
+   *   The name of the bundle you want the field attached to. For example, bio_data_1.
+   * @param $field_name
+   *   The name of the field to retrieve the instance of. $field_name can only refer to a
+   *   non-deleted, active field.
+   * @return
+   *   The field instance array as returned by field_info_instance() and used when
+   *   initializing this class.
+   */
+  public function getInstanceInfo($bundle_name, $field_name) {
+
+    if (empty($this->instance_info)) {
+      $this->instance_info = field_info_instance('TripalEntity', $field_name, $bundle_name);
+    }
+
+    return $this->instance_info;
+  }
+
+}

+ 97 - 75
tests/tripal_chado/fields/sbo__relationship_widgetTest.php

@@ -8,34 +8,6 @@ class sbo__relationship_widgetTest extends TripalTestCase {
   // Uncomment to auto start and rollback db transactions per test method.
   use DBTransaction;
 
-  /**
-   * Create a fake sbo__relationship_widget field?
-   */
-  private function initializeWidgetClass($bundle_name, $field_name, $widget_name, $entity_id) {
-    $vars = [];
-    $vars['widget_class'] = $vars['bundle'] = $vars['entity'] = NULL;
-
-    // First include the appropriate class.
-    $widget_class_path = DRUPAL_ROOT . '/' . drupal_get_path('module', 'tripal_chado')
-      . '/includes/TripalFields/sbo__relationship/sbo__relationship_widget.inc';
-    if ((include_once($widget_class_path)) == TRUE) {
-
-      // Load the bundle and field/instance info.
-      $vars['bundle'] = tripal_load_bundle_entity(array('name'=> $bundle_name));
-      $vars['field_info'] = field_info_field($field_name);
-      $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($vars['field_info'], $vars['instance_info']);
-
-      // load an entity to pretend the widget is modifying.
-      $vars['entity'] = entity_load('TripalEntity', [$entity_id]);
-      $vars['entity'] = $vars['entity'][$entity_id];
-    }
-
-    return $vars;
-  }
-
   /**
    * Data Provider: provides entities matching important test cases.
    *
@@ -46,8 +18,8 @@ class sbo__relationship_widgetTest extends TripalTestCase {
    *  project_relationship: subject_project_id, type_id, object_project_id, rank
    *
    * @returns
-   *   Returns an array where each item to be tested has the paramaters 
-   *   needed for initializeWidgetClass(). Specfically, $bundle_name, 
+   *   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() {
@@ -62,9 +34,9 @@ class sbo__relationship_widgetTest extends TripalTestCase {
        // 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 b 
-         LEFT JOIN tripal_entity e ON e.bundle='bio_data_'||b.bundle_id 
+         SELECT bundle_id
+         FROM chado_bundle b
+         LEFT JOIN tripal_entity e ON e.bundle='bio_data_'||b.bundle_id
          WHERE data_table=:table AND id IS NOT NULL LIMIT 1",
            array(':table' => $base_table))->fetchField();
 
@@ -85,11 +57,13 @@ class sbo__relationship_widgetTest extends TripalTestCase {
          'has_value' => FALSE,
          'subject_key' => 'subject_id',
          'object_key' => 'object_id',
+         'base_table' => $base_table,
+         'relationship_table' => $base_table.'_relationship'
        ];
        if ($base_table == 'organism') { $expect['has_rank'] = FALSE; }
        if ($base_table == 'stock') { $expect['has_value'] = TRUE; }
-       if ($base_table == 'project') { 
-         $expect['subject_key'] = 'subject_project_id'; 
+       if ($base_table == 'project') {
+         $expect['subject_key'] = 'subject_project_id';
          $expect['object_key'] = 'object_project_id';
        }
 
@@ -103,19 +77,40 @@ class sbo__relationship_widgetTest extends TripalTestCase {
    *
    * @dataProvider provideEntities()
    *
-   * @group lacey
+   * @group lacey-wip
+   * @group widget
+   * @group sbo__relationship
    */
   public function testWidgetClassInitialization($bundle_name, $field_name, $widget_name, $entity_id, $expect) {
 
-    // Initialize our variables.
-    $vars = $this->initializeWidgetClass($bundle_name, $field_name, $widget_name, $entity_id);
+    // Load the entity.
+    $entity = entity_load('TripalEntity', [$entity_id]);
+    $entity = $entity[$entity_id];
+
+    // Initialize the widget class via the TripalFieldTestHelper class.
+    $machine_names = array(
+      'field_name' => $field_name,
+      'widget_name' => $widget_name,
+    );
+    module_load_include('php', 'tripal_chado', '../tests/TripalFieldTestHelper');
+    $helper = new \TripalFieldTestHelper($bundle_name, $machine_names, $entity);
+    $widget_class = $helper->getInitializedClass();
 
     // Check we have the variables we initialized.
-    $this->assertNotEmpty($vars['bundle'], "Could not load the bundle.");
-    $this->assertNotEmpty($vars['field_info'], "Could not lookup the field information.");
-    $this->assertNotEmpty($vars['instance_info'], "Could not lookup the instance informatiob.");
-    $this->assertNotEmpty($vars['widget_class'], "Couldn't create a widget class instance.");
-    $this->assertNotEmpty($vars['entity'], "Couldn't load an entity.");
+    $this->assertNotEmpty($helper->bundle,
+      "Could not load the bundle.");
+    $this->assertNotEmpty($helper->getFieldInfo(),
+      "Could not lookup the field information.");
+    $this->assertNotEmpty($helper->getInstanceInfo(),
+      "Could not lookup the instance information.");
+    $this->assertNotEmpty($widget_class,
+      "Couldn't create a widget class instance.");
+    $this->assertNotEmpty($entity,
+      "Couldn't load an entity.");
+
+    // Check a little deeper...
+    $this->assertEquals($helper->instance_info['settings']['chado_table'], $expect['relationship_table'],
+      "Instance settings were not initialized fully.");
 
   }
 
@@ -124,18 +119,31 @@ class sbo__relationship_widgetTest extends TripalTestCase {
    *
    * @dataProvider provideEntities()
    *
-   * @group lacey
+   * @group widget
+   * @group sbo__relationship
    */
   public function testWidgetForm($bundle_name, $field_name, $widget_name, $entity_id, $expect) {
 
-    $vars = $this->initializeWidgetClass($bundle_name, $field_name, $widget_name, $entity_id);
-    $base_table = $vars['entity']->chado_table;
+    // Load the entity.
+    $entity = entity_load('TripalEntity', [$entity_id]);
+    $entity = $entity[$entity_id];
+
+    // Initialize the widget class via the TripalFieldTestHelper class.
+    $machine_names = array(
+      'field_name' => $field_name,
+      'widget_name' => $widget_name,
+    );
+    module_load_include('php', 'tripal_chado', '../tests/TripalFieldTestHelper');
+    $helper = new \TripalFieldTestHelper($bundle_name, $machine_names, $entity);
+    $widget_class = $helper->getInitializedClass();
+
+    $base_table = $entity->chado_table;
 
     // Stub out a fake $widget object.
     $widget = [
       '#entity_type' => 'TripalEntity',
-      '#entity' => $vars['entity'],
-      '#bundle' => $vars['bundle'],
+      '#entity' => $entity,
+      '#bundle' => $helper->bundle,
       '#field_name' => $field_name,
       '#language' => LANGUAGE_NONE,
       '#field_parents' => [],
@@ -149,8 +157,8 @@ class sbo__relationship_widgetTest extends TripalTestCase {
         '#type' => 'value',
         '#value' => '',
       ],
-      '#field' => $vars['field_info'],
-      '#instance' => $vars['instance_info'],
+      '#field' => $helper->field_info,
+      '#instance' => $helper->instance_info,
       '#theme' => 'tripal_field_default',
       'element_validate' => ['tripal_field_widget_form_validate']
     ];
@@ -158,13 +166,13 @@ class sbo__relationship_widgetTest extends TripalTestCase {
     // Stub out the form and form_state.
     $form = [
       '#parents' => [],
-      '#entity' => $vars['entity'],
+      '#entity' => $entity,
     ];
     $form_state = [
       'build_info' => [
         'args' => [
           0 => NULL,
-          1 => $vars['entity']
+          1 => $entity,
         ],
         'form_id' => 'tripal_entity_form',
       ],
@@ -179,11 +187,10 @@ class sbo__relationship_widgetTest extends TripalTestCase {
     $langcode = LANGUAGE_NONE;
     $items = [
       'value' => '',
-      'chado-organism_relationship__organism_relationship_id' => '',
-      'chado-organism_relationship__subject_id' => '',
-      'chado-organism_relationship__object_id' => '',
-      'chado-organism_relationship__type_id' => '',
-      'chado-organism_relationship__rank' => '',
+      'chado-'.$base_table.'_relationship__organism_relationship_id' => '',
+      'chado-'.$base_table.'_relationship__subject_id' => '',
+      'chado-'.$base_table.'_relationship__object_id' => '',
+      'chado-'.$base_table.'_relationship__type_id' => '',
       'object_name' => '',
       'subject_name' => '',
       'type_name' => '',
@@ -193,7 +200,7 @@ class sbo__relationship_widgetTest extends TripalTestCase {
     // Stub out the widget element.
     $element = [
       '#entity_type' => 'TripalEntity',
-      '#entity' => $vars['entity'],
+      '#entity' => $entity,
       '#bundle' => $bundle_name,
       '#field_name' => $field_name,
       '#language' => LANGUAGE_NONE,
@@ -207,26 +214,41 @@ class sbo__relationship_widgetTest extends TripalTestCase {
     ];
 
     // Execute the form method.
-    $vars['widget_class']->form($widget, $form, $form_state, $langcode, $items, $delta, $element);
+    $widget_class->form($widget, $form, $form_state, $langcode, $items, $delta, $element);
 
     // Check the resulting for array
-    $this->assertArrayHasKey('subject_name', $widget, "The form for $bundle_name($base_table) does not have a subject element."); 
-    $this->assertArrayHasKey('type_name', $widget, "The form for $bundle_name($base_table) does not have a type element.");
-    $this->assertArrayHasKey('object_name', $widget, "The form for $bundle_name($base_table) does not have a object element.");
+    $this->assertArrayHasKey('subject_name', $widget,
+      "The form for $bundle_name($base_table) does not have a subject element.");
+    $this->assertArrayHasKey('type_name', $widget,
+      "The form for $bundle_name($base_table) does not have a type element.");
+    $this->assertArrayHasKey('object_name', $widget,
+      "The form for $bundle_name($base_table) does not have a object element.");
+
+    //@ debug print_r($widget);
 
     // Check the subject/object keys were correctly determined.
-    $this->assertEquals($expect['subject_key'], $widget['#subject_id_key'], "The form didn't determine the subject key correctly.");
-    $this->assertEquals($expect['object_key'], $widget['#object_id_key'], "The form didn't determine the object key correctly.");
-
-    // If this table has a rank we want to ensure there is a form element to set it.
-    if ($expect['has_rank']) {
-      $this->assertArrayHasKey('rank', $widget, "The form for $bundle_name($base_table) does not have a rank element and it should.");
-    }
-
-    // If this table has a value we want to ensure there is a form element to set it.
-    // @todo this test may be problematic b/c all fields have values independent of the db.
-    if ($expect['has_value']) { 
-      $this->assertArrayHasKey('value', $widget, "The form for $bundle_name($base_table) does not have a value element and it should.");
-    } 
+    $this->assertEquals($expect['subject_key'], $widget['#subject_id_key'],
+      "The form didn't determine the subject key correctly.");
+    $this->assertEquals($expect['object_key'], $widget['#object_id_key'],
+      "The form didn't determine the object key correctly.");
+  }
+
+  /**
+   * Test the Relationship Type Options.
+   * Specfically, sbo__relationship_widget->get_rtype_select_options().
+   *
+   * @dataProvider provideEntities()
+   *
+   * @group widget
+   * @group sbo__relationship
+   */
+  public function testGetRTypeSelectOptions($bundle_name, $field_name, $widget_name, $entity_id, $expect) {
+
+    // The different options are set in the instance.
+    // Therefore we want to make a fake instance to control this setting.
+    $fake_instance = field_info_instance('TripalEntity', $field_name, $bundle_name);
+    //$fake_instance['settings']['relationships']['option1_vocabs'] = 5;
+
+    $this->assertTrue(true);
   }
 }