Skip to content

Commit

Permalink
MDL-42174 More behat tests for filepicker
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Dec 5, 2013
1 parent c36a240 commit fcb6ed4
Show file tree
Hide file tree
Showing 10 changed files with 392 additions and 87 deletions.
8 changes: 7 additions & 1 deletion lib/behat/behat_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.

use Behat\Mink\Exception\ExpectationException as ExpectationException,
Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException,
Behat\Mink\Element\NodeElement as NodeElement;

/**
* Steps definitions base class.
Expand All @@ -39,6 +40,11 @@
* It can not contain steps definitions to avoid duplicates, only utility
* methods shared between steps.
*
* @method NodeElement find_field(string $locator) Finds a form element
* @method NodeElement find_button(string $locator) Finds a form input submit element or a button
* @method NodeElement find_link(string $locator) Finds a link on a page
* @method NodeElement find_file(string $locator) Finds a forum input file element
*
* @package core
* @category test
* @copyright 2012 David Monllaó
Expand Down
11 changes: 7 additions & 4 deletions lib/behat/behat_files.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
require_once(__DIR__ . '/behat_base.php');

use Behat\Mink\Exception\ExpectationException as ExpectationException,
Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
Behat\Mink\Element\NodeElement as NodeElement;

/**
* Files-related actions.
Expand Down Expand Up @@ -104,7 +104,7 @@ protected function perform_on_element($action, ExpectationException $exception)
*
* @throws ExpectationException Thrown by behat_base::find
* @param string $name The name of the folder/file
* @param string $filepickerelement The filepicker locator, the whole DOM if false
* @param string $filepickerelement The filepicker form element locator, the repository items are in filepicker modal window if false
* @return void
*/
protected function open_element_contextual_menu($name, $filepickerelement = false) {
Expand All @@ -115,6 +115,9 @@ protected function open_element_contextual_menu($name, $filepickerelement = fals
if ($filepickerelement) {
$containernode = $this->get_filepicker_node($filepickerelement);
$exceptionmsg = 'The "'.$filepickerelement.'" filepicker ' . $exceptionmsg;
$locatorprefix = "//div[@class='fp-content']";
} else {
$locatorprefix = "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-repo-items ')]//descendant::div[@class='fp-content']";
}

$exception = new ExpectationException($exceptionmsg, $this->getSession());
Expand All @@ -128,7 +131,7 @@ protected function open_element_contextual_menu($name, $filepickerelement = fals
// First we look at the folder as we need to click on the contextual menu otherwise it would be opened.
$node = $this->find(
'xpath',
"//div[@class='fp-content']" .
$locatorprefix .
"//descendant::*[self::div | self::a][contains(concat(' ', normalize-space(@class), ' '), ' fp-file ')]" .
"[contains(concat(' ', normalize-space(@class), ' '), ' fp-folder ')]" .
"[normalize-space(.)=$name]" .
Expand All @@ -142,7 +145,7 @@ protected function open_element_contextual_menu($name, $filepickerelement = fals
// Here the contextual menu is hidden, we click on the thumbnail.
$node = $this->find(
'xpath',
"//div[@class='fp-content']" .
$locatorprefix .
"//descendant::*[self::div | self::a][contains(concat(' ', normalize-space(@class), ' '), ' fp-file ')]" .
"[normalize-space(.)=$name]" .
"//descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-thumbnail ')]",
Expand Down
24 changes: 23 additions & 1 deletion lib/tests/behat/behat_deprecated.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@

require_once(__DIR__ . '/../../../lib/behat/behat_base.php');

use Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
use Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException,
Behat\Behat\Context\Step\Given as Given;

/**
* Deprecated behat step definitions.
Expand Down Expand Up @@ -94,6 +95,27 @@ public function i_go_to_notifications_page() {
);
}

/**
* Adds the specified file from the 'Recent files' repository to the specified filepicker of the current page.
*
* @deprecated since 2.7
* @todo MDL-42862 This will be deleted in Moodle 2.9
* @see behat_filepicker::i_add_file_from_repository_to_filemanager()
*
* @When /^I add "(?P<filename_string>(?:[^"]|\\")*)" file from recent files to "(?P<filepicker_field_string>(?:[^"]|\\")*)" filepicker$/
* @param string $filename
* @param string $filepickerelement
*/
public function i_add_file_from_recent_files_to_filepicker($filename, $filepickerelement) {
$reponame = get_string('pluginname', 'repository_recent');
$alternative = 'I add "' . $this->escape($filename) . '" file from "' .
$reponame . '" to "' . $this->escape($filepickerelement) . '" filemanager';
$this->deprecated_message($alternative);
return array(
new Given($alternative)
);
}

/**
* Throws an exception if $CFG->behat_usedeprecated is not allowed.
*
Expand Down
5 changes: 4 additions & 1 deletion repository/recent/tests/behat/add_recent.feature
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ Feature: Recent files repository lists the recently used files
And I fill the moodle form with:
| Name | Folder name |
| Description | Folder description |
And I add "empty.txt" file from recent files to "Files" filepicker
And I add "empty.txt" file from "Recent files" to "Files" filemanager
And I add "empty.txt" file from "Recent files" to "Files" filemanager as:
| Save as | empty_copy.txt |
And I press "Save and display"
Then I should see "empty.txt"
And I should see "empty_copy.txt"
And I should see "Folder description"
68 changes: 0 additions & 68 deletions repository/recent/tests/behat/behat_repository_recent.php

This file was deleted.

129 changes: 128 additions & 1 deletion repository/tests/behat/behat_filepicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@

require_once(__DIR__ . '/../../../lib/behat/behat_files.php');

use Behat\Mink\Exception\ExpectationException as ExpectationException;
use Behat\Mink\Exception\ExpectationException as ExpectationException,
Behat\Gherkin\Node\TableNode as TableNode;

/**
* Steps definitions to deal with the filepicker.
Expand Down Expand Up @@ -208,4 +209,130 @@ public function i_delete_file_from_filepicker($name, $filepickerelement) {
$this->wait_until_contents_are_updated($containernode);
}


/**
* Makes sure user can see the exact number of elements (files in folders) in the filemanager.
*
* @Then /^I should see "(?P<elementscount_number>\d+)" elements in "(?P<filemanagerelement_string>(?:[^"]|\\")*)" filemanager$/
* @throws ExpectationException Thrown by behat_base::find
* @param int $elementscount
* @param string $filemanagerelement
*/
public function i_should_see_elements_in_filemanager($elementscount, $filemanagerelement) {
$filemanagernode = $this->get_filepicker_node($filemanagerelement);
$this->wait_until_contents_are_updated($filemanagernode);
$elements = $this->find_all('css', '.fp-content .fp-file', false, $filemanagernode);
if (count($elements) != $elementscount) {
throw new ExpectationException('Found '.count($elements).' elements in filemanager instead of expected '.$elementscount);
}
}

/**
* Picks the file from repository leaving default values in select file dialogue.
*
* @When /^I add "(?P<filepath_string>(?:[^"]|\\")*)" file from "(?P<repository_string>(?:[^"]|\\")*)" to "(?P<filemanagerelement_string>(?:[^"]|\\")*)" filemanager$/
* @throws ExpectationException Thrown by behat_base::find
* @param string $filepath
* @parma string $repository
* @param string $filemanagerelement
*/
public function i_add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement) {
$this->add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, new TableNode(), false);
}

/**
* Picks the file from repository leaving default values in select file dialogue and confirming to overwrite an existing file.
*
* @When /^I add and overwrite "(?P<filepath_string>(?:[^"]|\\")*)" file from "(?P<repository_string>(?:[^"]|\\")*)" to "(?P<filemanagerelement_string>(?:[^"]|\\")*)" filemanager$/
* @throws ExpectationException Thrown by behat_base::find
* @param string $filepath
* @parma string $repository
* @param string $filemanagerelement
*/
public function i_add_and_overwrite_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement) {
$this->add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, new TableNode(),
get_string('overwrite', 'repository'));
}

/**
* Picks the file from repository filling the form in Select file dialogue.
*
* @When /^I add "(?P<filepath_string>(?:[^"]|\\")*)" file from "(?P<repository_string>(?:[^"]|\\")*)" to "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager as:$/
* @throws ExpectationException Thrown by behat_base::find
* @param string $filepath
* @parma string $repository
* @param string $filemanagerelement
* @param TableNode $data Data to fill the form in Select file dialogue
*/
public function i_add_file_from_repository_to_filemanager_as($filepath, $repository, $filemanagerelement, TableNode $data) {
$this->add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, $data, false);
}

/**
* Picks the file from repository confirming to overwrite an existing file
*
* @When /^I add and overwrite "(?P<filepath_string>(?:[^"]|\\")*)" file from "(?P<repository_string>(?:[^"]|\\")*)" to "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager as:$/
* @throws ExpectationException Thrown by behat_base::find
* @param string $filepath
* @parma string $repository
* @param string $filemanagerelement
* @param TableNode $data Data to fill the form in Select file dialogue
*/
public function i_add_and_overwrite_file_from_repository_to_filemanager_as($filepath, $repository, $filemanagerelement, TableNode $data) {
$this->add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, $data,
get_string('overwrite', 'repository'));
}

/**
* Picks the file from private files repository
*
* @throws ExpectationException Thrown by behat_base::find
* @param string $filepath
* @parma string $repository
* @param string $filemanagerelement
* @param TableNode $data Data to fill the form in Select file dialogue
* @param false|string $overwriteaction false if we don't expect that file with the same name already exists,
* or button text in overwrite dialogue ("Overwrite", "Rename to ...", "Cancel")
*/
protected function add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, TableNode $data,
$overwriteaction = false) {
$filemanagernode = $this->get_filepicker_node($filemanagerelement);

// Wait until file manager is completely loaded.
$this->wait_until_contents_are_updated($filemanagernode);

// Opening the select repository window and selecting the upload repository.
$this->open_add_file_window($filemanagernode, $repository);

$this->open_element_contextual_menu($filepath);

// Fill the form in Select window.
$datahash = $data->getRowsHash();

// The action depends on the field type.
foreach ($datahash as $locator => $value) {
// Getting the node element pointed by the label.
$fieldnode = $this->find_field($locator);

// Gets the field type from a parent node.
$field = behat_field_manager::get_form_field($fieldnode, $this->getSession());

// Delegates to the field class.
$field->set_value($value);
}

$this->find_button(get_string('getfile', 'repository'))->click();

if ($overwriteaction !== false) {
$this->getSession()->wait(1 * 1000, false);
$this->find_button($overwriteaction)->click();
}

// Ensure the file has been uploaded and all ajax processes finished.
$this->wait_until_return_to_form();

// Wait until file manager contents are updated.
$this->wait_until_contents_are_updated($filemanagernode);
}

}
68 changes: 68 additions & 0 deletions repository/tests/behat/create_shortcut.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
@core @core_filepicker @repository @repository_user @_only_local
Feature: Create shortcuts
In order to automatically synchronize copies of the file with the source
As a teacher
I need to be able to pick file as a shortcut

