Browse Source

Relationship tests working!

Lacey Sanderson 6 years ago
parent
commit
1de569eb27

+ 4 - 0
tests/TripalFieldTestHelper.php

@@ -28,6 +28,9 @@ class TripalFieldTestHelper {
   // One of 'field', 'widget', or 'formatter'.
   public $type;
 
+  // The name of the field for the class being tested.
+  public $field_name;
+
   /**
    * Create an instance of TripalFieldTestHelper.
    *
@@ -60,6 +63,7 @@ class TripalFieldTestHelper {
       $this->type = 'formatter';
       $this->class_name = $machine_names['formatter_name'];
     }
+    $this->field_name = $machine_names['field_name'];
 
     $class_name = '\\' . $this->class_name;
     $class_path = DRUPAL_ROOT . '/' . drupal_get_path('module', 'tripal_chado')

+ 6 - 29
tests/tripal_chado/fields/sbo__relationship_widgetTest.php

@@ -278,7 +278,7 @@ class sbo__relationship_widgetTest extends TripalTestCase {
             break;
           case 'no_type':
             $values['type_name'] = '';
-            $values['vocabulary'] = 0;
+            $values['vocabulary'] = NULL;
             $values[ 'chado-'.$base_table.'_relationship__type_id' ] = NULL;
             $expect['num_errors'] = 1;
             break;
@@ -325,6 +325,7 @@ class sbo__relationship_widgetTest extends TripalTestCase {
     $widget_class = $helper->getInitializedClass();
 
     // Mock objects.
+    $field_name = $info['field_name'];
     $delta = 1;
     $langcode = LANGUAGE_NONE;
     $widget = $helper->mockElement($delta, $langcode);
@@ -332,7 +333,7 @@ class sbo__relationship_widgetTest extends TripalTestCase {
     $form_state = $helper->mockFormState($delta, $langcode, $initial_values);
     $element = $helper->mockElement($delta, $langcode);
 
-    $return = $widget_class->validate($element, $form, $form_state, $langcode, $delta);
+    $widget_class->validate($element, $form, $form_state, $langcode, $delta);
 
     // @debug print_r($form_state['values'][$field_name][$langcode][$delta]);
 
@@ -355,42 +356,18 @@ class sbo__relationship_widgetTest extends TripalTestCase {
 
     // Check for errors.
     $errors = form_get_errors();
+    // @debug print "Errors: " . print_r($errors, TRUE)."\n";
 
     if ($expect['num_errors'] === 0) {
       $this->assertEmpty($errors,
         "There should be no form errors for the following initial values: ".print_r($initial_values,TRUE)." But these were registered: ".print_r($errors, TRUE));
-
-      print_r($return);
-    }
-    elseif (sizeof($errors) > 1) {
-      $this->assertEquals(sizeof($errors), $expect['num_errors'],
-        "The number of errors didn't match what we expectedfor the following initial values: ".print_r($initial_values,TRUE)." Here are the errors: ".print_r($errors, TRUE));
     }
     else {
-      $this->assertEquals(sizeof($errors), $expect['num_errors'],
-        "There were no errors even when we expected some for the following initial values: ".print_r($initial_values,TRUE));
+      $this->assertEquals($expect['num_errors'], sizeof($errors),
+        "The number of errors didn't match what we expected for the following initial values: ".print_r($initial_values,TRUE)." Here are the errors: ".print_r($errors, TRUE));
     }
 
     // Clean up after ourselves by removing any errors we logged.
     form_clear_error();
   }
-
-  /**
-   * 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);
-  }*/
 }

+ 3 - 12
tripal_chado/includes/TripalFields/sbo__relationship/sbo__relationship_widget.inc

@@ -324,8 +324,6 @@ class sbo__relationship_widget extends ChadoFieldWidget {
     $base_table = $this->instance['settings']['base_table'];
     $chado_record_id = array_key_exists('#entity', $element)? $element['#entity']->chado_record_id : NULL;
 
-    // @debug print_r($form_state[$field_name][$langcode][$delta]);
-
     $fkeys = $this->schema['foreign keys'];
 
     // 'nd_reagent_relationship' and 'project_relationship' have different column names from
@@ -346,15 +344,14 @@ class sbo__relationship_widget extends ChadoFieldWidget {
     // Validation:
     //------------
     // If the row is empty then skip this one, there's nothing to validate.
-    if (!($type_id OR $type_name) && !$subject_name && !$object_name) {
-      print "We are never testing the empty row... so we shouldn't be here.\n";
+    if (!($type_id || $type_name) && !$subject_name && !$object_name) {
+      print "HERE; Type Name: $type_name, Type ID: $type_id, Subject Name: $subject_name, Object Name: $object_name\n";
       return;
     }
 
     // Do not proceed if subject ID or object ID does not exist
     if (!key_exists($subject_id_key, $fkeys[$base_table]['columns']) ||
         !key_exists($object_id_key, $fkeys[$base_table]['columns'])) {
-      print "Keys should always exist for not here either.\n";
       return;
     }
 
@@ -371,13 +368,7 @@ class sbo__relationship_widget extends ChadoFieldWidget {
             form_set_error('sbo__relationship]['.$langcode.']['.$delta.'][subject_name', $error['message']);
             break;
           case 'type':
-            if (isset($element['vocabulary'])) {
-              form_set_error('sbo__relationship]['.$langcode.']['.$delta.'][type_name', $error['message']);
-              form_set_error('sbo__relationship]['.$langcode.']['.$delta.'][vocabulary', '');
-            }
-            elseif (isset($element['type_id'])) {
-              form_set_error('sbo__relationship]['.$langcode.']['.$delta.'][type_id', $error['message']);
-            }
+            form_set_error('sbo__relationship]['.$langcode.']['.$delta, $error['message']);
             break;
           case 'object':
             form_set_error('sbo__relationship]['.$langcode.']['.$delta.'][object_name', $error['message']);