From e96bbb53afdaa16650164809689a45ac35926b22 Mon Sep 17 00:00:00 2001 From: "ferran.recio" Date: Tue, 6 Aug 2019 16:34:45 +0200 Subject: [PATCH 1/2] MDL-66332 too_capability: add show differences option --- .../tool/capability/classes/settings_form.php | 5 +++++ admin/tool/capability/index.php | 7 ++++++- .../capability/lang/en/tool_capability.php | 3 +++ admin/tool/capability/renderer.php | 20 ++++++++++++++----- 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/admin/tool/capability/classes/settings_form.php b/admin/tool/capability/classes/settings_form.php index cf5fa3e8197..1bdf548dd04 100644 --- a/admin/tool/capability/classes/settings_form.php +++ b/admin/tool/capability/classes/settings_form.php @@ -60,6 +60,11 @@ class tool_capability_settings_form extends moodleform { $form->addElement('select', 'roles', get_string('roleslabel', 'tool_capability'), $roles, $attributes); $form->setType('roles', PARAM_TEXT); + $form->addElement('checkbox', 'onlydiff', + get_string('filters', 'tool_capability'), + get_string('onlydiff', 'tool_capability')); + $form->setType('onlydiff', PARAM_BOOL); + $form->addElement('submit', 'submitbutton', get_string('getreport', 'tool_capability')); } diff --git a/admin/tool/capability/index.php b/admin/tool/capability/index.php index 57f071678c8..32ef291501e 100644 --- a/admin/tool/capability/index.php +++ b/admin/tool/capability/index.php @@ -66,6 +66,7 @@ $capabilities = array(); $rolestoshow = array(); $roleids = array('0'); $cleanedroleids = array(); +$onlydiff = false; if ($data = $form->get_data()) { $roleids = array(); @@ -90,6 +91,10 @@ if ($data = $form->get_data()) { } } } + + if (isset($data->onlydiff)) { + $onlydiff = $data->onlydiff; + } } \tool_capability\event\report_viewed::create()->trigger(); @@ -103,7 +108,7 @@ $form->display(); // If we have a capability, generate the report. if (count($capabilities) && count($rolestoshow)) { /* @var tool_capability_renderer $renderer */ - echo $renderer->capability_comparison_table($capabilities, $context->id, $rolestoshow); + echo $renderer->capability_comparison_table($capabilities, $context->id, $rolestoshow, $onlydiff); } // Footer. diff --git a/admin/tool/capability/lang/en/tool_capability.php b/admin/tool/capability/lang/en/tool_capability.php index b44db226de1..05a45340f56 100644 --- a/admin/tool/capability/lang/en/tool_capability.php +++ b/admin/tool/capability/lang/en/tool_capability.php @@ -22,15 +22,18 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +$string['onlydiff'] = 'Show differences only'; $string['capabilitylabel'] = 'Capability:'; $string['capabilityreport'] = 'Capability overview'; $string['eventreportviewed'] = 'Report viewed'; +$string['filters'] = 'Filter results'; $string['forroles'] = 'For roles {$a}'; $string['getreport'] = 'Get the overview'; $string['changeoverrides'] = 'Change overrides in this context'; $string['changeroles'] = 'Change role definitions'; $string['intro'] = 'This report shows, for a particular capability, what permission that capability has in the definition of every role (or a selection of roles), and everywhere in the site where that capability is overridden.'; $string['pluginname'] = 'Capability overview'; +$string['nodifferences'] = 'There are no differences to show between selected roles in this context'; $string['reportforcapability'] = 'Report for capability \'{$a}\''; $string['reportsettings'] = 'Report settings'; $string['roleslabel'] = 'Roles:'; diff --git a/admin/tool/capability/renderer.php b/admin/tool/capability/renderer.php index 30a9628b90b..d083f280860 100644 --- a/admin/tool/capability/renderer.php +++ b/admin/tool/capability/renderer.php @@ -72,9 +72,10 @@ class tool_capability_renderer extends plugin_renderer_base { * @param array $capabilities An array of capabilities to show comparison for. * @param int $contextid The context we are displaying for. * @param array $roles An array of roles to show comparison for. + * @param bool $onlydiff show only different permissions * @return string */ - public function capability_comparison_table(array $capabilities, $contextid, array $roles) { + public function capability_comparison_table(array $capabilities, $contextid, array $roles, $onlydiff=false) { $strpermissions = $this->get_permission_strings(); $permissionclasses = $this->get_permission_classes(); @@ -99,18 +100,23 @@ class tool_capability_renderer extends plugin_renderer_base { $row = new html_table_row(array($captitle)); + $permissiontypes = array(); foreach ($roles as $role) { if (isset($contexts[$contextid]->rolecapabilities[$role->id])) { $permission = $contexts[$contextid]->rolecapabilities[$role->id]; } else { $permission = CAP_INHERIT; } + if (!in_array($permission, $permissiontypes)) { + $permissiontypes[] = $permission; + } $cell = new html_table_cell($strpermissions[$permission]); $cell->attributes['class'] = $permissionclasses[$permission]; $row->cells[] = $cell; } - - $table->data[] = $row; + if (!$onlydiff || count($permissiontypes) > 1) { + $table->data[] = $row; + } } // Start the list item, and print the context name as a link to the place to make changes. @@ -125,11 +131,15 @@ class tool_capability_renderer extends plugin_renderer_base { $title = get_string('permissionsincontext', 'core_role', $context->get_context_name()); $html = $this->output->heading(html_writer::link($url, $title), 3); - $html .= html_writer::table($table); + if (!empty($table->data)) { + $html .= html_writer::table($table); + } else { + $html .= html_writer::tag('p', get_string('nodifferences', 'tool_capability')); + } // If there are any child contexts, print them recursively. if (!empty($contexts[$contextid]->children)) { foreach ($contexts[$contextid]->children as $childcontextid) { - $html .= $this->capability_comparison_table($capabilities, $childcontextid, $roles, true); + $html .= $this->capability_comparison_table($capabilities, $childcontextid, $roles, $onlydiff); } } return $html; From 178be88e974967b1f42f6492370094adde2fbc4a Mon Sep 17 00:00:00 2001 From: "ferran.recio" Date: Thu, 29 Aug 2019 16:23:41 +0200 Subject: [PATCH 2/2] MDL-66332 tool_capability: adding acceptance tests --- .../tests/behat/show_capabilies.feature | 104 ++++++++++++++++++ .../tests/behat/show_differences.feature | 67 +++++++++++ .../moodle-tool_capability-search-debug.js | 2 +- .../moodle-tool_capability-search-min.js | 2 +- .../moodle-tool_capability-search.js | 2 +- .../capability/yui/src/search/js/search.js | 2 +- 6 files changed, 175 insertions(+), 4 deletions(-) create mode 100644 admin/tool/capability/tests/behat/show_capabilies.feature create mode 100644 admin/tool/capability/tests/behat/show_differences.feature diff --git a/admin/tool/capability/tests/behat/show_capabilies.feature b/admin/tool/capability/tests/behat/show_capabilies.feature new file mode 100644 index 00000000000..d0ec7c653fd --- /dev/null +++ b/admin/tool/capability/tests/behat/show_capabilies.feature @@ -0,0 +1,104 @@ +@tool @tool_capability +Feature: show capabilities for selected roles + In order to check roles capabilities + As an admin + I need to be able to customize capabilities report viewing only specific roles and capabilities + + Background: + Given the following "roles" exist: + | shortname | name | archetype | + | studenteq | Studenteq | student | + | studentdf | Studentdf | student | + And the following "permission overrides" exist: + | capability | permission | role | contextlevel | reference | + | moodle/course:changefullname | Allow | studentdf | System | | + | moodle/course:changeshortname | Prohibit | studentdf | System | | + | moodle/course:changeidnumber | Prevent | studentdf | System | | + And I log in as "admin" + And I navigate to "Users > Permissions > Capability overview" in site administration + + Scenario: visualize capabilities table with a limited number of capabilities + When I set the following fields to these values: + | Capability: | moodle/course:changefullname, moodle/course:changeshortname | + | Roles: | Studentdf | + And I click on "Get the overview" "button" + Then I should see "moodle/course:changefullname" in the "comparisontable" "table" + And I should see "moodle/course:changeshortname" in the "comparisontable" "table" + And I should not see "moodle/course:changecategory" in the "comparisontable" "table" + + Scenario: visualize an allow capability + When I set the following fields to these values: + | Capability: | moodle/course:changefullname | + | Roles: | Studentdf | + And I click on "Get the overview" "button" + Then I should see "Allow" in the "comparisontable" "table" + And I should not see "Prevent" in the "comparisontable" "table" + And I should not see "Prohibit" in the "comparisontable" "table" + And I should not see "Not set" in the "comparisontable" "table" + + Scenario: visualize a prohibit capability + When I set the following fields to these values: + | Capability: | moodle/course:changeshortname | + | Roles: | Studentdf | + And I click on "Get the overview" "button" + Then I should not see "Allow" in the "comparisontable" "table" + And I should not see "Prevent" in the "comparisontable" "table" + And I should see "Prohibit" in the "comparisontable" "table" + And I should not see "Not set" in the "comparisontable" "table" + + Scenario: visualize a not set capability + When I set the following fields to these values: + | Capability: | moodle/course:changecategory | + | Roles: | Studentdf | + And I click on "Get the overview" "button" + Then I should not see "Allow" in the "comparisontable" "table" + And I should not see "Prevent" in the "comparisontable" "table" + And I should not see "Prohibit" in the "comparisontable" "table" + And I should see "Not set" in the "comparisontable" "table" + + Scenario: visualize more than one role + When I set the following fields to these values: + | Capability: | moodle/course:changecategory | + | Roles: | Student, Studentdf | + And I click on "Get the overview" "button" + Then I should see "Student" in the "comparisontable" "table" + And I should see "Studentdf" in the "comparisontable" "table" + And I should not see "Teacher" in the "comparisontable" "table" + + Scenario: visualize all roles without selecting any role + When I set the following fields to these values: + | Capability: | moodle/course:changecategory | + And I click on "Get the overview" "button" + Then I should see "Student" in the "comparisontable" "table" + And I should see "Studentdf" in the "comparisontable" "table" + And I should see "Teacher" in the "comparisontable" "table" + + Scenario: visualize all roles by selecting All option + When I set the following fields to these values: + | Capability: | moodle/course:changecategory | + | Roles: | All | + And I click on "Get the overview" "button" + Then I should see "Student" in the "comparisontable" "table" + And I should see "Studentdf" in the "comparisontable" "table" + And I should see "Teacher" in the "comparisontable" "table" + + @javascript + Scenario: filter capability list using javascript + Given I should see "moodle/site:config" in the "Capability" "field" + And I should see "moodle/course:change" in the "Capability" "field" + When I wait until the page is ready + And I set the field "capabilitysearch" to "moodle/course:change" + Then I should see "moodle/course:change" in the "Capability" "field" + And I should not see "moodle/site:config" in the "Capability" "field" + + @javascript + Scenario: selecting capabilities using filters + Given I should see "moodle/course:change" in the "Capability" "field" + When I wait until the page is ready + And I set the field "capabilitysearch" to "moodle/course:change" + When I set the following fields to these values: + | Capability: | moodle/course:changecategory | + | Roles: | Student | + And I set the field "capabilitysearch" to "" + And I click on "Get the overview" "button" + Then I should see "moodle/course:changecategory" in the "comparisontable" "table" diff --git a/admin/tool/capability/tests/behat/show_differences.feature b/admin/tool/capability/tests/behat/show_differences.feature new file mode 100644 index 00000000000..1c907a42186 --- /dev/null +++ b/admin/tool/capability/tests/behat/show_differences.feature @@ -0,0 +1,67 @@ +@tool @tool_capability +Feature: show only differences between roles for selected capabilities + In order to check roles capabilities + As an admin + I need to be able to filter capabilities report viewing only role differences + + Background: + Given the following "roles" exist: + | shortname | name | archetype | + | studenteq | Studenteq | student | + | studentdf | Studentdf | student | + And the following "permission overrides" exist: + | capability | permission | role | contextlevel | reference | + | moodle/course:changefullname | Allow | studentdf | System | | + | moodle/course:changeshortname | Prohibit | studentdf | System | | + And I log in as "admin" + And I navigate to "Users > Permissions > Capability overview" in site administration + + Scenario: Compare identical roles + When I set the following fields to these values: + | Capability: | moodle/course:changefullname, moodle/course:changeshortname, moodle/course:changeidnumber, moodle/course:changesummary | + | Roles: | Student, Studenteq | + And I set the field "Show differences only" to "1" + And I click on "Get the overview" "button" + Then I should see "There are no differences to show between selected roles in this context" + + Scenario: Compare different roles + When I set the following fields to these values: + | Capability: | moodle/course:changefullname, moodle/course:changeshortname, moodle/course:changeidnumber, moodle/course:changesummary | + | Roles: | Student, Studentdf | + And I set the field "Show differences only" to "1" + And I click on "Get the overview" "button" + Then I should not see "There are no differences to show between selected roles in this context" + And I should see "moodle/course:changefullname" in the "comparisontable" "table" + And I should see "moodle/course:changeshortname" in the "comparisontable" "table" + And I should not see "moodle/course:changesummary" in the "comparisontable" "table" + + Scenario: Compare different roles but comparing capabilities that are equals on both + When I set the following fields to these values: + | Capability: | moodle/course:changeidnumber, moodle/course:changesummary | + | Roles: | Student, Studentdf | + And I set the field "Show differences only" to "1" + And I click on "Get the overview" "button" + Then I should see "There are no differences to show between selected roles in this context" + + Scenario: Compare all roles without selecting specific role + When I set the following fields to these values: + | Capability: | moodle/course:changefullname, moodle/site:config | + And I set the field "Show differences only" to "1" + And I click on "Get the overview" "button" + Then I should not see "moodle/site:config" in the "comparisontable" "table" + And I should see "moodle/course:changefullname" in the "comparisontable" "table" + + Scenario: Compare all roles without selecting specific role on not defined capability + When I set the following fields to these values: + | Capability: | moodle/site:config | + And I set the field "Show differences only" to "1" + And I click on "Get the overview" "button" + Then I should see "There are no differences to show between selected roles in this context" + + Scenario: Comparing only one role + When I set the following fields to these values: + | Capability: | moodle/course:changefullname, moodle/course:changeshortname, moodle/course:changeidnumber, moodle/course:changesummary | + | Roles: | Student | + And I set the field "Show differences only" to "1" + And I click on "Get the overview" "button" + Then I should see "There are no differences to show between selected roles in this context" diff --git a/admin/tool/capability/yui/build/moodle-tool_capability-search/moodle-tool_capability-search-debug.js b/admin/tool/capability/yui/build/moodle-tool_capability-search/moodle-tool_capability-search-debug.js index 6b8b3686dd7..1c0553405bc 100644 --- a/admin/tool/capability/yui/build/moodle-tool_capability-search/moodle-tool_capability-search-debug.js +++ b/admin/tool/capability/yui/build/moodle-tool_capability-search/moodle-tool_capability-search-debug.js @@ -76,7 +76,7 @@ SEARCH.prototype = { this.button = this.form.all('input[type=submit]'); this.lastsearch = this.form.one('input[name=search]'); - var div = Y.Node.create('
'), + var div = Y.Node.create('
'), label = Y.Node.create(''); this.input = Y.Node.create(''); diff --git a/admin/tool/capability/yui/build/moodle-tool_capability-search/moodle-tool_capability-search-min.js b/admin/tool/capability/yui/build/moodle-tool_capability-search/moodle-tool_capability-search-min.js index 7e1baef5c5d..697a2149049 100644 --- a/admin/tool/capability/yui/build/moodle-tool_capability-search/moodle-tool_capability-search-min.js +++ b/admin/tool/capability/yui/build/moodle-tool_capability-search/moodle-tool_capability-search-min.js @@ -1 +1 @@ -YUI.add("moodle-tool_capability-search",function(e,t){var n=function(){n.superclass.constructor.apply(this,arguments)};n.prototype={form:null,select:null,selectoptions:{},input:null,button:null,lastsearch:null,initializer:function(){this.form=e.one("#capability-overview-form"),this.select=this.form.one("select[data-search=capability]"),this.select.setStyle("minWidth",this.select.get("offsetWidth")),this.select.get("options").each(function(e){var t=e.get("value");this.selectoptions[t]=e},this),this.button=this.form.all("input[type=submit]"),this.lastsearch=this.form.one("input[name=search]");var t=e.Node.create('
'),n=e.Node.create('");this.input=e.Node.create(''),t.append(n).append(this.input),this.select.insert(t,"before"),this.input.on("keyup",this.typed,this),this.select.on("change",this.validate,this),this.lastsearch&&(this.input.set("value",this.lastsearch.get("value")),this.typed(),this.select.one("option[selected]")&&this.select.set("scrollTop",this.select.one("option[selected]").get("getX"))),this.validate()},validate:function(){this.button.set("disabled",this.select.get("value")==="")},typed:function(){var e=this.input.get("value"),t=0,n=null,r;this.lastsearch&&this.lastsearch.set("value",e),this.select.all("option").remove();for(r in this.selectoptions)r.indexOf(e)>=0&&(t++,n=this.selectoptions[r],this.select.append(this.selectoptions[r]));t===0?this.input.addClass("error"):(this.input.removeClass("error"),t===1&&n.set("selected",!0)),this.validate()}},e.extend(n,e.Base,n.prototype,{NAME:"tool_capability-search",ATTRS:{strsearch:{}}}),M.tool_capability=M.tool_capability||{},M.tool_capability.init_capability_search=function(e){new n(e)}},"@VERSION@",{requires:["base","node"]}); +YUI.add("moodle-tool_capability-search",function(e,t){var n=function(){n.superclass.constructor.apply(this,arguments)};n.prototype={form:null,select:null,selectoptions:{},input:null,button:null,lastsearch:null,initializer:function(){this.form=e.one("#capability-overview-form"),this.select=this.form.one("select[data-search=capability]"),this.select.setStyle("minWidth",this.select.get("offsetWidth")),this.select.get("options").each(function(e){var t=e.get("value");this.selectoptions[t]=e},this),this.button=this.form.all("input[type=submit]"),this.lastsearch=this.form.one("input[name=search]");var t=e.Node.create('
'),n=e.Node.create('");this.input=e.Node.create(''),t.append(n).append(this.input),this.select.insert(t,"before"),this.input.on("keyup",this.typed,this),this.select.on("change",this.validate,this),this.lastsearch&&(this.input.set("value",this.lastsearch.get("value")),this.typed(),this.select.one("option[selected]")&&this.select.set("scrollTop",this.select.one("option[selected]").get("getX"))),this.validate()},validate:function(){this.button.set("disabled",this.select.get("value")==="")},typed:function(){var e=this.input.get("value"),t=0,n=null,r;this.lastsearch&&this.lastsearch.set("value",e),this.select.all("option").remove();for(r in this.selectoptions)r.indexOf(e)>=0&&(t++,n=this.selectoptions[r],this.select.append(this.selectoptions[r]));t===0?this.input.addClass("error"):(this.input.removeClass("error"),t===1&&n.set("selected",!0)),this.validate()}},e.extend(n,e.Base,n.prototype,{NAME:"tool_capability-search",ATTRS:{strsearch:{}}}),M.tool_capability=M.tool_capability||{},M.tool_capability.init_capability_search=function(e){new n(e)}},"@VERSION@",{requires:["base","node"]}); diff --git a/admin/tool/capability/yui/build/moodle-tool_capability-search/moodle-tool_capability-search.js b/admin/tool/capability/yui/build/moodle-tool_capability-search/moodle-tool_capability-search.js index 6b8b3686dd7..1c0553405bc 100644 --- a/admin/tool/capability/yui/build/moodle-tool_capability-search/moodle-tool_capability-search.js +++ b/admin/tool/capability/yui/build/moodle-tool_capability-search/moodle-tool_capability-search.js @@ -76,7 +76,7 @@ SEARCH.prototype = { this.button = this.form.all('input[type=submit]'); this.lastsearch = this.form.one('input[name=search]'); - var div = Y.Node.create('
'), + var div = Y.Node.create('
'), label = Y.Node.create(''); this.input = Y.Node.create(''); diff --git a/admin/tool/capability/yui/src/search/js/search.js b/admin/tool/capability/yui/src/search/js/search.js index b3b152721fb..1e4b96c9d91 100644 --- a/admin/tool/capability/yui/src/search/js/search.js +++ b/admin/tool/capability/yui/src/search/js/search.js @@ -74,7 +74,7 @@ SEARCH.prototype = { this.button = this.form.all('input[type=submit]'); this.lastsearch = this.form.one('input[name=search]'); - var div = Y.Node.create('
'), + var div = Y.Node.create('
'), label = Y.Node.create(''); this.input = Y.Node.create('');