Commit 030bd88a authored by Robert Lyon's avatar Robert Lyon

Bug 1512147: Fixing behat to allow select2 via ajax

Includes fixing up how we access select2 both via ajax and for
existing list.

Have added a simple behat test for sending a message as that uses the
select2 in an ajax way

Change-Id: I3aa6722169c61e4fc2ad196a02fa5bb696761210
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
parent 3db4ecaf
...@@ -93,13 +93,13 @@ class BehatForms extends BehatBase { ...@@ -93,13 +93,13 @@ class BehatForms extends BehatBase {
} }
/** /**
* Selects a list of values in a select2 multiple-select field * Fills in Select2 field with specified
* *
* @When /^I set the select2 field "(?P<field>(?:[^"]|\\")*)" to "(?P<value>(?:[^"]|\\")*)"$/ * @When /^(?:|I )set the select2 field "(?P<field>(?:[^"]|\\")*)" to "(?P<textValues>(?:[^"]|\\")*)"$/
* @When /^(?:|I )set the select2 value "(?P<textValues>(?:[^"]|\\")*)" for "(?P<field>(?:[^"]|\\")*)"$/
*/ */
public function i_set_the_select2_field_to($field, $textValues) { public function iFillInSelect2Field($field, $textValues) {
$page = $this->getSession()->getPage(); $page = $this->getSession()->getPage();
$values = []; $values = [];
foreach(preg_split('/,\s*/', $textValues) as $value) { foreach(preg_split('/,\s*/', $textValues) as $value) {
$option = $page->find('xpath', '//select[@id="' . $field . '"]//option[text()="' . $value . '"]'); $option = $page->find('xpath', '//select[@id="' . $field . '"]//option[text()="' . $value . '"]');
...@@ -109,6 +109,88 @@ class BehatForms extends BehatBase { ...@@ -109,6 +109,88 @@ class BehatForms extends BehatBase {
$values = json_encode($values); $values = json_encode($values);
$this->getSession()->executeScript("jQuery('#{$field}').val({$values}).trigger('change');"); $this->getSession()->executeScript("jQuery('#{$field}').val({$values}).trigger('change');");
} }
/**
* Fill Select2 input field
*
* @When /^(?:|I )fill in select2 input "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)"$/
*/
public function iFillInSelect2InputWith($field, $value) {
$page = $this->getSession()->getPage();
$this->select2OpenField($page, $field);
$this->select2FillSearchField($page, $field, $value);
}
/**
* Fill Select2 input field and select a value
*
* @When /^(?:|I )fill in select2 input "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)" and select "(?P<entry>(?:[^"]|\\")*)"$/
*/
public function iFillInSelect2InputWithAndSelect($field, $value, $entry) {
$page = $this->getSession()->getPage();
$this->select2OpenField($page, $field);
$this->select2FillSearchField($page, $field, $value);
$this->select2Value($page, $field, $entry);
$this->getSession()->executeScript("jQuery('#{$field}').trigger('change');");
}
/**
* Open Select2 choice list
*
* @param DocumentElement $page
* @param string $field
* @throws \Exception
*/
private function select2OpenField($page, $field) {
$fieldName = sprintf('#%s', $field);
$inputField = $page->find('css', $fieldName);
if (!$inputField) {
throw new \Exception(sprintf('No field "%s" found', $field));
}
$choice = $inputField->getParent()->find('css', '.select2-selection');
if (!$choice) {
throw new \Exception(sprintf('No select2 choice found for "%s"', $field));
}
$choice->press();
}
/**
* Fill Select2 search field
*
* @param DocumentElement $page
* @param string $field
* @param string $value
* @throws \Exception
*/
private function select2FillSearchField($page, $field, $value) {
$driver = $this->getSession()->getDriver();
if ('MaharaSelenium2Driver' === get_class($driver)) {
// Can't use $this->getSession()->getPage()->find() because of https://github.com/minkphp/MinkSelenium2Driver/issues/188
$select2Input = $this->getSession()->getDriver()->getWebDriverSession()->element('xpath', "//html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' select2-search__field ')]");
if (!$select2Input) {
throw new \Exception(sprintf('No field "%s" found', $field));
}
$select2Input->postValue(['value' => [$value]]);
}
else {
throw new \Exception(sprintf('Not able to select via ajax. Need MaharaSelenium2Driver'));
}
$this->getSession()->wait(10000, "(jQuery('#select2-{$field}-results .loading-results').length === 0)");
}
/**
* Select value in choice list
*
* @param DocumentElement $page
* @param string $field
* @param string $value
* @throws \Exception
*/
private function select2Value($page, $field, $value) {
$chosenResults = $page->findAll('css', '.select2-results ul li');
foreach ($chosenResults as $result) {
if ($result->getText() == $value) {
$result->click();
return;
}
}
throw new \Exception(sprintf('Value "%s" not found for "%s"', $value, $field));
}
/** /**
* Checks, the field matches the value. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps. * Checks, the field matches the value. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps.
......
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
And I follow "A's Page 01" And I follow "A's Page 01"
And I follow "Edit this page" And I follow "Edit this page"
And I follow "Share page" And I follow "Share page"
And I set the select2 field "editaccess_views" to "A's Page 01, A's Page 02, A's Page 03, A's Page 04, A's Page 05, A's Page 06, A's Page 07, A's Page 08, A's Page 09, A's Page 10, A's Page 11, A's Page 12, A's Page 13, A's Page 14, A's Page 15, A's Page 16, A's Page 17, A's Page 18, A's Page 19, A's Page 20, A's Page 21" And I set the select2 value "A's Page 01, A's Page 02, A's Page 03, A's Page 04, A's Page 05, A's Page 06, A's Page 07, A's Page 08, A's Page 09, A's Page 10, A's Page 11, A's Page 12, A's Page 13, A's Page 14, A's Page 15, A's Page 16, A's Page 17, A's Page 18, A's Page 19, A's Page 20, A's Page 21" for "editaccess_views"
And I select "Public" from "accesslist[0][searchtype]" And I select "Public" from "accesslist[0][searchtype]"
And I press "editaccess_submit" And I press "editaccess_submit"
And I follow "Logout" And I follow "Logout"
......
...@@ -69,7 +69,7 @@ These list must take into account the sort option choosen in the block config (B ...@@ -69,7 +69,7 @@ These list must take into account the sort option choosen in the block config (B
And I choose "Shared by me" in "Portfolio" And I choose "Shared by me" in "Portfolio"
And I follow "Pages" in the "div#main-column-container" "css_element" And I follow "Pages" in the "div#main-column-container" "css_element"
And I click on "Edit access" in "Page userA_01" row And I click on "Edit access" in "Page userA_01" row
And I set the select2 field "editaccess_views" to "Page userA_01, Page userA_02, Page userA_03, Page userA_04, Page userA_05" And I set the select2 value "Page userA_01, Page userA_02, Page userA_03, Page userA_04, Page userA_05" for "editaccess_views"
And I select "Group Z" from "accesslist[0][searchtype]" And I select "Group Z" from "accesslist[0][searchtype]"
And I press "Save" And I press "Save"
# Edit access for Collection userA_01 # Edit access for Collection userA_01
...@@ -77,7 +77,7 @@ These list must take into account the sort option choosen in the block config (B ...@@ -77,7 +77,7 @@ These list must take into account the sort option choosen in the block config (B
And I follow "Collections" in the "div#main-column-container" "css_element" And I follow "Collections" in the "div#main-column-container" "css_element"
And I click on "Edit access" in "Collection userA_01" row And I click on "Edit access" in "Collection userA_01" row
And I should not see "Collection userA_02" in the "ul.select2-selection__rendered" "css_element" And I should not see "Collection userA_02" in the "ul.select2-selection__rendered" "css_element"
And I set the select2 field "editaccess_collections" to "Collection userA_01, Collection userA_02, Collection userA_03, Collection userA_04, Collection userA_05, Collection userA_06" And I set the select2 value "Collection userA_01, Collection userA_02, Collection userA_03, Collection userA_04, Collection userA_05, Collection userA_06" for "editaccess_collections"
And I select "Group Z" from "accesslist[0][searchtype]" And I select "Group Z" from "accesslist[0][searchtype]"
And I press "Save" And I press "Save"
And I log out And I log out
...@@ -90,7 +90,7 @@ These list must take into account the sort option choosen in the block config (B ...@@ -90,7 +90,7 @@ These list must take into account the sort option choosen in the block config (B
# Edit access for pages # Edit access for pages
And I choose "Shared by me" in "Portfolio" And I choose "Shared by me" in "Portfolio"
And I click on "Edit access" in "Page userB_01" row And I click on "Edit access" in "Page userB_01" row
And I set the select2 field "editaccess_views" to "Page userB_01, Page userB_02, Page userB_03, Page userB_04, Page userB_05, Page userB_06, Page userB_07" And I set the select2 value "Page userB_01, Page userB_02, Page userB_03, Page userB_04, Page userB_05, Page userB_06, Page userB_07" for "editaccess_views"
And I select "Group Z" from "accesslist[0][searchtype]" And I select "Group Z" from "accesslist[0][searchtype]"
And I press "Save" And I press "Save"
And I log out And I log out
...@@ -160,7 +160,7 @@ These list must take into account the sort option choosen in the block config (B ...@@ -160,7 +160,7 @@ These list must take into account the sort option choosen in the block config (B
And I choose "Portfolio" And I choose "Portfolio"
And I click on "Edit \"Page userA_01\"" And I click on "Edit \"Page userA_01\""
And I follow "Edit title and description" And I follow "Edit title and description"
And I set the field "Page description" to "<p>This is the page 01 (updated)</p>" And I set the value "Page description" for "<p>This is the page 01 (updated)</p>"
And I press "Save" And I press "Save"
And I follow "Display page" And I follow "Display page"
# Check if it is now in the first page of the list of shared pages # Check if it is now in the first page of the list of shared pages
...@@ -174,7 +174,7 @@ These list must take into account the sort option choosen in the block config (B ...@@ -174,7 +174,7 @@ These list must take into account the sort option choosen in the block config (B
# Update the shared collection "Collection userA_06" # Update the shared collection "Collection userA_06"
And I choose "Collections" in "Portfolio" And I choose "Collections" in "Portfolio"
And I click on "Edit \"Collection userA_06\"" And I click on "Edit \"Collection userA_06\""
And I set the field "Collection description" to "This is the collection 06 (updated)" And I set the value "Collection description" for "This is the collection 06 (updated)"
And I press "Save" And I press "Save"
# Check if it is now in the first page of the list of shared collections # Check if it is now in the first page of the list of shared collections
And I choose "Groups" And I choose "Groups"
......
...@@ -76,14 +76,14 @@ The list of shared pages must take into account of access date (Bug 1374163) ...@@ -76,14 +76,14 @@ The list of shared pages must take into account of access date (Bug 1374163)
# Edit access for Collection 01 # Edit access for Collection 01
And I choose "Shared by me" in "Portfolio" And I choose "Shared by me" in "Portfolio"
And I click on "Edit access" in "Collection 01" row And I click on "Edit access" in "Collection 01" row
And I set the select2 field "editaccess_collections" to "Collection 01" And I set the select2 value "Collection 01"" for "editaccess_collections"
And I select "Group Z" from "accesslist[0][searchtype]" And I select "Group Z" from "accesslist[0][searchtype]"
And I set the field "accesslist[0][startdate]" to "2015/06/15 03:00" And I set the field "accesslist[0][startdate]" to "2015/06/15 03:00"
And I press "Save" And I press "Save"
# Edit access for Collection 03 # Edit access for Collection 03
And I choose "Shared by me" in "Portfolio" And I choose "Shared by me" in "Portfolio"
And I click on "Edit access" in "Collection 03" row And I click on "Edit access" in "Collection 03" row
And I set the select2 field "editaccess_collections" to "Collection 03" And I set the select2 value "Collection 03" for "editaccess_collections"
And I follow "Advanced options" And I follow "Advanced options"
And I fill in the following: And I fill in the following:
| Access start date/time | 2015/06/15 00:00 | | Access start date/time | 2015/06/15 00:00 |
...@@ -91,7 +91,7 @@ The list of shared pages must take into account of access date (Bug 1374163) ...@@ -91,7 +91,7 @@ The list of shared pages must take into account of access date (Bug 1374163)
# Edit access for Collection 05 # Edit access for Collection 05
And I choose "Shared by me" in "Portfolio" And I choose "Shared by me" in "Portfolio"
And I click on "Edit access" in "Collection 05" row And I click on "Edit access" in "Collection 05" row
And I set the select2 field "editaccess_collections" to "Collection 05" And I set the select2 value "Collection 05" for "editaccess_collections"
And I select "Group Z" from "accesslist[0][searchtype]" And I select "Group Z" from "accesslist[0][searchtype]"
And I press "Save" And I press "Save"
# Check the list of shared pages to group "Group Z" # Check the list of shared pages to group "Group Z"
......
@javascript @core @core_messages
Feature: Select2 ajax test using sendmessage
In order to retrieve data via ajax and select it
As an admin I need to fill in a select2 box
So I can confirm the value is selectable
Background:
Given the following "users" exist:
| username | password | email | firstname | lastname | institution | authname | role |
| userA | Kupuhipa1 | test01@example.com | Andrea | Andrews | mahara | internal | member |
| userB | Kupuhipa1 | test02@example.com | Barry | Bishop | mahara | internal | member |
| userC | Kupuhipa1 | test03@example.com | Catriona | Carson | mahara | internal | member |
Scenario: Selecting select2 option via ajax
# Log in as an Admin user
Given I log in as "admin" with password "Kupuhipa1"
# Checking messages
And I follow "mail"
And I follow "Compose"
And I fill in select2 input "sendmessage_recipients" with "userA" and select "Andrea Andrews (userA)"
And I set the following fields to these values:
| Subject | Test message |
| Message | This is a test |
And I press "Send message"
Then I should see "Message sent"
\ No newline at end of file
@javascript @core core_messages @javascript @core @core_messages
Feature: Send messages to other users Feature: Send messages to other users
In order to send a message to another user In order to send a message to another user
As an admin I need to create an user As an admin I need to create an user
......
...@@ -35,7 +35,7 @@ Scenario: Create users and search for them (Bug 897586) ...@@ -35,7 +35,7 @@ Scenario: Create users and search for them (Bug 897586)
And I should see "Page saved successfully" And I should see "Page saved successfully"
# Sharing both of the pages that have been created # Sharing both of the pages that have been created
And I follow "Share page" And I follow "Share page"
And I set the select2 field "editaccess_views" to "Testing page 1, Testing page 2" And I set the select2 value "Testing page 1, Testing page 2" for "editaccess_views"
And I select "Public" from "accesslist[0][searchtype]" And I select "Public" from "accesslist[0][searchtype]"
And I press "Save" And I press "Save"
# Verifying that both of the pages have been shared # Verifying that both of the pages have been shared
...@@ -63,7 +63,7 @@ Scenario: Create users and search for them (Bug 897586) ...@@ -63,7 +63,7 @@ Scenario: Create users and search for them (Bug 897586)
And I should see "Page saved successfully" And I should see "Page saved successfully"
# Sharing both of the pages Jen created # Sharing both of the pages Jen created
And I follow "Share page" And I follow "Share page"
And I set the select2 field "editaccess_views" to "Testing page 3, Testing page 4" And I set the select2 value "Testing page 3, Testing page 4" for "editaccess_views"
And I select "Public" from "accesslist[0][searchtype]" And I select "Public" from "accesslist[0][searchtype]"
And I press "Save" And I press "Save"
# Verifying that both of the pages have been shared # Verifying that both of the pages have been shared
......
...@@ -69,8 +69,8 @@ As an admin ...@@ -69,8 +69,8 @@ As an admin
And I follow "A's Page 01" And I follow "A's Page 01"
And I follow "Edit this page" And I follow "Edit this page"
And I follow "Share page" And I follow "Share page"
And I set the select2 field "editaccess_views" to "A's Page 01, A's Page 02, A's Page 03, A's Page 04, A's Page 05, A's Page 06, A's Page 07, A's Page 08, A's Page 09, A's Page 10, A's Page 11, A's Page 12, A's Page 13, A's Page 14, A's Page 15, A's Page 16, A's Page 17, A's Page 18, A's Page 19, A's Page 20, A's Page 21, A's Page 22, A's Page 23, A's Page 24, A's Page 25, A's Page 26, A's Page 27, A's Page 28, A's Page 29, A's Page 30, A's Page 31, A's Page 32, A's Page 33, A's Page 34, A's Page 35, A's Page 36, A's Page 37, A's Page 38, A's Page 39, A's Page 40, A's Page 41, A's Page 42, A's Page 43, A's Page 44, A's Page 45, A's Page 46, A's Page 47, A's Page 48, A's Page 49, A's Page 50, A's Page 51" And I set the select2 value ""A's Page 01, A's Page 02, A's Page 03, A's Page 04, A's Page 05, A's Page 06, A's Page 07, A's Page 08, A's Page 09, A's Page 10, A's Page 11, A's Page 12, A's Page 13, A's Page 14, A's Page 15, A's Page 16, A's Page 17, A's Page 18, A's Page 19, A's Page 20, A's Page 21, A's Page 22, A's Page 23, A's Page 24, A's Page 25, A's Page 26, A's Page 27, A's Page 28, A's Page 29, A's Page 30, A's Page 31, A's Page 32, A's Page 33, A's Page 34, A's Page 35, A's Page 36, A's Page 37, A's Page 38, A's Page 39, A's Page 40, A's Page 41, A's Page 42, A's Page 43, A's Page 44, A's Page 45, A's Page 46, A's Page 47, A's Page 48, A's Page 49, A's Page 50, A's Page 51" for "editaccess_views"
And I select "Public" from "accesslist[0][searchtype]" And I select "Public" from "accesslist[0][searchtype]"
And I press "Save" And I press "Save"
And I follow "Logout" And I follow "Logout"
And I log in as "userB" with password "Kupuhipa1" And I log in as "userB" with password "Kupuhipa1"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment