mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-44484 admin: Theme can be null and create user
- Loading branch information
1 parent
09df597
commit e6ce167
Showing
7 changed files
with
255 additions
and
21 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
admin/tool/uploaduser/classes/local/field_value_validators.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* File containing the field_value_validators class. | ||
* | ||
* @package tool_uploaduser | ||
* @copyright 2019 Mathew May | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
namespace tool_uploaduser\local; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
/** | ||
* Field validator class. | ||
* | ||
* @package tool_uploaduser | ||
* @copyright 2019 Mathew May | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class field_value_validators { | ||
|
||
/** | ||
* List of valid and compatible themes. | ||
* | ||
* @return array | ||
*/ | ||
protected static $themescache; | ||
|
||
/** | ||
* Validates the value provided for the theme field. | ||
* | ||
* @param string $value The value for the theme field. | ||
* @return array Contains the validation status and message. | ||
*/ | ||
public static function validate_theme($value) { | ||
global $CFG; | ||
|
||
$status = 'normal'; | ||
$message = ''; | ||
|
||
// Validate if user themes are allowed. | ||
if (!$CFG->allowuserthemes) { | ||
$status = 'warning'; | ||
$message = get_string('userthemesnotallowed', 'tool_uploaduser'); | ||
} else { | ||
// Cache list of themes if not yet set. | ||
if (!isset(self::$themescache)) { | ||
self::$themescache = get_list_of_themes(); | ||
} | ||
|
||
// Check if we have a valid theme. | ||
if (empty($value)) { | ||
$status = 'warning'; | ||
$message = get_string('notheme', 'tool_uploaduser'); | ||
} else if (!isset(self::$themescache[$value])) { | ||
$status = 'warning'; | ||
$message = get_string('invalidtheme', 'tool_uploaduser', s($value)); | ||
} | ||
} | ||
|
||
return [$status, $message]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
admin/tool/uploaduser/tests/field_value_validators_test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* Tests for field value validators of tool_uploaduser. | ||
* | ||
* @package tool_uploaduser | ||
* @copyright 2019 Jun Pataleta | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
use tool_uploaduser\local\field_value_validators; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
global $CFG; | ||
|
||
/** | ||
* Tests for field value validators of tool_uploaduser. | ||
* | ||
* @package tool_uploaduser | ||
* @copyright 2019 Jun Pataleta | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class field_value_validators_testcase extends advanced_testcase { | ||
|
||
/** | ||
* Data provider for \field_value_validators_testcase::test_validate_theme(). | ||
*/ | ||
public function themes_provider() { | ||
return [ | ||
'User themes disabled' => [ | ||
false, 'boost', 'warning', get_string('userthemesnotallowed', 'tool_uploaduser') | ||
], | ||
'User themes enabled, empty theme' => [ | ||
true, '', 'warning', get_string('notheme', 'tool_uploaduser') | ||
], | ||
'User themes enabled, invalid theme' => [ | ||
true, 'badtheme', 'warning', get_string('invalidtheme', 'tool_uploaduser', 'badtheme') | ||
], | ||
'User themes enabled, valid theme' => [ | ||
true, 'boost', 'normal', '' | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* Unit test for \tool_uploaduser\local\field_value_validators::validate_theme() | ||
* | ||
* @dataProvider themes_provider | ||
* @param boolean $userthemesallowed Whether to allow user themes. | ||
* @param string $themename The theme name to be tested. | ||
* @param string $expectedstatus The expected status. | ||
* @param string $expectedmessage The expected validation message. | ||
*/ | ||
public function test_validate_theme($userthemesallowed, $themename, $expectedstatus, $expectedmessage) { | ||
$this->resetAfterTest(); | ||
|
||
// Set value for $CFG->allowuserthemes. | ||
set_config('allowuserthemes', $userthemesallowed); | ||
|
||
// Validate the theme. | ||
list($status, $message) = field_value_validators::validate_theme($themename); | ||
|
||
// Check the status and validation message. | ||
$this->assertEquals($expectedstatus, $status); | ||
$this->assertEquals($expectedmessage, $message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
username,password,firstname,lastname,email,course1,role1,theme | ||
jonest,jonest,Tom,Jones,jonest@example.com,math102,3,boost | ||
reznor,reznor,Trent,Reznor,reznor@example.com,math102,3,classic | ||
troy,troy,Troy,Barnes,troy@greendale.com,math102,3, | ||
donald,donald,Donald,Barnes,donald@greendale.com,math102,3,somefaketheme |