From 3b732cd6cc02dbcc56bd01b2fa4fde24eb0dd13c Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Thu, 24 Oct 2013 09:57:26 +0800 Subject: [PATCH] MDL-42492 course: Show option to move category to top level added behat to test move category --- course/classes/management_renderer.php | 6 +- course/management.php | 12 ++- course/tests/behat/behat_course.php | 74 +++++++++++++++++++ .../tests/behat/category_management.feature | 27 +++++++ lang/en/moodle.php | 2 + lib/coursecatlib.php | 2 +- 6 files changed, 119 insertions(+), 4 deletions(-) diff --git a/course/classes/management_renderer.php b/course/classes/management_renderer.php index 815d4edda45..fa26e1b3bf0 100644 --- a/course/classes/management_renderer.php +++ b/course/classes/management_renderer.php @@ -384,7 +384,11 @@ class core_course_management_renderer extends plugin_renderer_base { ); } if (coursecat::can_change_parent_any()) { - $options = coursecat::make_categories_list('moodle/category:manage'); + $options = array(); + if (has_capability('moodle/category:manage', context_system::instance())) { + $options[0] = coursecat::get(0)->get_formatted_name(); + } + $options += coursecat::make_categories_list('moodle/category:manage'); $select = html_writer::select( $options, 'movecategoriesto', diff --git a/course/management.php b/course/management.php index 3eed8025175..301e0a5aca7 100644 --- a/course/management.php +++ b/course/management.php @@ -324,12 +324,20 @@ if ($action !== false && confirm_sesskey()) { $a = new stdClass; $a->count = $movecount; $a->to = $movetocat->get_formatted_name(); - $notificationspass[] = get_string('movecategoriessuccess', 'moodle', $a); + $movesuccessstrkey = 'movecategoriessuccess'; + if ($movetocatid == 0) { + $movesuccessstrkey = 'movecategoriestotopsuccess'; + } + $notificationspass[] = get_string($movesuccessstrkey, 'moodle', $a); } else if ($movecount === 1) { $a = new stdClass; $a->moved = $cattomove->get_formatted_name(); $a->to = $movetocat->get_formatted_name(); - $notificationspass[] = get_string('movecategorysuccess', 'moodle', $a); + $movesuccessstrkey = 'movecategorysuccess'; + if ($movetocatid == 0) { + $movesuccessstrkey = 'movecategorytotopsuccess'; + } + $notificationspass[] = get_string($movesuccessstrkey, 'moodle', $a); } } else if ($bulkresortcategories) { // Bulk resort selected categories. diff --git a/course/tests/behat/behat_course.php b/course/tests/behat/behat_course.php index 1aed5c6f8b4..13c97cd1371 100644 --- a/course/tests/behat/behat_course.php +++ b/course/tests/behat/behat_course.php @@ -974,6 +974,80 @@ class behat_course extends behat_base { $node->click(); } + /** + * Clicks on a category checkbox in the management interface. + * + * @Given /^I select category "(?P[^"]*)" in the management interface$/ + * @param string $name + */ + public function i_select_category_in_the_management_interface($name) { + $node = $this->get_management_category_listing_node_by_name($name); + $node->checkField('bcat[]'); + } + + /** + * Clicks course checkbox in the management interface. + * + * @Given /^I select course "(?P[^"]*)" in the management interface$/ + * @param string $name + */ + public function i_select_course_in_the_management_interface($name) { + $node = $this->get_management_course_listing_node_by_name($name); + $node->checkField('bc[]'); + } + + /** + * Move selected categories to top level in the management interface. + * + * @Given /^I move category "(?P[^"]*)" to top level in the management interface$/ + * @param string $idnumber + * @return Given[] + */ + public function i_move_category_to_top_level_in_the_management_interface($idnumber) { + $id = $this->get_category_id($idnumber); + $selector = sprintf('.listitem-category[data-id="%d"] > div', $id); + $node = $this->find('css', $selector); + $node->checkField('bcat[]'); + return array( + new Given('I select "' . coursecat::get(0)->get_formatted_name() . '" from "menumovecategoriesto"'), + new Given('I press "bulkmovecategories"'), + ); + } + + /** + * Checks that a category is a subcategory of specific category. + * + * @Given /^I should see category "(?P[^"]*)" as subcategory of "(?P[^"]*)" in the management interface$/ + * @throws ExpectationException + * @param string $subcatidnumber + * @param string $catidnumber + */ + public function i_should_see_category_as_subcategory_of_in_the_management_interface($subcatidnumber, $catidnumber) { + $categorynodeid = $this->get_category_id($catidnumber); + $subcategoryid = $this->get_category_id($subcatidnumber); + $exception = new ExpectationException('The category '.$subcatidnumber.' is not a subcategory of '.$catidnumber, $this->getSession()); + $selector = sprintf('#category-listing .listitem-category[data-id="%d"] .listitem-category[data-id="%d"]', $categorynodeid, $subcategoryid); + $this->find('css', $selector, $exception); + } + + /** + * Checks that a category is not a subcategory of specific category. + * + * @Given /^I should not see category "(?P[^"]*)" as subcategory of "(?P[^"]*)" in the management interface$/ + * @throws ExpectationException + * @param string $subcatidnumber + * @param string $catidnumber + */ + public function i_should_not_see_category_as_subcategory_of_in_the_management_interface($subcatidnumber, $catidnumber) { + try { + $this->i_should_see_category_as_subcategory_of_in_the_management_interface($subcatidnumber, $catidnumber); + } catch (ExpectationException $e) { + // ExpectedException means that it is not highlighted. + return; + } + throw new ExpectationException('The category '.$subcatidnumber.' is a subcategory of '.$catidnumber, $this->getSession()); + } + /** * Click to expand a category revealing its sub categories within the management UI. * diff --git a/course/tests/behat/category_management.feature b/course/tests/behat/category_management.feature index 5dbc4f9f22d..f435dcece96 100644 --- a/course/tests/behat/category_management.feature +++ b/course/tests/behat/category_management.feature @@ -5,6 +5,7 @@ Feature: Test category management actions Test we can create a sub category Test we can edit a category Test we can delete a category + Test we can move a category Test we can assign roles within a category Test we can set permissions on a category Test we can manage cohorts within a category @@ -210,3 +211,29 @@ Feature: Test category management actions And I should see category listing "Cat 1" before "Test category 2" And I should see "No courses in this category" + @javascript + Scenario: Test moving a categories through the management interface. + Given the following "categories" exists: + | name | category | idnumber | + | Cat 1 | 0 | CAT1 | + | Cat 2 | 0 | CAT2 | + | Cat 3 | 0 | CAT3 | + + And I log in as "admin" + And I go to the courses management page + And I should see the "Course categories" management page + And I should see "Cat 1" in the "#category-listing ul.ml" "css_element" + And I should see "Cat 2" in the "#category-listing ul.ml" "css_element" + And I should see "Cat 3" in the "#category-listing ul.ml" "css_element" + And I select category "Cat 2" in the management interface + And I select category "Cat 3" in the management interface + And I select "Cat 1" from "menumovecategoriesto" + When I press "bulkmovecategories" + # Redirect + And I click on category "Cat 1" in the management interface + # Redirect + Then I should see category "CAT3" as subcategory of "CAT1" in the management interface + And I move category "CAT3" to top level in the management interface + # Redirect + And I should not see category "CAT3" as subcategory of "CAT1" in the management interface + Then I should see category "CAT2" as subcategory of "CAT1" in the management interface diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 522bad1dca0..7ea4073fc4b 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -1145,6 +1145,8 @@ $string['movecontent'] = 'Move {$a}'; $string['movecategorycontentto'] = 'Move into'; $string['movecategorysuccess'] = 'Successfully moved category \'{$a->moved}\' into category \'{$a->to}\''; $string['movecategoriessuccess'] = 'Successfully moved {$a->count} categories into category \'{$a->to}\''; +$string['movecategorytotopsuccess'] = 'Successfully moved category \'{$a->moved}\' to top level'; +$string['movecategoriestotopsuccess'] = 'Successfully moved {$a->count} categories to top level'; $string['movecategoryto'] = 'Move category to:'; $string['movecontentstoanothercategory'] = 'Move contents to another category'; $string['movecourseto'] = 'Move course to:'; diff --git a/lib/coursecatlib.php b/lib/coursecatlib.php index 8f941eab3a9..a30f58e4861 100644 --- a/lib/coursecatlib.php +++ b/lib/coursecatlib.php @@ -2043,7 +2043,7 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate { $context = $this->get_context(); return format_string($this->name, true, array('context' => $context) + $options); } else { - return ''; // TODO 'Top'?. + return get_string('top'); } }