|
@@ -79,7 +79,6 @@ class sbo__relationship_widgetTest extends TripalTestCase {
|
|
|
*
|
|
|
* @dataProvider provideEntities()
|
|
|
*
|
|
|
- * @group lacey-wip
|
|
|
* @group widget
|
|
|
* @group sbo__relationship
|
|
|
*/
|
|
@@ -143,52 +142,14 @@ class sbo__relationship_widgetTest extends TripalTestCase {
|
|
|
|
|
|
$base_table = $entity->chado_table;
|
|
|
|
|
|
- // Stub out a fake $widget object.
|
|
|
- $widget = [
|
|
|
- '#entity_type' => 'TripalEntity',
|
|
|
- '#entity' => $entity,
|
|
|
- '#bundle' => $helper->bundle,
|
|
|
- '#field_name' => $field_name,
|
|
|
- '#language' => LANGUAGE_NONE,
|
|
|
- '#field_parents' => [],
|
|
|
- '#columns' => [],
|
|
|
- '#title' => '',
|
|
|
- '#description' => '',
|
|
|
- '#required' => FALSE,
|
|
|
- '#delta' => 0,
|
|
|
- '#weight' => 0, //same as delta.
|
|
|
- 'value' => [
|
|
|
- '#type' => 'value',
|
|
|
- '#value' => '',
|
|
|
- ],
|
|
|
- '#field' => $helper->field_info,
|
|
|
- '#instance' => $helper->instance_info,
|
|
|
- '#theme' => 'tripal_field_default',
|
|
|
- 'element_validate' => ['tripal_field_widget_form_validate']
|
|
|
- ];
|
|
|
-
|
|
|
- // Stub out the form and form_state.
|
|
|
- $form = [
|
|
|
- '#parents' => [],
|
|
|
- '#entity' => $entity,
|
|
|
- ];
|
|
|
- $form_state = [
|
|
|
- 'build_info' => [
|
|
|
- 'args' => [
|
|
|
- 0 => NULL,
|
|
|
- 1 => $entity,
|
|
|
- ],
|
|
|
- 'form_id' => 'tripal_entity_form',
|
|
|
- ],
|
|
|
- 'rebuild' => FALSE,
|
|
|
- 'rebuild_info' => [],
|
|
|
- 'redirect' => NULL,
|
|
|
- 'temporary' => [],
|
|
|
- 'submitted' => FALSE,
|
|
|
- ];
|
|
|
-
|
|
|
- // stub out the data for the field.
|
|
|
+ // Stub out a fake objects.
|
|
|
+ $delta = 1;
|
|
|
$langcode = LANGUAGE_NONE;
|
|
|
+ $widget = $helper->mockElement($delta, $langcode);
|
|
|
+ $form = $helper->mockForm($delta, $langcode);
|
|
|
+ $form_state = $helper->mockFormState($delta, $langcode);
|
|
|
+ $element = $helper->mockElement($delta, $langcode);
|
|
|
+
|
|
|
$items = [
|
|
|
'value' => '',
|
|
|
'chado-'.$base_table.'_relationship__organism_relationship_id' => '',
|
|
@@ -199,23 +160,6 @@ class sbo__relationship_widgetTest extends TripalTestCase {
|
|
|
'subject_name' => '',
|
|
|
'type_name' => '',
|
|
|
];
|
|
|
- $delta = 0;
|
|
|
-
|
|
|
- // Stub out the widget element.
|
|
|
- $element = [
|
|
|
- '#entity_type' => 'TripalEntity',
|
|
|
- '#entity' => $entity,
|
|
|
- '#bundle' => $bundle_name,
|
|
|
- '#field_name' => $field_name,
|
|
|
- '#language' => LANGUAGE_NONE,
|
|
|
- '#field_parents' => [],
|
|
|
- '#columns' => [],
|
|
|
- '#title' => '',
|
|
|
- '#description' => '',
|
|
|
- '#required' => FALSE,
|
|
|
- '#delta' => 0,
|
|
|
- '#weight' => 0,
|
|
|
- ];
|
|
|
|
|
|
// Execute the form method.
|
|
|
$widget_class->form($widget, $form, $form_state, $langcode, $items, $delta, $element);
|
|
@@ -228,13 +172,150 @@ class sbo__relationship_widgetTest extends TripalTestCase {
|
|
|
$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.");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * DataProvider: Provides datasets to validate.
|
|
|
+ */
|
|
|
+ public function provideThings2Validate() {
|
|
|
+ $data = [];
|
|
|
+
|
|
|
+ foreach (['organism', 'stock', 'project'] as $base_table) {
|
|
|
+
|
|
|
+ $base_table = $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 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();
|
|
|
+
|
|
|
+ if (!$bundle_id) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ $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();
|
|
|
+ $entity = entity_load('TripalEntity', [ $entity_id ]);
|
|
|
+ $entity = $entity[ $entity_id ];
|
|
|
+
|
|
|
+ $values = [
|
|
|
+ 'subject_name' => 'Fake Non-existant Name '.uniqid(),
|
|
|
+ 'type_name' => 'organism',
|
|
|
+ 'vocabulary' => 47,
|
|
|
+ 'object_name' => $entity->chado_record->name,
|
|
|
+ ];
|
|
|
+ if ($base_table == 'organism') {
|
|
|
+ $values['object_name'] = $entity->chado_record->species;
|
|
|
+ }
|
|
|
+
|
|
|
+ // set variables to guide testing.
|
|
|
+ $expect = [
|
|
|
+ 'has_rank' => TRUE,
|
|
|
+ 'has_value' => FALSE,
|
|
|
+ 'subject_key' => 'subject_id',
|
|
|
+ 'object_key' => 'object_id',
|
|
|
+ 'base_table' => $base_table,
|
|
|
+ 'relationship_table' => $base_table.'_relationship',
|
|
|
+ 'num_errors' => 0,
|
|
|
+ ];
|
|
|
+ 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';
|
|
|
+ $expect['object_key'] = 'object_project_id';
|
|
|
+ }
|
|
|
+
|
|
|
+ $data[] = [
|
|
|
+ [
|
|
|
+ 'field_name' => $field_name,
|
|
|
+ 'widget_name' => $widget_name,
|
|
|
+ 'bundle_id' => $bundle_id,
|
|
|
+ 'bundle_name' => $bundle_name,
|
|
|
+ ],
|
|
|
+ $entity,
|
|
|
+ $values,
|
|
|
+ $expect,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test sbo__relationship_widget->validate().
|
|
|
+ *
|
|
|
+ * @dataProvider provideThings2Validate()
|
|
|
+ *
|
|
|
+ * @group lacey-wip
|
|
|
+ * @group widget
|
|
|
+ * @group sbo__relationship
|
|
|
+ */
|
|
|
+ public function testWidgetValidate($info, $entity, $initial_values, $expect) {
|
|
|
+
|
|
|
+ $base_table = $entity->chado_table;
|
|
|
+
|
|
|
+ // Initialize the widget class via the TripalFieldTestHelper class.
|
|
|
+ $machine_names = array(
|
|
|
+ 'field_name' => $info['field_name'],
|
|
|
+ 'widget_name' => $info['widget_name'],
|
|
|
+ );
|
|
|
+ $field_info = field_info_field($info['field_name']);
|
|
|
+ $instance_info = field_info_instance('TripalEntity', $info['field_name'], $info['bundle_name']);
|
|
|
+ $helper = new \TripalFieldTestHelper($info['bundle_name'], $machine_names, $entity, $field_info, $instance_info);
|
|
|
+ $widget_class = $helper->getInitializedClass();
|
|
|
+
|
|
|
+ // Mock objects.
|
|
|
+ $delta = 1;
|
|
|
+ $langcode = LANGUAGE_NONE;
|
|
|
+ $widget = $helper->mockElement($delta, $langcode);
|
|
|
+ $form = $helper->mockForm($delta, $langcode);
|
|
|
+ $form_state = $helper->mockFormState($delta, $langcode, $initial_values);
|
|
|
+ $element = $helper->mockElement($delta, $langcode);
|
|
|
+ $widget_class->validate($element, $form, $form_state, $langcode, $delta);
|
|
|
+
|
|
|
+ // @debug print_r($form_state['values'][$field_name][$langcode][$delta]);
|
|
|
+
|
|
|
+ // Ensure the chado-table__column entries are there.
|
|
|
+ $this->assertArrayHasKey(
|
|
|
+ 'chado-'.$base_table.'_relationship__'.$expect['subject_key'],
|
|
|
+ $form_state['values'][$field_name][$langcode][$delta],
|
|
|
+ 'Failed to find the subject_id in the processed values (Base: '.$base_table.'). This implies the validate function was not able to validate the subject.'
|
|
|
+ );
|
|
|
+ $this->assertArrayHasKey(
|
|
|
+ 'chado-'.$base_table.'_relationship__'.$expect['object_key'],
|
|
|
+ $form_state['values'][$field_name][$langcode][$delta],
|
|
|
+ 'Failed to find the object_id in the processed values (Base: '.$base_table.'). This implies the validate function was not able to validate the object.'
|
|
|
+ );
|
|
|
+ $this->assertArrayHasKey(
|
|
|
+ 'chado-'.$base_table.'_relationship__type_id',
|
|
|
+ $form_state['values'][$field_name][$langcode][$delta],
|
|
|
+ 'Failed to find the type_id in the processed values (Base: '.$base_table.'). This implies the validate function was not able to validate the type.'
|
|
|
+ );
|
|
|
+
|
|
|
+ // Check for errors.
|
|
|
+ $errors = form_get_errors();
|
|
|
+
|
|
|
+ $this->assertEmpty($errors,
|
|
|
+ "There should be no form errors! But these were registered: ".print_r($errors, TRUE));
|
|
|
+
|
|
|
+ // Clean up after ourselves by removing any errors we logged.
|
|
|
+ form_clear_error();
|
|
|
}
|
|
|
|
|
|
/**
|