Skip to content

Commit

Permalink
Change 'Datasets' field on Resources form to use autocomplete (#2035)
Browse files Browse the repository at this point in the history
* Changed dataset field on resource form to be autocomplete

* Fixed code style errors

* Fixed automatic reference to datasets

* Update CHANGELOG.txt

* Update dkan_dataset.forms.inc

* Refactored step used to fill in autocomplete fields

* Fixed tests

* Removed unused step

* Added javascript tagas

* Fixed tests
  • Loading branch information
MSolv authored and dkinzer committed Jul 27, 2017
1 parent c979205 commit 20f4629
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 121 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
@@ -1,5 +1,6 @@
7.x-1.13.6
----------
- #2035 Change 'Datasets' field on Resources form to use autocomplete.
- #2034 Added language selectors on 'chosen_jquery_selector' configuration.
- #2018 Additional test/devops improvements needed for deployment. See #2012, #2014, #2015 and #2016 for specifics.

Expand Down
16 changes: 3 additions & 13 deletions modules/dkan/dkan_dataset/dkan_dataset.forms.inc
Expand Up @@ -299,6 +299,7 @@ function dkan_dataset_form_alter(&$form, &$form_state, $form_id) {

$form['#validate'][] = 'dkan_dataset_dataset_node_form_validate';
}

if ($form_id == 'resource_node_form') {
drupal_add_library('system', 'jquery.bbq');
$form['#attached']['css'][] = drupal_get_path('module', 'dkan_dataset') . '/css/dkan_dataset.css';
Expand All @@ -313,11 +314,6 @@ function dkan_dataset_form_alter(&$form, &$form_state, $form_id) {
// Get langcode for field_dataset_ref.
$field_dataset_ref_langcode = dkan_dataset_form_field_language($form, 'field_dataset_ref');
$form['field_dataset_ref'][$field_dataset_ref_langcode]['#description'] = t('Dataset that this resource is attached to.');
$dataset_options = $form['field_dataset_ref'][$field_dataset_ref_langcode]['#options'];
$fix_apostrophes = function ($value) {
return html_entity_decode($value, ENT_QUOTES, "UTF-8");
};
$form['field_dataset_ref'][$field_dataset_ref_langcode]['#options'] = array_map($fix_apostrophes, $dataset_options);

// Get langcode for field_upload.
$field_upload_langcode = dkan_dataset_form_field_language($form, 'field_upload');
Expand Down Expand Up @@ -352,18 +348,13 @@ function dkan_dataset_form_alter(&$form, &$form_state, $form_id) {

if ($query = drupal_get_query_parameters()) {
if (isset($query['dataset'])) {
$form['field_dataset_ref'][$field_dataset_ref_langcode]['#default_value'] = $query['dataset'];
$selected_dataset = node_load($query['dataset']);
$form['field_dataset_ref'][$field_dataset_ref_langcode][0]['target_id']['#default_value'] = $selected_dataset->title . ' (' . $query['dataset'] . ')';
$form['prefix']['#type'] = 'item';
$form['prefix']['#weight'] = '-20';
$form['prefix']['#markup'] = dkan_form_create_stages('resource-create', $query['dataset']);
}
}
elseif (isset($form['#node']->field_dataset_ref['nid'])) {
$form['field_dataset_ref'][$field_dataset_ref_langcode]['#default_value'] = $form['#node']->field_dataset_ref['nid'];
$form['prefix']['#type'] = 'item';
$form['prefix']['#weight'] = '-20';
$form['prefix']['#markup'] = dkan_form_create_stages('resource-create', $form['#node']->field_dataset_ref['nid']);
}
$form['#validate'][] = 'dkan_dataset_resource_node_form_validate';
// This saves us on the ajax reloading.
if (isset($query['dataset'])) {
Expand Down Expand Up @@ -628,7 +619,6 @@ function dkan_dataset_resource_node_form_validate($form, &$form_state) {
// Check for geojson.
if ($type == 'json' && isset($file)) {
$response = drupal_http_request($file->uri, array('timeout' => 5));
// dpm($response);
if ($response->code == '200') {
$data = $response->data;
$decoded = drupal_json_decode($data);
Expand Down
Expand Up @@ -1519,11 +1519,14 @@ uk-ogl|UK Open Government Licence (OGL)',
),
'widget' => array(
'active' => 1,
'module' => 'options',
'module' => 'entityreference',
'settings' => array(
'apply_chosen' => 0,
'match_operator' => 'CONTAINS',
'path' => '',
'size' => 60,
),
'type' => 'options_select',
'type' => 'entityreference_autocomplete',
'weight' => 8,
),
);
Expand Down
80 changes: 33 additions & 47 deletions test/dkanextension/src/Drupal/DKANExtension/Context/DKANContext.php
Expand Up @@ -407,23 +407,6 @@ public function iWaitForSeconds($milliseconds) {
}




/**
* Selects option in select field with specified by node title.
*
* @When /^(?:|I )select node named "(?P<option>(?:[^"]|\\")*)" from "(?P<select>(?:[^"]|\\")*)"$/
*/
// public function selectNodeOption($select, $option)
// {
// $this->drushContext->assertDrushCommandWithArgument('php-eval', "\"return db_query('SELECT nid FROM node WHERE title = \'$option\'')->fetchField();\"");
// $option = $this->drushContext->readDrushOutput();
// $option = trim(str_replace(array("'"), "", $option));
// $select = $this->fixStepArgument($select);
// $option = $this->fixStepArgument($option);
// $this->getSession()->getPage()->selectFieldOption($select, $option);
// }

/**
* Properly inputs item in field rendered by Chosen.js.
*
Expand Down Expand Up @@ -588,36 +571,6 @@ public function iClickMapIcon($num) {
$element->click();
}

/**
* Copy of "I fill in" but doesn't escape "(".
*
* When using "I fill in" it escaped autocomplete fields node id. Just using
* the title wouldn't work. The following focuses on the field and selects
* the first dropdown.
*
* @Given /^I fill in the autocomplete field "([^"]*)" with "([^"]*)"$/
*/
public function iFillInTheAutoFieldWith($field, $value) {
$session = $this->getSession();
$field = $this->fixStepArgument($field);
$value = $this->fixStepArgument($value);
$input_title = $session->getPage()->find(
'xpath',
$session->getSelectorsHandler()->selectorToXpath('xpath', '//input[@value="' . $field . '"]')

);
$input_title->click();
$this->iWaitForSeconds(2);
// Selects the first dropdown since there is no id or other way to
// reference the desired entry.
$title = $session->getPage()->find(
'xpath',
$session->getSelectorsHandler()->selectorToXpath('xpath', '//li[.="' . $value . '"]')

);
$title->click();
}

/**
* @Given /^I empty the field "([^"]*)"$/
*/
Expand Down Expand Up @@ -1054,6 +1007,39 @@ public function iVisitTheLink($selector) {
$session->visit($this->locatePath($url));
}


/**
* @Given /^I fill in the autocomplete field "([^"]*)" with "([^"]*)"$/
*
* Fill in the 'Autocomplete' field on a form.
*/
public function iFillInTheAutocompleteFieldWith($field, $value) {
$session = $this->getSession();
$page = $session->getPage();
$element = $page->findField($field);
if (!$element) {
throw new ElementNotFoundException($session, NULL, 'named', $field);
}
$page->fillField($field, $value);
// Trigger all needed key events in order for the autocomplete to be triggered.
// Just filling the field with a value is not enough.
// TODO: Is there a better way to do this?
$chars = str_split($value);
$last_char = array_pop($chars);
// Delete last char.
$session->getDriver()->keyDown($element->getXpath(), 8);
$session->getDriver()->keyUp($element->getXpath(), 8);
// Re-add last char.
$session->getDriver()->keyDown($element->getXpath(), $last_char);
$session->getDriver()->keyUp($element->getXpath(), $last_char);
$this->iWaitForSeconds(5);
$title = $page->find(
'xpath',
$session->getSelectorsHandler()->selectorToXpath('xpath', '//li[.="' . $value . '"]')
);
$title->click();
}

/************************************/
/* Gravatar */
/************************************/
Expand Down
Expand Up @@ -194,41 +194,6 @@ public function iEmptyTheResourcesField($locator) {
}
}

/**
* @Given /^I fill in the resources field "([^"]*)" with "([^"]*)"$/
*
* Fill in the 'Resources' field on a Dataset form.
*/
public function iFillInTheResourcesFieldWith($field, $value) {
$session = $this->getSession();
$page = $session->getPage();

$element = $page->findField($field);
if (!$element) {
throw new ElementNotFoundException($session, NULL, 'named', $field);
}
$page->fillField($field, $value);

// Trigger all needed key events in order for the autocomplete to be triggered.
// Just filling the field with a value is not enough.
// TODO: Is there a better way to do this?
$chars = str_split($value);
$last_char = array_pop($chars);
// Delete last char.
$session->getDriver()->keyDown($element->getXpath(), 8);
$session->getDriver()->keyUp($element->getXpath(), 8);
// Re-add last char.
$session->getDriver()->keyDown($element->getXpath(), $last_char);
$session->getDriver()->keyUp($element->getXpath(), $last_char);
$this->dkanContext->iWaitForSeconds(5);

$title = $page->find(
'xpath',
$session->getSelectorsHandler()->selectorToXpath('xpath', '//li[.="' . $value . '"]')
);
$title->click();
}

/**
* @Then I should see all published datasets
*/
Expand Down
10 changes: 5 additions & 5 deletions test/features/dataset.author.feature
Expand Up @@ -142,7 +142,7 @@ Feature: Dataset Features
Given I am logged in as "Katie"
And I am on "Dataset 06" page
When I click "Edit"
And I fill in the resources field "edit-field-resources-und-0-target-id" with "Resource 04"
And I fill in the autocomplete field "edit-field-resources-und-0-target-id" with "Resource 04"
And I press "Finish"
Then I should see "Dataset 06 has been updated"
And I should see "Groups were updated on 1 resource(s)"
Expand All @@ -158,7 +158,7 @@ Feature: Dataset Features
Given I am logged in as "Katie"
And I am on "Dataset 06" page
When I click "Edit"
And I fill in the resources field "edit-field-resources-und-0-target-id" with "Resource 04"
And I fill in the autocomplete field "edit-field-resources-und-0-target-id" with "Resource 04"
And I press "Finish"
Then I should see "Dataset 06 has been updated"
And I should see "Resource 04" in the "dataset resource list" region
Expand All @@ -176,7 +176,7 @@ Feature: Dataset Features
Given I am logged in as "Katie"
And I am on "Dataset 07" page
When I click "Edit"
And I fill in the resources field "edit-field-resources-und-0-target-id" with "Resource 04"
And I fill in the autocomplete field "edit-field-resources-und-0-target-id" with "Resource 04"
And I press "Finish"
Then I should see "Dataset 07 has been updated"
And I should see "Groups were updated on 1 resource(s)"
Expand All @@ -189,7 +189,7 @@ Feature: Dataset Features
Given I am logged in as "Katie"
And I am on "Dataset 07" page
When I click "Edit"
And I fill in the resources field "edit-field-resources-und-0-target-id" with "Resource 04"
And I fill in the autocomplete field "edit-field-resources-und-0-target-id" with "Resource 04"
And I press "Finish"
Then I should see "Dataset 07 has been updated"
And I should see "Groups were updated on 1 resource(s)"
Expand Down Expand Up @@ -226,7 +226,7 @@ Feature: Dataset Features
And I am on "Dataset 08" page
When I click "Edit"
And I fill in the chosen field "edit_og_group_ref_und_chosen" with "Group 02"
And I fill in the resources field "edit-field-resources-und-0-target-id" with "Resource 04"
And I fill in the autocomplete field "edit-field-resources-und-0-target-id" with "Resource 04"
And I press "Finish"
Then I should see "Dataset 08 has been updated"
And I should see "Groups were updated on 1 resource(s)"
Expand Down
42 changes: 23 additions & 19 deletions test/features/resource.author.feature
Expand Up @@ -119,24 +119,24 @@ Feature: Resource
And I press "Delete"
Then I should see "Resource 02 has been deleted"

@dkanBug @noworkflow
@dkanBug @noworkflow @javascript
Scenario: Change dataset on resource
Given I am logged in as "Katie"
And I am on "Resource 01" page
When I click "Edit"
And I select "Dataset 02" from "Dataset"
And I fill in the autocomplete field "edit-field-dataset-ref-und-0-target-id" with "Dataset 02"
And I press "Save"
Then I should see "Resource 01 has been updated"
When I click "Back to dataset"
Then I should see "Dataset 02" in the "dataset title" region
And I should see "Resource 01" in the "dataset resource list" region

@noworkflow
@noworkflow @javascript
Scenario: Add a resource with no datasets to a dataset with no resource
Given I am logged in as "Katie"
And I am on "Resource 06" page
When I click "Edit"
And I select "Dataset 03" from "Dataset"
And I fill in the autocomplete field "edit-field-dataset-ref-und-0-target-id" with "Dataset 03"
And I press "Save"
Then I should see "Resource 06 has been updated"
And I should see "Groups were updated on 1 resource(s)"
Expand All @@ -149,18 +149,18 @@ Feature: Resource
Given I am logged in as "Katie"
And I am on "Resource 07" page
When I click "Edit"
And I select "- None -" from "Dataset"
And I fill in "edit-field-dataset-ref-und-0-target-id" with ""
And I press "Save"
Then I should see "Resource 07 has been updated"
And I should see "Groups were updated on 1 resource(s)"
And I should not see the link "Back to dataset"

@noworkflow
@noworkflow @javascript
Scenario: Add a resource with no group to a dataset with group
Given I am logged in as "Katie"
And I am on "Resource 06" page
When I click "Edit"
And I select "Dataset 05" from "Dataset"
And I fill in the autocomplete field "edit-field-dataset-ref-und-0-target-id" with "Dataset 05"
And I press "Save"
Then I should see "Resource 06 has been updated"
And I should see "Groups were updated on 1 resource(s)"
Expand All @@ -170,51 +170,55 @@ Feature: Resource
Given I am logged in as "Katie"
And I am on "Resource 08" page
When I click "Edit"
And I select "- None -" from "Dataset"
And I fill in "edit-field-dataset-ref-und-0-target-id" with ""
And I press "Save"
Then I should see "Resource 08 has been updated"
And I should see "Groups were updated on 1 resource(s)"
When I am on "Dataset 05" page
Then I should not see "Resource 08" in the "dataset resource list" region

@noworkflow
@noworkflow @javascript
Scenario: Add a resource to multiple datasets with groups
Given I am logged in as "Katie"
And I am on "Resource 06" page
When I click "Edit"
And I select "Dataset 05" from "Dataset"
And I additionally select "Dataset 06" from "Dataset"
And I fill in the autocomplete field "edit-field-dataset-ref-und-0-target-id" with "Dataset 05"
And I press "Add another item"
And I fill in the autocomplete field "edit-field-dataset-ref-und-1-target-id" with "Dataset 06"
And I press "Save"
Then I should see "Resource 06 has been updated"
And I should see "Groups were updated on 1 resource(s)"

@noworkflow
@noworkflow @javascript
Scenario: Remove one dataset with group from resource with multiple datasets
Given I am logged in as "Katie"
And I am on "Resource 06" page
When I click "Edit"
And I select "Dataset 05" from "Dataset"
And I additionally select "Dataset 06" from "Dataset"
And I fill in the autocomplete field "edit-field-dataset-ref-und-0-target-id" with "Dataset 05"
And I press "Add another item"
And I fill in the autocomplete field "edit-field-dataset-ref-und-1-target-id" with "Dataset 06"
And I press "Save"
Then I should see "Resource 06 has been updated"
When I click "Edit"
And I select "Dataset 05" from "Dataset"
And I fill in "edit-field-dataset-ref-und-0-target-id" with ""
And I press "Save"
Then I should see "Resource 06 has been updated"
And I should see "Groups were updated on 1 resource(s)"

@noworkflow
@noworkflow @javascript
Scenario: Remove all datasets with groups from resource
Given I am logged in as "Katie"
And I am on "Resource 06" page
When I click "Edit"
And I select "Dataset 05" from "Dataset"
And I additionally select "Dataset 06" from "Dataset"
And I fill in the autocomplete field "edit-field-dataset-ref-und-0-target-id" with "Dataset 05"
And I press "Add another item"
And I fill in the autocomplete field "edit-field-dataset-ref-und-1-target-id" with "Dataset 06"
And I press "Save"
Then I should see "Resource 06 has been updated"
And I should see "Groups were updated on 1 resource(s)"
When I click "Edit"
And I select "- None -" from "Dataset"
And I fill in "edit-field-dataset-ref-und-0-target-id" with ""
And I fill in "edit-field-dataset-ref-und-1-target-id" with ""
And I press "Save"
Then I should see "Resource 06 has been updated"
And I should see "Groups were updated on 1 resource(s)"
Expand Down

0 comments on commit 20f4629

Please sign in to comment.