@javascript
Scenario: Upload a file as a copy and as a shortcut in filemanager
Given the following "users" exists:
| username | firstname | lastname | email |
| teacher1 | Terry | Teacher | teacher1@asd.com |
And the following "courses" exists:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "course enrolments" exists:
| user | course | role |
| teacher1 | C1 | editingteacher |
When I log in as "teacher1"
And I expand "My profile" node
And I follow "My private files"
And I upload "lib/tests/fixtures/empty.txt" file to "Files" filemanager
Then I should see "empty.txt" in the "div.fp-content" "css_element"
And I press "Save changes"
And I am on homepage
And I follow "Course 1"
And I turn editing mode on
And I add a "Folder" to section "1"
And I fill the moodle form with:
| Name | Test folder |
| Description | Test folder description |
And I add "empty.txt" file from "Private files" to "Files" filemanager
And I should see "1" elements in "Files" filemanager
And I should see "empty.txt" in the ".fp-content .fp-file" "css_element"
And ".fp-content .fp-file.fp-isreference" "css_element" should not exists
And I add "empty.txt" file from "Private files" to "Files" filemanager as:
| Save as | empty_ref.txt |
| Create an alias/shortcut to the file | 1 |
And I should see "2" elements in "Files" filemanager
And I should see "empty_ref.txt" in the ".fp-content .fp-file.fp-isreference" "css_element"
And I press "Save and display"
And I should see "empty.txt"
And I should see "empty_ref.txt"
And I press "Edit"
And I should see "2" elements in "Files" filemanager
And I should see "empty_ref.txt" in the ".fp-content .fp-file.fp-isreference" "css_element"
# ------ Overwriting the reference with a non-reference ---------
And I add and overwrite "empty.txt" file from "Private files" to "Files" filemanager as:
| Save as | empty_ref.txt |
And I should see "2" elements in "Files" filemanager
And ".fp-content .fp-file.fp-isreference" "css_element" should not exists
And I press "Save changes"
And I should see "empty.txt"
And I should see "empty_ref.txt"
And I press "Edit"
And I should see "2" elements in "Files" filemanager
And ".fp-content .fp-file.fp-isreference" "css_element" should not exists
# ------ Overwriting non-reference with a reference ---------
And I add and overwrite "empty.txt" file from "Private files" to "Files" filemanager as:
| Save as | empty_ref.txt |
| Create an alias/shortcut to the file | 1 |
And I should see "2" elements in "Files" filemanager
And I should see "empty_ref.txt" in the ".fp-content .fp-file.fp-isreference" "css_element"
And I press "Save changes"
And I should see "empty.txt"
And I should see "empty_ref.txt"
And I press "Edit"
And I should see "2" elements in "Files" filemanager
And I should see "empty_ref.txt" in the ".fp-content .fp-file.fp-isreference" "css_element"
Loading

0 comments on commit fcb6ed4

Please sign in to comment.