MDL-75111 phpunit: Move tests to use correct names and ns (take#4)

Applied the following changes to various testcase classes:

- Namespaced with component[\level2-API]
- Moved to level2-API subdirectory when required.
- Fixed incorrect use statements with leading backslash.
- Remove file phpdoc block
- Remove MOODLE_INTERNAL if not needed.
- Changed code to point to global scope when needed.
- Fix some relative paths and comments here and there.
- All them passing individually.
- Complete runs passing too.

Special mention to:

- The following task tests have been moved within the level2 directory:
  - \core\adhoc_task_test => \core\task\adhoc_task_test
  - \core\scheduled_task_test => \core\task\scheduled_task_test
  - \core\calendar_cron_task_test => \core\task\calendar_cron_task_test
  - \core\h5p_get_content_types_task_test => \core\task\h5p_get_content_types_task_test
  - \core\task_database_logger_test => \core\task\database_logger_test
  - \core\task_logging_test => \core\task\logging_test

- The following event tests have been moved within level2 directory:
  - \core\event_context_locked_test => \core\event\context_locked_test
  - \core\event_deprecated_test => \core\event\deprecated_test
  - \core\event_grade_deleted_test => \core\event\grade_deleted_test
  - \core\event_profile_field_test => \core\event\profile_field_test
  - \core\event_unknown_logged_test => \core\event\unknown_logged_test
  - \core\event_user_graded_test => \core\event\user_graded_test
  - \core\event_user_password_updated_test => \core\event\user_password_updated_test

- The following output tests have been moved within level2 directory:
  - \core\mustache_template_finder_test => \core\output\mustache_template_finder_test
  - \core\mustache_template_source_loader_test => \core\output\mustache_template_source_loader_test
  - \core\output_mustache_helper_collection_test => \core\output\mustache_helper_collection_test

- The following tests have been moved to their correct tests directories:
  - lib/tests/time_splittings_test.php => analytics/tests/time_splittings_test.php

- All the classes and tests under lib/filebrowser and lib/filestorage
  belong to core, not to core_files. Some day we should move
  them to their correct subsystem.
- All the classes and tests under lib/grade belong to core, not
  to core_grades. Some day we should move them to their correct
  subsystem.
- The core_grades_external class and its \core\grades_external_test
  unit test should belong to the grades subsystem or, alternatively,
  to \core\external, they both should be moved together.
- The core_grading_external class and its \core\grading_external_test
  unit test should belong to the grading subsystem or, alternatively,
  to \core\external, they both should be moved together.
- The \core\message\message and \core\message\inbound (may be others)
  classes, and their associated tests should go to the core_message
  subsystem.
- The core_user class, and its associated tests should go to the
  core_user subsystem.
- The \core\update namespace is plain wrong (update is not valid API)
  and needs action 1) create it or 2) move elsewhere.
This commit is contained in:
Eloy Lafuente (stronk7)
2022-08-26 16:34:24 +02:00
parent cd11b40558
commit 5c884e835e
125 changed files with 2430 additions and 2726 deletions
+5 -14
View File
@@ -14,17 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for the analysis class.
*
* @package core_analytics
* @copyright 2019 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/fixtures/test_timesplitting_upcoming_seconds.php');
namespace core_analytics;
/**
* Unit tests for the analysis class.
@@ -33,18 +23,19 @@ require_once(__DIR__ . '/fixtures/test_timesplitting_upcoming_seconds.php');
* @copyright 2019 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class analytics_analysis_testcase extends advanced_testcase {
class analysis_test extends \advanced_testcase {
/**
* Test fill_firstanalyses_cache.
* @return null
*/
public function test_fill_firstanalyses_cache() {
require_once(__DIR__ . '/fixtures/test_timesplitting_upcoming_seconds.php');
$this->resetAfterTest();
$modelid = 1;
$params = ['startdate' => (new DateTimeImmutable('-5 seconds'))->getTimestamp()];
$params = ['startdate' => (new \DateTimeImmutable('-5 seconds'))->getTimestamp()];
$course1 = $this->getDataGenerator()->create_course($params);
$course2 = $this->getDataGenerator()->create_course($params);
$analysable1 = new \core_analytics\course($course1);
@@ -72,7 +63,7 @@ class analytics_analysis_testcase extends advanced_testcase {
$this->assertEquals($earliest, $firstanalyses[$modelid . '_' . $course1->id]);
// Upcoming periodic time-splitting methods can read and process the cached data.
$seconds = new test_timesplitting_upcoming_seconds();
$seconds = new \test_timesplitting_upcoming_seconds();
$seconds->set_modelid($modelid);
$seconds->set_analysable($analysable1);
+6 -15
View File
@@ -14,18 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for the calculation info cache.
*
* @package core_analytics
* @copyright 2019 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/fixtures/test_indicator_max.php');
require_once(__DIR__ . '/fixtures/test_indicator_min.php');
namespace core_analytics;
/**
* Unit tests for the calculation info cache.
@@ -34,7 +23,7 @@ require_once(__DIR__ . '/fixtures/test_indicator_min.php');
* @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class analytics_calculation_info_testcase extends advanced_testcase {
class calculation_info_test extends \advanced_testcase {
/**
* test_calculation_info description
@@ -47,12 +36,14 @@ class analytics_calculation_info_testcase extends advanced_testcase {
* @return null
*/
public function test_calculation_info_add_pull($info1, $info2, $info3, $info4) {
require_once(__DIR__ . '/fixtures/test_indicator_max.php');
require_once(__DIR__ . '/fixtures/test_indicator_min.php');
$this->resetAfterTest();
$atimesplitting = new \core\analytics\time_splitting\quarters();
$indicator1 = new test_indicator_min();
$indicator2 = new test_indicator_max();
$indicator1 = new \test_indicator_min();
$indicator2 = new \test_indicator_max();
$calculationinfo = new \core_analytics\calculation_info();
$calculationinfo->add_shared(111, [111 => $info1]);
+2 -10
View File
@@ -14,15 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for the dataset manager.
*
* @package core_analytics
* @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
namespace core_analytics;
/**
* Unit tests for the dataset manager.
@@ -31,7 +23,7 @@ defined('MOODLE_INTERNAL') || die();
* @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class dataset_manager_testcase extends advanced_testcase {
class dataset_manager_test extends \advanced_testcase {
/**
* setUp
+3 -9
View File
@@ -14,13 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for the indicator API.
*
* @package core_analytics
* @copyright 2019 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_analytics;
defined('MOODLE_INTERNAL') || die();
@@ -35,7 +29,7 @@ require_once(__DIR__ . '/fixtures/test_indicator_min.php');
* @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class analytics_indicator_testcase extends advanced_testcase {
class indicator_test extends \advanced_testcase {
/**
* test_validate_calculated_value
@@ -79,7 +73,7 @@ class analytics_indicator_testcase extends advanced_testcase {
->onlyMethods(['calculate_sample'])
->getMock();
$indicatormock->method('calculate_sample')->willReturn($willreturn);
$this->expectException(coding_exception::class);
$this->expectException(\coding_exception::class);
list($values, $unused) = $indicatormock->calculate([1], 'notrelevanthere');
}
+10 -8
View File
@@ -22,6 +22,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_analytics;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/fixtures/test_indicator_max.php');
@@ -39,7 +41,7 @@ require_once(__DIR__ . '/fixtures/test_analysis.php');
* @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class analytics_model_testcase extends advanced_testcase {
class model_test extends \advanced_testcase {
public function setUp(): void {
@@ -183,7 +185,7 @@ class analytics_model_testcase extends advanced_testcase {
global $DB;
$this->resetAfterTest();
$statictarget = new test_static_target_shortname();
$statictarget = new \test_static_target_shortname();
$indicators['test_indicator_max'] = \core_analytics\manager::get_indicator('test_indicator_max');
$model = \core_analytics\model::create($statictarget, $indicators, '\core\analytics\time_splitting\quarters');
$modelobj = $model->get_model_obj();
@@ -301,11 +303,11 @@ class analytics_model_testcase extends advanced_testcase {
$courses[$analysable->get_id()] = $course;
}
$target = new test_target_course_level_shortname();
$target = new \test_target_course_level_shortname();
$analyser = new \core\analytics\analyser\courses(1, $target, [], [], []);
$result = new \core_analytics\local\analysis\result_array(1, false, []);
$analysis = new test_analysis($analyser, false, $result);
$analysis = new \test_analysis($analyser, false, $result);
// Each analysable element takes 0.5 secs minimum (test_analysis), so the max (and likely) number of analysable
// elements that will be processed is 2.
@@ -342,7 +344,7 @@ class analytics_model_testcase extends advanced_testcase {
// Training and prediction data do not get mixed.
$result = new \core_analytics\local\analysis\result_array(1, false, []);
$analysis = new test_analysis($analyser, false, $result);
$analysis = new \test_analysis($analyser, false, $result);
$analysis->run();
$params = array('modelid' => 1, 'action' => 'training');
$this->assertLessThanOrEqual(2, $DB->count_records('analytics_used_analysables', $params));
@@ -402,7 +404,7 @@ class analytics_model_testcase extends advanced_testcase {
$this->model->update(true, [], false);
$this->assertFalse($this->model->can_export_configuration());
$statictarget = new test_static_target_shortname();
$statictarget = new \test_static_target_shortname();
$indicators['test_indicator_max'] = \core_analytics\manager::get_indicator('test_indicator_max');
$model = \core_analytics\model::create($statictarget, $indicators, '\\core\\analytics\\time_splitting\\quarters');
$this->assertFalse($model->can_export_configuration());
@@ -418,7 +420,7 @@ class analytics_model_testcase extends advanced_testcase {
$modelconfig = new \core_analytics\model_config($this->model);
$method = new ReflectionMethod('\\core_analytics\\model_config', 'export_model_data');
$method = new \ReflectionMethod('\\core_analytics\\model_config', 'export_model_data');
$method->setAccessible(true);
$modeldata = $method->invoke($modelconfig);
@@ -566,7 +568,7 @@ class analytics_model_testcase extends advanced_testcase {
private function add_fake_log() {
global $DB, $USER;
$log = new stdClass();
$log = new \stdClass();
$log->modelid = $this->modelobj->id;
$log->version = $this->modelobj->version;
$log->target = $this->modelobj->target;
+2 -8
View File
@@ -14,13 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for prediction actions.
*
* @package core_analytics
* @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_analytics;
defined('MOODLE_INTERNAL') || die();
@@ -34,7 +28,7 @@ require_once(__DIR__ . '/fixtures/test_target_shortname.php');
* @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class analytics_prediction_actions_testcase extends advanced_testcase {
class prediction_actions_test extends \advanced_testcase {
/**
* Common startup tasks
+12 -18
View File
@@ -14,21 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for evaluation, training and prediction.
*
* NOTE: in order to execute this test using a separate server for the
* python ML backend you need to define these variables in your config.php file:
*
* define('TEST_MLBACKEND_PYTHON_HOST', '127.0.0.1');
* define('TEST_MLBACKEND_PYTHON_PORT', 5000);
* define('TEST_MLBACKEND_PYTHON_USERNAME', 'default');
* define('TEST_MLBACKEND_PYTHON_PASSWORD', 'sshhhh');
*
* @package core_analytics
* @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_analytics;
defined('MOODLE_INTERNAL') || die();
@@ -48,11 +34,19 @@ require_once(__DIR__ . '/../../course/lib.php');
/**
* Unit tests for evaluation, training and prediction.
*
* NOTE: in order to execute this test using a separate server for the
* python ML backend you need to define these variables in your config.php file:
*
* define('TEST_MLBACKEND_PYTHON_HOST', '127.0.0.1');
* define('TEST_MLBACKEND_PYTHON_PORT', 5000);
* define('TEST_MLBACKEND_PYTHON_USERNAME', 'default');
* define('TEST_MLBACKEND_PYTHON_PASSWORD', 'sshhhh');
*
* @package core_analytics
* @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_analytics_prediction_testcase extends advanced_testcase {
class prediction_test extends \advanced_testcase {
/**
* Purge all the mlbackend outputs.
@@ -746,7 +740,7 @@ class core_analytics_prediction_testcase extends advanced_testcase {
$samples,
$ranges
);
$dataset = phpunit_util::call_internal_method($analysis, 'calculate_indicators', $params,
$dataset = \phpunit_util::call_internal_method($analysis, 'calculate_indicators', $params,
'\core_analytics\analysis');
$this->assertArrayHasKey('123-0', $dataset);
$this->assertArrayHasKey('123-1', $dataset);
@@ -770,7 +764,7 @@ class core_analytics_prediction_testcase extends advanced_testcase {
$samples,
$ranges
);
$dataset = phpunit_util::call_internal_method($analysis, 'calculate_indicators', $params,
$dataset = \phpunit_util::call_internal_method($analysis, 'calculate_indicators', $params,
'\core_analytics\analysis');
$this->assertArrayNotHasKey('123-0', $dataset);
$this->assertArrayNotHasKey('123-1', $dataset);
+4 -9
View File
@@ -14,14 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Provides the {@link analytics_stats_testcase} class.
*
* @package core_analytics
* @category test
* @copyright 2019 David Mudrák <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_analytics;
defined('MOODLE_INTERNAL') || die();
@@ -31,10 +24,12 @@ require_once(__DIR__ . '/fixtures/test_target_shortname.php');
/**
* Unit tests for the analytics stats.
*
* @package core_analytics
* @category test
* @copyright 2019 David Mudrák <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class analytics_stats_testcase extends advanced_testcase {
class stats_test extends \advanced_testcase {
/**
* Set up the test environment.
@@ -14,30 +14,26 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for core time splitting methods.
*
* @package core
* @category analytics
* @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_analytics;
use test_timesplitting_seconds;
use test_timesplitting_upcoming_seconds;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/../../analytics/tests/fixtures/test_timesplitting_seconds.php');
require_once(__DIR__ . '/../../analytics/tests/fixtures/test_timesplitting_upcoming_seconds.php');
require_once(__DIR__ . '/fixtures/test_timesplitting_seconds.php');
require_once(__DIR__ . '/fixtures/test_timesplitting_upcoming_seconds.php');
require_once(__DIR__ . '/../../lib/enrollib.php');
/**
* Unit tests for core time splitting methods.
*
* @package core
* @category analytics
* @category test
* @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_analytics_time_splittings_testcase extends advanced_testcase {
class time_splittings_test extends \advanced_testcase {
/**
* setUp
@@ -214,7 +210,7 @@ class core_analytics_time_splittings_testcase extends advanced_testcase {
// We now use an ongoing course not yet ready to generate predictions.
$threedaysago = new DateTime('-3 days');
$threedaysago = new \DateTime('-3 days');
$params = array(
'startdate' => $threedaysago->getTimestamp(),
);
@@ -229,7 +225,7 @@ class core_analytics_time_splittings_testcase extends advanced_testcase {
// We now use a ready-to-predict ongoing course.
$onemonthago = new DateTime('-30 days');
$onemonthago = new \DateTime('-30 days');
$params = array(
'startdate' => $onemonthago->getTimestamp(),
);
@@ -284,10 +280,10 @@ class core_analytics_time_splittings_testcase extends advanced_testcase {
// We now check how new ranges get added as time passes.
$fewsecsago = new DateTime('-5 seconds');
$fewsecsago = new \DateTime('-5 seconds');
$params = array(
'startdate' => $fewsecsago->getTimestamp(),
'enddate' => (new DateTimeImmutable('+1 year'))->getTimestamp(),
'enddate' => (new \DateTimeImmutable('+1 year'))->getTimestamp(),
);
$course = $this->getDataGenerator()->create_course($params);
$analysable = new \core_analytics\course($course);
+80 -72
View File
@@ -14,18 +14,26 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core;
use database_column_info;
use moodle_database;
use sql_generator;
use xmldb_field;
use xmldb_index;
use xmldb_key;
use xmldb_structure;
use xmldb_table;
/**
* DDL layer tests.
*
* @package core_ddl
* @category phpunit
* @category test
* @copyright 2008 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
class core_ddl_testcase extends database_driver_testcase {
class ddl_test extends \database_driver_testcase {
/** @var xmldb_table[] keys are table name. Created in setUp. */
private $tables = array();
/** @var array table name => array of stdClass test records loaded into that table. Created in setUp. */
@@ -165,7 +173,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$result = $DB->get_records('test_table0');
} catch (dml_exception $e) {
} catch (\dml_exception $e) {
$result = false;
}
$this->resetDebugging();
@@ -187,7 +195,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$result = $DB->get_records('test_table0');
} catch (dml_exception $e) {
} catch (\dml_exception $e) {
$result = false;
}
$this->resetDebugging();
@@ -274,7 +282,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('ddl_exception', $e);
}
@@ -289,7 +297,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('ddl_exception', $e);
}
@@ -320,7 +328,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
@@ -336,7 +344,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
@@ -365,7 +373,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
@@ -381,7 +389,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
@@ -397,7 +405,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
@@ -413,7 +421,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
@@ -429,7 +437,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
@@ -445,7 +453,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
@@ -461,7 +469,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
@@ -477,7 +485,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
@@ -493,7 +501,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
@@ -509,7 +517,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
@@ -525,7 +533,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
@@ -541,7 +549,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
}
@@ -558,7 +566,7 @@ class core_ddl_testcase extends database_driver_testcase {
$text = str_repeat('š', 1333);
$data = new stdClass();
$data = new \stdClass();
$data->name = 'test';
$table = new xmldb_table('test_innodb');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
@@ -575,7 +583,7 @@ class core_ddl_testcase extends database_driver_testcase {
$expected = (array)$data;
$expected['id'] = (string)$id;
$this->assertEqualsCanonicalizing($expected, (array)$DB->get_record('test_innodb', array('id' => $id)));
} catch (dml_exception $e) {
} catch (\dml_exception $e) {
// Give some nice error message when known problematic MySQL with InnoDB detected.
if ($DB->get_dbfamily() === 'mysql') {
$engine = strtolower($DB->get_dbengine());
@@ -590,7 +598,7 @@ class core_ddl_testcase extends database_driver_testcase {
$dbman->drop_table($table);
$data = new stdClass();
$data = new \stdClass();
$data->name = 'test';
$table = new xmldb_table('test_innodb');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
@@ -613,7 +621,7 @@ class core_ddl_testcase extends database_driver_testcase {
$dbman->drop_table($table);
// MySQL VARCHAR fields may hit a different 65535 row size limit when creating tables.
$data = new stdClass();
$data = new \stdClass();
$data->name = 'test';
$table = new xmldb_table('test_innodb');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
@@ -695,7 +703,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->rename_table($sourcetable, $targettable->getName());
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('ddl_exception', $e);
$this->assertEquals('Table "test_table1" already exists (can not rename table)', $e->getMessage());
}
@@ -714,7 +722,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->field_exists('nonexistenttable', 'id');
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('moodle_exception', $e);
}
@@ -732,7 +740,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->field_exists($nonexistenttable, $realfield);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('moodle_exception', $e);
}
@@ -751,13 +759,13 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$this->assertFalse($dbman->field_exists($nonexistenttable, 'id'));
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('moodle_exception', $e);
}
try {
$this->assertFalse($dbman->field_exists('nonexistenttable', $realfield));
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('moodle_exception', $e);
}
// Non existing fields (return false).
@@ -783,7 +791,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->add_field($table, $field);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('ddl_exception', $e);
}
@@ -793,7 +801,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->add_field($table, $field);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('ddl_exception', $e);
}
@@ -948,7 +956,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->drop_field($table, $field);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('ddl_dependency_exception', $e);
}
$this->assertTrue($dbman->field_exists($table, 'type')); // Continues existing, drop aborted.
@@ -959,7 +967,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->drop_field($table, $field);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('ddl_dependency_exception', $e);
}
$this->assertTrue($dbman->field_exists($table, $field)); // Continues existing, drop aborted.
@@ -970,7 +978,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->drop_field($table, $field);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('ddl_field_missing_exception', $e);
}
@@ -1004,7 +1012,7 @@ class core_ddl_testcase extends database_driver_testcase {
$table->add_index('onenumber', XMLDB_INDEX_NOTUNIQUE, array('onenumber'));
$dbman->create_table($table);
$record = new stdClass();
$record = new \stdClass();
$record->onenumber = 2;
$record->anothernumber = 4;
$recoriginal = $DB->insert_record('test_table_cust0', $record);
@@ -1015,7 +1023,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->change_field_type($table, $field);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('ddl_dependency_exception', $e);
}
// Column continues being integer 10 not null default 2.
@@ -1051,7 +1059,7 @@ class core_ddl_testcase extends database_driver_testcase {
// TODO: check the rest of attributes.
// Insert one string value and try to convert to integer. Must throw exception.
$record = new stdClass();
$record = new \stdClass();
$record->onenumber = 7;
$record->anothernumber = 'string value';
$rectodrop = $DB->insert_record('test_table_cust0', $record);
@@ -1060,7 +1068,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->change_field_type($table, $field);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('ddl_change_structure_exception', $e);
}
// Column continues being char 30 not null default "test'n drop" now.
@@ -1182,7 +1190,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->change_field_precision($table, $field);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('ddl_change_structure_exception', $e);
}
// No changes in field specs at all.
@@ -1216,7 +1224,7 @@ class core_ddl_testcase extends database_driver_testcase {
// TODO: check the rest of attributes.
// Insert one record with 6-digit field.
$record = new stdClass();
$record = new \stdClass();
$record->course = 10;
$record->secondname = 'third record';
$record->intro = 'third record';
@@ -1228,7 +1236,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->change_field_precision($table, $field);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('ddl_change_structure_exception', $e);
}
// No changes in field specs at all.
@@ -1242,7 +1250,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->change_field_precision($table, $field);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('ddl_dependency_exception', $e);
}
// No changes in field specs at all.
@@ -1261,12 +1269,12 @@ class core_ddl_testcase extends database_driver_testcase {
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$record = new stdClass();
$record = new \stdClass();
$record->name = null;
try {
$result = $DB->insert_record('test_table_cust0', $record, false);
} catch (dml_exception $e) {
} catch (\dml_exception $e) {
$result = false;
}
$this->resetDebugging();
@@ -1287,7 +1295,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$result = $DB->insert_record('test_table_cust0', $record, false);
} catch (dml_exception $e) {
} catch (\dml_exception $e) {
$result = false;
}
$this->resetDebugging();
@@ -1311,7 +1319,7 @@ class core_ddl_testcase extends database_driver_testcase {
$field->set_attributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'Moodle2');
$dbman->change_field_default($table, $field);
$record = new stdClass();
$record = new \stdClass();
$record->onenumber = 666;
$id = $DB->insert_record('test_table_cust0', $record);
@@ -1322,7 +1330,7 @@ class core_ddl_testcase extends database_driver_testcase {
$field->set_attributes(XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, 666);
$dbman->change_field_default($table, $field);
$record = new stdClass();
$record = new \stdClass();
$record->name = 'something';
$id = $DB->insert_record('test_table_cust0', $record);
@@ -1343,7 +1351,7 @@ class core_ddl_testcase extends database_driver_testcase {
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$record = new stdClass();
$record = new \stdClass();
$record->onenumber = 666;
$record->name = 'something';
$DB->insert_record('test_table_cust0', $record, false);
@@ -1354,7 +1362,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$result = $DB->insert_record('test_table_cust0', $record, false);
} catch (dml_exception $e) {
} catch (\dml_exception $e) {
$result = false;
}
$this->resetDebugging();
@@ -1375,7 +1383,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->add_index($table, $index);
$this->fail('Exception expected for duplicate indexes');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('ddl_exception', $e);
}
@@ -1384,7 +1392,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->add_index($table, $index);
$this->fail('Exception expected for duplicate indexes');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('ddl_exception', $e);
}
@@ -1398,7 +1406,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$table->add_index('onenumber', XMLDB_INDEX_NOTUNIQUE, array('onenumber'));
$this->fail('Coding exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
@@ -1412,7 +1420,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$table->add_key('onenumber', XMLDB_KEY_FOREIGN, array('onenumber'));
$this->fail('Coding exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
@@ -1614,7 +1622,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->delete_tables_from_xmldb_file('fpsoiudfposui');
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->resetDebugging();
$this->assertInstanceOf('moodle_exception', $e);
}
@@ -1622,7 +1630,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->delete_tables_from_xmldb_file(__DIR__ . '/fixtures/invalid.xml');
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->resetDebugging();
$this->assertInstanceOf('moodle_exception', $e);
}
@@ -1645,7 +1653,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->install_from_xmldb_file('fpsoiudfposui');
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->resetDebugging();
$this->assertInstanceOf('moodle_exception', $e);
}
@@ -1653,7 +1661,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->install_from_xmldb_file(__DIR__ . '/fixtures/invalid.xml');
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->resetDebugging();
$this->assertInstanceOf('moodle_exception', $e);
}
@@ -1680,7 +1688,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->create_temp_table($dupetable);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('ddl_exception', $e);
}
@@ -1689,7 +1697,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->create_table($dupetable);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('ddl_exception', $e);
}
@@ -1726,7 +1734,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->drop_table($noetable);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('ddl_table_missing_exception', $e);
}
@@ -1875,7 +1883,7 @@ class core_ddl_testcase extends database_driver_testcase {
$dbman->drop_table($table0);
$rc = new ReflectionClass('moodle_database');
$rc = new \ReflectionClass('moodle_database');
$rcm = $rc->getMethod('get_temp_tables_cache');
$rcm->setAccessible(true);
$metacachetemp = $rcm->invokeArgs($DB, []);
@@ -1886,7 +1894,7 @@ class core_ddl_testcase extends database_driver_testcase {
// Data of test_table1 should be intact.
$this->assertEquals(true, $metacachetemp->has('test_table1'));
$rc = new ReflectionClass('moodle_database');
$rc = new \ReflectionClass('moodle_database');
$rcm = $rc->getMethod('get_metacache');
$rcm->setAccessible(true);
$metacache = $rcm->invokeArgs($DB, []);
@@ -2009,7 +2017,7 @@ class core_ddl_testcase extends database_driver_testcase {
$tablename = $table->getName();
$this->tables[$tablename] = $table;
$rec = new stdClass();
$rec = new \stdClass();
$rec->name = $maxstr;
$id = $DB->insert_record($tablename, $rec);
@@ -2029,7 +2037,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
}
@@ -2062,7 +2070,7 @@ class core_ddl_testcase extends database_driver_testcase {
$tablename = $table->getName();
$this->tables[$tablename] = $table;
$rec = new stdClass();
$rec = new \stdClass();
$rec->name1 = $maxstr;
$rec->name2 = $reststr;
@@ -2088,7 +2096,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
}
@@ -2116,7 +2124,7 @@ class core_ddl_testcase extends database_driver_testcase {
$maxstr .= 'a'; // Ascii only.
}
$rec = new stdClass();
$rec = new \stdClass();
$rec->name = $maxstr;
$id = $DB->insert_record($tablename, $rec);
@@ -2131,7 +2139,7 @@ class core_ddl_testcase extends database_driver_testcase {
$maxstr .= '言'; // Random long string that should fix exactly the limit for one char column.
}
$rec = new stdClass();
$rec = new \stdClass();
$rec->name = $maxstr;
try {
@@ -2163,7 +2171,7 @@ class core_ddl_testcase extends database_driver_testcase {
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
}
+10 -16
View File
@@ -14,27 +14,21 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* DML Table tests.
*
* @package core_dml
* @category phpunit
* @copyright 2019 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
defined('MOODLE_INTERNAL') || die();
use \core\dml\table;
use core\dml\table;
use xmldb_table;
/**
* DML Table tests.
*
* @package core
* @category test
* @copyright 2019 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core\dml\table
*/
class core_dml_table_testcase extends database_driver_testcase {
class dml_table_test extends \database_driver_testcase {
/**
* Data provider for various \core\dml\table method tests.
@@ -162,15 +156,15 @@ class core_dml_table_testcase extends database_driver_testcase {
* @param array $fieldlist The list of fields
* @param string $primarykey The name of the primary key
* @param string $fieldprefix The prefix to use for each field
* @param stdClass $result The result of the get_records_sql
* @param stdClass $expected The expected output
* @param \stdClass $result The result of the get_records_sql
* @param \stdClass $expected The expected output
*/
public function test_extract_fields_from_result(
array $fieldlist,
string $primarykey,
string $fieldprefix,
stdClass $result,
stdClass $expected
\stdClass $result,
\stdClass $expected
) {
$dbman = $this->tdb->get_manager();
+138 -127
View File
@@ -18,22 +18,33 @@
* DML layer tests.
*
* @package core
* @subpackage dml
* @category test
* @copyright 2008 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
use dml_exception;
use dml_missing_record_exception;
use dml_multiple_records_exception;
use moodle_database;
use moodle_transaction;
use xmldb_key;
use xmldb_table;
defined('MOODLE_INTERNAL') || die();
/**
* DML layer tests.
*
* @package core
* @subpackage dml
* @category test
* @copyright 2008 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \moodle_database
*/
class dml_test extends database_driver_testcase {
class dml_test extends \database_driver_testcase {
protected function setUp(): void {
parent::setUp();
@@ -220,7 +231,7 @@ class dml_test extends database_driver_testcase {
try {
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_DOLLAR, 'param', false);
$this->fail('An Exception is missing, expected due to not supported SQL_PARAMS_DOLLAR');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
$this->assertSame('typenotimplement', $e->errorcode);
}
@@ -230,7 +241,7 @@ class dml_test extends database_driver_testcase {
try {
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false);
$this->fail('An Exception is missing, expected due to empty array of items');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
@@ -362,7 +373,7 @@ class dml_test extends database_driver_testcase {
try {
$DB->fix_sql_params($sql, $params);
$this->fail("Expecting an exception, none occurred");
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
}
@@ -372,7 +383,7 @@ class dml_test extends database_driver_testcase {
try {
$DB->fix_sql_params($sql, $params);
$this->fail("Expecting an exception, none occurred");
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
}
@@ -382,7 +393,7 @@ class dml_test extends database_driver_testcase {
try {
$DB->fix_sql_params($sql, $params);
$this->fail("Expecting an exception, none occurred");
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
}
@@ -399,7 +410,7 @@ class dml_test extends database_driver_testcase {
try {
$DB->fix_sql_params($sql, $params);
$this->fail("Expecting an exception, none occurred");
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
}
@@ -410,7 +421,7 @@ class dml_test extends database_driver_testcase {
try {
$DB->fix_sql_params($sql, $params);
$this->fail("Expecting an exception, none occurred");
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
}
@@ -427,7 +438,7 @@ class dml_test extends database_driver_testcase {
try {
$DB->fix_sql_params($sql, $params);
$this->fail("Expecting an exception, none occurred");
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
@@ -467,7 +478,7 @@ class dml_test extends database_driver_testcase {
$DB = $this->tdb;
require_once($CFG->dirroot . '/lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php');
$fixture = new test_dml_sql_debugging_fixture($this);
$fixture = new \test_dml_sql_debugging_fixture($this);
$sql = "SELECT * FROM {users}";
@@ -501,7 +512,7 @@ SELECT * FROM {users}
-- line 74 of /lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to test_dml_sql_debugging_fixture->one()
-- line 83 of /lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to test_dml_sql_debugging_fixture->two()
-- line 92 of /lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to test_dml_sql_debugging_fixture->three()
-- line 497 of /lib/dml/tests/dml_test.php: call to test_dml_sql_debugging_fixture->four()
-- line 508 of /lib/dml/tests/dml_test.php: call to test_dml_sql_debugging_fixture->four()
EOD;
$this->assertEquals($this->unix_to_os_dirsep($expected), $out);
@@ -557,7 +568,7 @@ EOD;
$this->assertTrue($dbman->table_exists($tablename));
// Test insert record.
$rec1 = new stdClass();
$rec1 = new \stdClass();
$rec1->long_int_columnname_with_28c = 28;
$rec1->long_dec_columnname_with_28c = 28.28;
$rec1->long_str_columnname_with_28c = '28';
@@ -1015,7 +1026,7 @@ EOD;
try {
$DB->execute($sql);
$this->fail("Expecting an exception, none occurred");
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
}
@@ -1146,7 +1157,7 @@ EOD;
try {
$rs = $DB->get_recordset($tablename, $conditions);
$this->fail('An Exception is missing, expected due to equating of text fields');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
$this->assertSame('textconditionsnotallowed', $e->errorcode);
}
@@ -1554,7 +1565,7 @@ EOD;
// Only in debug mode - hopefully all devs test code in debug mode...
$this->fail('An Exception is missing, expected due to equating of text fields');
}
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
$this->assertSame('textconditionsnotallowed', $e->errorcode);
}
@@ -1564,7 +1575,7 @@ EOD;
try {
$records = $DB->get_records('xxxx', array('id' => 0));
$this->fail('An Exception is missing, expected due to query against non-existing table');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
if (debugging()) {
// Information for developers only, normal users get general error message.
@@ -1575,7 +1586,7 @@ EOD;
try {
$records = $DB->get_records('xxxx', array('id' => '1'));
$this->fail('An Exception is missing, expected due to query against non-existing table');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
if (debugging()) {
// Information for developers only, normal users get general error message.
@@ -1587,7 +1598,7 @@ EOD;
try {
$records = $DB->get_records($tablename, array('xxxx' => 0));
$this->fail('An Exception is missing, expected due to query against non-existing column');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
if (debugging()) {
// Information for developers only, normal users get general error message.
@@ -1855,7 +1866,7 @@ EOD;
$DB->insert_record($tablename, array('course' => 2));
$record = $DB->get_record($tablename, array('id' => 2));
$this->assertInstanceOf('stdClass', $record);
$this->assertInstanceOf(\stdClass::class, $record);
$this->assertEquals(2, $record->course);
$this->assertEquals(2, $record->id);
@@ -1878,7 +1889,7 @@ EOD;
$DB->insert_record($tablename, array('course' => 2));
$record = $DB->get_record_select($tablename, "id = ?", array(2));
$this->assertInstanceOf('stdClass', $record);
$this->assertInstanceOf(\stdClass::class, $record);
$this->assertEquals(2, $record->course);
@@ -1902,7 +1913,7 @@ EOD;
// Standard use.
$record = $DB->get_record_sql("SELECT * FROM {{$tablename}} WHERE id = ?", array(2));
$this->assertInstanceOf('stdClass', $record);
$this->assertInstanceOf(\stdClass::class, $record);
$this->assertEquals(2, $record->course);
$this->assertEquals(2, $record->id);
@@ -1983,7 +1994,7 @@ EOD;
// Only in debug mode - hopefully all devs test code in debug mode...
$this->fail('An Exception is missing, expected due to equating of text fields');
}
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
$this->assertSame('textconditionsnotallowed', $e->errorcode);
}
@@ -2106,7 +2117,7 @@ EOD;
$this->assertEquals($record, $before);
$record = $DB->get_record($tablename, array('course' => 1));
$this->assertInstanceOf('stdClass', $record);
$this->assertInstanceOf(\stdClass::class, $record);
$this->assertSame('xx', $record->onechar);
$result = $DB->insert_record_raw($tablename, array('course' => 2, 'onechar' => 'yy'), false);
@@ -2115,21 +2126,21 @@ EOD;
// Note: bulk not implemented yet.
$DB->insert_record_raw($tablename, array('course' => 3, 'onechar' => 'zz'), true, true);
$record = $DB->get_record($tablename, array('course' => 3));
$this->assertInstanceOf('stdClass', $record);
$this->assertInstanceOf(\stdClass::class, $record);
$this->assertSame('zz', $record->onechar);
// Custom sequence (id) - returnid is ignored.
$result = $DB->insert_record_raw($tablename, array('id' => 10, 'course' => 3, 'onechar' => 'bb'), true, false, true);
$this->assertTrue($result);
$record = $DB->get_record($tablename, array('id' => 10));
$this->assertInstanceOf('stdClass', $record);
$this->assertInstanceOf(\stdClass::class, $record);
$this->assertSame('bb', $record->onechar);
// Custom sequence - missing id error.
try {
$DB->insert_record_raw($tablename, array('course' => 3, 'onechar' => 'bb'), true, false, true);
$this->fail('Exception expected due to missing record');
} catch (coding_exception $ex) {
} catch (\coding_exception $ex) {
$this->assertTrue(true);
}
@@ -2192,7 +2203,7 @@ EOD;
$this->assertEquals(99, $record->course);
// Check nulls are set properly for all types.
$record = new stdClass();
$record = new \stdClass();
$record->oneint = null;
$record->onenum = null;
$record->onechar = null;
@@ -2208,7 +2219,7 @@ EOD;
$this->assertNull($record->onebinary);
// Check zeros are set properly for all types.
$record = new stdClass();
$record = new \stdClass();
$record->oneint = 0;
$record->onenum = 0;
$recid = $DB->insert_record($tablename, $record);
@@ -2217,7 +2228,7 @@ EOD;
$this->assertEquals(0, $record->onenum);
// Check booleans are set properly for all types.
$record = new stdClass();
$record = new \stdClass();
$record->oneint = true; // Trues.
$record->onenum = true;
$record->onechar = true;
@@ -2229,7 +2240,7 @@ EOD;
$this->assertEquals(1, $record->onechar);
$this->assertEquals(1, $record->onetext);
$record = new stdClass();
$record = new \stdClass();
$record->oneint = false; // Falses.
$record->onenum = false;
$record->onechar = false;
@@ -2242,34 +2253,34 @@ EOD;
$this->assertEquals(0, $record->onetext);
// Check string data causes exception in numeric types.
$record = new stdClass();
$record = new \stdClass();
$record->oneint = 'onestring';
$record->onenum = 0;
try {
$DB->insert_record($tablename, $record);
$this->fail("Expecting an exception, none occurred");
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
}
$record = new stdClass();
$record = new \stdClass();
$record->oneint = 0;
$record->onenum = 'onestring';
try {
$DB->insert_record($tablename, $record);
$this->fail("Expecting an exception, none occurred");
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
}
// Check empty string data is stored as 0 in numeric datatypes.
$record = new stdClass();
$record = new \stdClass();
$record->oneint = ''; // Empty string.
$record->onenum = 0;
$recid = $DB->insert_record($tablename, $record);
$record = $DB->get_record($tablename, array('id' => $recid));
$this->assertTrue(is_numeric($record->oneint) && $record->oneint == 0);
$record = new stdClass();
$record = new \stdClass();
$record->oneint = 0;
$record->onenum = ''; // Empty string.
$recid = $DB->insert_record($tablename, $record);
@@ -2277,7 +2288,7 @@ EOD;
$this->assertTrue(is_numeric($record->onenum) && $record->onenum == 0);
// Check empty strings are set properly in string types.
$record = new stdClass();
$record = new \stdClass();
$record->oneint = 0;
$record->onenum = 0;
$record->onechar = '';
@@ -2288,7 +2299,7 @@ EOD;
$this->assertTrue($record->onetext === '');
// Check operation ((210.10 + 39.92) - 150.02) against numeric types.
$record = new stdClass();
$record = new \stdClass();
$record->oneint = ((210.10 + 39.92) - 150.02);
$record->onenum = ((210.10 + 39.92) - 150.02);
$recid = $DB->insert_record($tablename, $record);
@@ -2303,7 +2314,7 @@ EOD;
'backslashes and quotes sequences (even): \\"\\" \\\'\\\'',
'backslashes and quotes sequences (odd): \\"\\"\\" \\\'\\\'\\\'');
foreach ($teststrings as $teststring) {
$record = new stdClass();
$record = new \stdClass();
$record->onechar = $teststring;
$record->onetext = $teststring;
$recid = $DB->insert_record($tablename, $record);
@@ -2315,7 +2326,7 @@ EOD;
// Check LOBs in text/binary columns.
$clob = file_get_contents(__DIR__ . '/fixtures/clob.txt');
$blob = file_get_contents(__DIR__ . '/fixtures/randombinary');
$record = new stdClass();
$record = new \stdClass();
$record->onetext = $clob;
$record->onebinary = $blob;
$recid = $DB->insert_record($tablename, $record);
@@ -2328,7 +2339,7 @@ EOD;
// And "small" LOBs too, just in case.
$newclob = substr($clob, 0, 500);
$newblob = substr($blob, 0, 250);
$record = new stdClass();
$record = new \stdClass();
$record->onetext = $newclob;
$record->onebinary = $newblob;
$recid = $DB->insert_record($tablename, $record);
@@ -2342,7 +2353,7 @@ EOD;
// And "diagnostic" LOBs too, just in case.
$newclob = '\'"\\;/ěščřžýáíé';
$newblob = '\'"\\;/ěščřžýáíé';
$record = new stdClass();
$record = new \stdClass();
$record->onetext = $newclob;
$record->onebinary = $newblob;
$recid = $DB->insert_record($tablename, $record);
@@ -2354,7 +2365,7 @@ EOD;
$this->assertEquals(false, $rs->key()); // Ensure recordset key() method to be working ok after closing.
// Test data is not modified.
$record = new stdClass();
$record = new \stdClass();
$record->id = -1; // Has to be ignored.
$record->course = 3;
$record->lalala = 'lalal'; // Unused.
@@ -2417,7 +2428,7 @@ EOD;
try {
$DB->insert_record($tablename, $record, false);
$this->fail("Expecting an exception, none occurred");
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
}
@@ -2425,7 +2436,7 @@ EOD;
try {
$DB->insert_record($tablename, $record, true);
$this->fail("Expecting an exception, none occurred");
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
}
@@ -2433,7 +2444,7 @@ EOD;
try {
$DB->insert_record('nonexistenttable', $record, true);
$this->fail("Expecting an exception, none occurred");
} catch (exception $e) {
} catch (\Exception $e) {
$this->assertTrue($e instanceof dml_exception);
}
}
@@ -2456,7 +2467,7 @@ EOD;
$this->assertCount(0, $DB->get_records($tablename));
$record = new stdClass();
$record = new \stdClass();
$record->id = '1';
$record->course = '1';
$record->oneint = null;
@@ -2514,7 +2525,7 @@ EOD;
try {
$DB->insert_records($tablename, $records);
$this->fail('coding_exception expected when insert_records receives different object data structures');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
@@ -2525,7 +2536,7 @@ EOD;
try {
$DB->insert_records($tablename, $records);
$this->fail('coding_exception expected when insert_records receives different object data structures');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
@@ -2533,7 +2544,7 @@ EOD;
try {
$DB->insert_records($tablename, $records);
$this->fail('coding_exception expected when insert_records receives non-traversable data');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
@@ -2541,7 +2552,7 @@ EOD;
try {
$DB->insert_records($tablename, $records);
$this->fail('coding_exception expected when insert_records receives non-objet record');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
}
@@ -2572,7 +2583,7 @@ EOD;
try {
$DB->insert_record($tablename, (object) ['notnull1' => 1, 'nullable1' => 1, 'nullable2' => 1]);
$this->fail('dml_write_exception expected when a record violates a unique index');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_write_exception', $e);
}
@@ -2646,7 +2657,7 @@ EOD;
$this->assertEquals(2, $records[13]->course);
// Check nulls are set properly for all types.
$record = new stdClass();
$record = new \stdClass();
$record->id = 20;
$record->oneint = null;
$record->onenum = null;
@@ -2663,7 +2674,7 @@ EOD;
$this->assertNull($record->onebinary);
// Check zeros are set properly for all types.
$record = new stdClass();
$record = new \stdClass();
$record->id = 23;
$record->oneint = 0;
$record->onenum = 0;
@@ -2673,29 +2684,29 @@ EOD;
$this->assertEquals(0, $record->onenum);
// Check string data causes exception in numeric types.
$record = new stdClass();
$record = new \stdClass();
$record->id = 32;
$record->oneint = 'onestring';
$record->onenum = 0;
try {
$DB->import_record($tablename, $record);
$this->fail("Expecting an exception, none occurred");
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
}
$record = new stdClass();
$record = new \stdClass();
$record->id = 35;
$record->oneint = 0;
$record->onenum = 'onestring';
try {
$DB->import_record($tablename, $record);
$this->fail("Expecting an exception, none occurred");
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
}
// Check empty strings are set properly in string types.
$record = new stdClass();
$record = new \stdClass();
$record->id = 44;
$record->oneint = 0;
$record->onenum = 0;
@@ -2707,7 +2718,7 @@ EOD;
$this->assertTrue($record->onetext === '');
// Check operation ((210.10 + 39.92) - 150.02) against numeric types.
$record = new stdClass();
$record = new \stdClass();
$record->id = 47;
$record->oneint = ((210.10 + 39.92) - 150.02);
$record->onenum = ((210.10 + 39.92) - 150.02);
@@ -2724,7 +2735,7 @@ EOD;
'backslashes and quotes sequences (even): \\"\\" \\\'\\\'',
'backslashes and quotes sequences (odd): \\"\\"\\" \\\'\\\'\\\'');
foreach ($teststrings as $teststring) {
$record = new stdClass();
$record = new \stdClass();
$record->id = $i;
$record->onechar = $teststring;
$record->onetext = $teststring;
@@ -2737,7 +2748,7 @@ EOD;
// Check LOBs in text/binary columns.
$clob = file_get_contents(__DIR__ . '/fixtures/clob.txt');
$record = new stdClass();
$record = new \stdClass();
$record->id = 70;
$record->onetext = $clob;
$record->onebinary = '';
@@ -2748,7 +2759,7 @@ EOD;
$this->assertEquals($clob, $record->onetext, 'Test CLOB insert (full contents output disabled)');
$blob = file_get_contents(__DIR__ . '/fixtures/randombinary');
$record = new stdClass();
$record = new \stdClass();
$record->id = 71;
$record->onetext = '';
$record->onebinary = $blob;
@@ -2761,7 +2772,7 @@ EOD;
// And "small" LOBs too, just in case.
$newclob = substr($clob, 0, 500);
$newblob = substr($blob, 0, 250);
$record = new stdClass();
$record = new \stdClass();
$record->id = 73;
$record->onetext = $newclob;
$record->onebinary = $newblob;
@@ -2801,7 +2812,7 @@ EOD;
try {
$DB->update_record_raw($tablename, $record);
$this->fail("Expecting an exception, none occurred");
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('moodle_exception', $e);
}
@@ -2810,7 +2821,7 @@ EOD;
try {
$DB->update_record_raw($tablename, $record);
$this->fail("Expecting an exception, none occurred");
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
}
@@ -2900,7 +2911,7 @@ EOD;
try {
$DB->update_record($tablename, $record);
$this->fail("Expecting an exception, none occurred");
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
}
$record->oneint = 0;
@@ -2908,7 +2919,7 @@ EOD;
try {
$DB->update_record($tablename, $record);
$this->fail("Expecting an exception, none occurred");
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
}
@@ -3070,7 +3081,7 @@ EOD;
// Only in debug mode - hopefully all devs test code in debug mode...
$this->fail('An Exception is missing, expected due to equating of text fields');
}
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
$this->assertSame('textconditionsnotallowed', $e->errorcode);
}
@@ -3176,13 +3187,13 @@ EOD;
try {
$DB->set_field_select($tablename, 'oneint', 'onestring', 'id = ?', array(1));
$this->fail("Expecting an exception, none occurred");
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
}
try {
$DB->set_field_select($tablename, 'onenum', 'onestring', 'id = ?', array(1));
$this->fail("Expecting an exception, none occurred");
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
}
@@ -3285,7 +3296,7 @@ EOD;
// Only in debug mode - hopefully all devs test code in debug mode...
$this->fail('An Exception is missing, expected due to equating of text fields');
}
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
$this->assertSame('textconditionsnotallowed', $e->errorcode);
}
@@ -3338,14 +3349,14 @@ EOD;
try {
$DB->count_records_sql("SELECT onechar FROM {{$tablename}} WHERE course = ?", array(3));
$this->fail('Exception expected when non-number field used in count_records_sql');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
try {
$DB->count_records_sql("SELECT course FROM {{$tablename}} WHERE 1 = 2");
$this->fail('Exception expected when non-number field used in count_records_sql');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
}
@@ -3378,7 +3389,7 @@ EOD;
// Only in debug mode - hopefully all devs test code in debug mode...
$this->fail('An Exception is missing, expected due to equating of text fields');
}
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
$this->assertSame('textconditionsnotallowed', $e->errorcode);
}
@@ -3529,7 +3540,7 @@ EOD;
// Only in debug mode - hopefully all devs test code in debug mode...
$this->fail('An Exception is missing, expected due to equating of text fields');
}
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
$this->assertSame('textconditionsnotallowed', $e->errorcode);
}
@@ -3542,7 +3553,7 @@ EOD;
// Only in debug mode - hopefully all devs test code in debug mode...
$this->fail('An Exception is missing, expected due to equating of text fields');
}
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
$this->assertSame('textconditionsnotallowed', $e->errorcode);
}
@@ -3625,11 +3636,11 @@ EOD;
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$o = new stdClass(); // Objects without __toString - never worked.
$o = new \stdClass(); // Objects without __toString - never worked.
try {
$DB->fix_sql_params("SELECT {{$tablename}} WHERE course = ? ", array($o));
$this->fail('coding_exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
@@ -3638,89 +3649,89 @@ EOD;
try {
$DB->fix_sql_params("SELECT {{$tablename}} WHERE course = ? ", array($o));
$this->fail('coding_exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
try {
$DB->execute("SELECT {{$tablename}} WHERE course = ? ", array($o));
$this->fail('coding_exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
try {
$DB->get_recordset_sql("SELECT {{$tablename}} WHERE course = ? ", array($o));
$this->fail('coding_exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
try {
$DB->get_records_sql("SELECT {{$tablename}} WHERE course = ? ", array($o));
$this->fail('coding_exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
try {
$record = new stdClass();
$record = new \stdClass();
$record->course = $o;
$DB->insert_record_raw($tablename, $record);
$this->fail('coding_exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
try {
$record = new stdClass();
$record = new \stdClass();
$record->course = $o;
$DB->insert_record($tablename, $record);
$this->fail('coding_exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
try {
$record = new stdClass();
$record = new \stdClass();
$record->course = $o;
$DB->import_record($tablename, $record);
$this->fail('coding_exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
try {
$record = new stdClass();
$record = new \stdClass();
$record->id = 1;
$record->course = $o;
$DB->update_record_raw($tablename, $record);
$this->fail('coding_exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
try {
$record = new stdClass();
$record = new \stdClass();
$record->id = 1;
$record->course = $o;
$DB->update_record($tablename, $record);
$this->fail('coding_exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
try {
$DB->set_field_select($tablename, 'course', 1, "course = ? ", array($o));
$this->fail('coding_exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
try {
$DB->delete_records_select($tablename, "course = ? ", array($o));
$this->fail('coding_exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
}
@@ -4032,7 +4043,7 @@ EOD;
try {
$DB->insert_record($tablename, array('name'=>'AAA'));
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
// TODO: ignore case insensitive uniqueness problems for now.
// $this->fail("Unique index is case sensitive - this may cause problems in some tables");
}
@@ -4041,7 +4052,7 @@ EOD;
$DB->insert_record($tablename, array('name'=>'aäa'));
$DB->insert_record($tablename, array('name'=>'aáa'));
$this->assertTrue(true);
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$family = $DB->get_dbfamily();
if ($family === 'mysql' or $family === 'mssql') {
$this->fail("Unique index is accent insensitive, this may cause problems for non-ascii languages. This is usually caused by accent insensitive default collation.");
@@ -4575,9 +4586,9 @@ EOD;
// Silence php warning.
@$DB->sql_substr("name");
$this->fail("Expecting an exception, none occurred");
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
} catch (Error $error) {
} catch (\Error $error) {
// PHP 7.1 throws Error even earlier.
$this->assertMatchesRegularExpression('/Too few arguments to function/', $error->getMessage());
}
@@ -5053,7 +5064,7 @@ EOD;
$long = '1234567890123456789';
$DB->replace_all_text($tablename, $columns['name'], 'kk', $long);
$result = $DB->get_records($tablename, array(), 'id ASC');
$expected[$id5]->name = core_text::substr($long.'llll', 0, 20);
$expected[$id5]->name = \core_text::substr($long.'llll', 0, 20);
$this->assertEquals($expected, $result);
$DB->replace_all_text($tablename, $columns['intro'], 'kk', $long);
@@ -5103,7 +5114,7 @@ EOD;
$this->assertEquals(1, $DB->count_records($tablename));
try {
$DB->insert_record($tablename, (object)array('course'=>1));
} catch (Exception $e) {
} catch (\Exception $e) {
// This must be ignored and it must not roll back the whole transaction.
}
$DB->insert_record($tablename, (object)array('course'=>2));
@@ -5120,7 +5131,7 @@ EOD;
$this->assertEquals(1, $DB->count_records($tablename));
try {
$DB->get_records_sql('s e l e c t');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
// This must be ignored and it must not roll back the whole transaction.
}
$DB->insert_record($tablename, (object)array('course'=>2));
@@ -5137,7 +5148,7 @@ EOD;
$this->assertEquals(1, $DB->count_records($tablename));
try {
$DB->execute('xxxx');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
// This must be ignored and it must not roll back the whole transaction.
}
$DB->insert_record($tablename, (object)array('course'=>2));
@@ -5154,7 +5165,7 @@ EOD;
$this->assertEquals(1, $DB->count_records($tablename));
try {
$DB->change_database_structure('xxxx');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
// This must be ignored and it must not roll back the whole transaction.
}
$DB->insert_record($tablename, (object)array('course'=>2));
@@ -5186,9 +5197,9 @@ EOD;
$DB->insert_record($tablename, $data);
$this->assertEquals(1, $DB->count_records($tablename));
try {
$transaction->rollback(new Exception('test'));
$transaction->rollback(new \Exception('test'));
$this->fail('transaction rollback must rethrow exception');
} catch (Exception $e) {
} catch (\Exception $e) {
// Ignored.
}
$this->assertEquals(0, $DB->count_records($tablename));
@@ -5232,9 +5243,9 @@ EOD;
$DB->insert_record($tablename, $data);
$transaction2->allow_commit();
try {
$transaction1->rollback(new Exception('test'));
$transaction1->rollback(new \Exception('test'));
$this->fail('transaction rollback must rethrow exception');
} catch (Exception $e) {
} catch (\Exception $e) {
$this->assertEquals(get_class($e), 'Exception');
}
$this->assertEquals(0, $DB->count_records($tablename));
@@ -5249,15 +5260,15 @@ EOD;
$data = (object)array('course'=>4);
$DB->insert_record($tablename, $data);
try {
$transaction2->rollback(new Exception('test'));
$transaction2->rollback(new \Exception('test'));
$this->fail('transaction rollback must rethrow exception');
} catch (Exception $e) {
} catch (\Exception $e) {
$this->assertEquals(get_class($e), 'Exception');
}
$this->assertEquals(2, $DB->count_records($tablename)); // Not rolled back yet.
try {
$transaction1->allow_commit();
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_transaction_exception', $e);
}
$this->assertEquals(2, $DB->count_records($tablename)); // Not rolled back yet.
@@ -5355,7 +5366,7 @@ EOD;
$DB->insert_record($tablename, $data);
try {
$DB->transactions_forbidden();
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_transaction_exception', $e);
}
// The previous test does not force rollback.
@@ -5386,13 +5397,13 @@ EOD;
try {
$transaction1->allow_commit();
$this->fail('wrong order of commits must throw exception');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_transaction_exception', $e);
}
try {
$transaction2->allow_commit();
$this->fail('first wrong commit forces rollback');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_transaction_exception', $e);
}
// This is done in default exception handler usually.
@@ -5411,18 +5422,18 @@ EOD;
$DB->insert_record($tablename, $data);
try {
// This first rollback should prevent all other rollbacks.
$transaction1->rollback(new Exception('test'));
} catch (Exception $e) {
$transaction1->rollback(new \Exception('test'));
} catch (\Exception $e) {
$this->assertEquals(get_class($e), 'Exception');
}
try {
$transaction2->rollback(new Exception('test'));
} catch (Exception $e) {
$transaction2->rollback(new \Exception('test'));
} catch (\Exception $e) {
$this->assertEquals(get_class($e), 'Exception');
}
try {
$transaction1->rollback(new Exception('test'));
} catch (moodle_exception $e) {
$transaction1->rollback(new \Exception('test'));
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_transaction_exception', $e);
}
// This is done in default exception handler usually.
@@ -5438,13 +5449,13 @@ EOD;
try {
$transaction2->allow_commit();
$this->fail('foreign transaction must fail');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_transaction_exception', $e);
}
try {
$transaction1->allow_commit();
$this->fail('first wrong commit forces rollback');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_transaction_exception', $e);
}
$DB->force_transaction_rollback();
@@ -5549,7 +5560,7 @@ EOD;
$DB2->get_session_lock($rowid, $timeout);
$DB2->release_session_lock($rowid); // Should not be executed, but here for safety.
$this->fail('An Exception is missing, expected due to session lock acquired.');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_sessionwait_exception', $e);
$DB->release_session_lock($rowid); // Release lock on connection1.
}
@@ -5884,7 +5895,7 @@ EOD;
// Inserts counts as writes.
$rec1 = new stdClass();
$rec1 = new \stdClass();
$rec1->fieldvalue = 11;
$rec1->id = $DB->insert_record($tablename, $rec1);
$this->assertEquals($initwrites + 1, $DB->perf_get_writes());
@@ -5895,7 +5906,7 @@ EOD;
$this->assertGreaterThanOrEqual($previousqueriestime, $lastqueriestime);
$previousqueriestime = $lastqueriestime;
$rec2 = new stdClass();
$rec2 = new \stdClass();
$rec2->fieldvalue = 22;
$rec2->id = $DB->insert_record($tablename, $rec2);
$this->assertEquals($initwrites + 2, $DB->perf_get_writes());
+5 -14
View File
@@ -14,26 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Test \core\dml\recordset_walk.
*
* @package core
* @category dml
* @copyright 2015 David Monllao
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
namespace core;
/**
* Test case for recordset_walk.
*
* @package core
* @category dml
* @category test
* @copyright 2015 David Monllao
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_recordset_walk_testcase extends advanced_testcase {
class recordset_walk_test extends \advanced_testcase {
public function setUp(): void {
parent::setUp();
@@ -113,7 +104,7 @@ class core_recordset_walk_testcase extends advanced_testcase {
/**
* Simple callback requiring 1 row fields.
*
* @param stdClass $data
* @param \stdClass $data
* @return \Traversable
*/
public function simple_callback($data, $nothing = 'notpassed') {
@@ -126,7 +117,7 @@ class core_recordset_walk_testcase extends advanced_testcase {
/**
* Callback requiring 1 row fields + other params.
*
* @param stdClass $data
* @param \stdClass $data
* @param mixed $extra
* @return \Traversable
*/
@@ -15,14 +15,18 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Test sqlsrv dml support.
* Test case for sqlsrv dml support.
*
* @package core
* @category dml
* @category test
* @copyright 2017 John Okely
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
use sqlsrv_native_moodle_database;
defined('MOODLE_INTERNAL') || die();
global $CFG;
@@ -32,11 +36,11 @@ require_once($CFG->dirroot.'/lib/dml/sqlsrv_native_moodle_database.php');
* Test case for sqlsrv dml support.
*
* @package core
* @category dml
* @category test
* @copyright 2017 John Okely
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class sqlsrv_native_moodle_database_testcase extends advanced_testcase {
class sqlsrv_native_moodle_database_test extends \advanced_testcase {
public function setUp(): void {
parent::setUp();
@@ -118,7 +122,7 @@ class sqlsrv_native_moodle_database_testcase extends advanced_testcase {
public function test_add_no_lock_to_temp_tables($input, $expected) {
$sqlsrv = new sqlsrv_native_moodle_database();
$reflector = new ReflectionObject($sqlsrv);
$reflector = new \ReflectionObject($sqlsrv);
$method = $reflector->getMethod('add_no_lock_to_temp_tables');
$method->setAccessible(true);
@@ -247,7 +251,7 @@ EOT
$this->assertSame($expectedmainquery, $mainquery);
// The has_query_order_by static method is protected. Use Reflection to call the method.
$method = new ReflectionMethod('sqlsrv_native_moodle_database', 'has_query_order_by');
$method = new \ReflectionMethod('sqlsrv_native_moodle_database', 'has_query_order_by');
$method->setAccessible(true);
$result = $method->invoke(null, $sql);
$this->assertSame($expectedresult, $result);
+10 -6
View File
@@ -18,23 +18,27 @@
* TinyMCE tests.
*
* @package editor_tinymce
* @category phpunit
* @category test
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
namespace editor_tinymce;
use editor_tinymce_plugin;
use tinymce_texteditor;
defined('MOODLE_INTERNAL') || die();
/**
* TinyMCE tests.
*
* @package editor_tinymce
* @category phpunit
* @category test
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class editor_tinymce_testcase extends advanced_testcase {
class editor_test extends \advanced_testcase {
public function test_autoloading() {
// Note: This test core_frankestyle calssloader.
@@ -163,7 +167,7 @@ class editor_tinymce_testcase extends advanced_testcase {
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tinymce_testplugin extends editor_tinymce_plugin {
protected function update_init_params(array &$params, context $context, array $options = null) {
protected function update_init_params(array &$params, \context $context, array $options = null) {
// Empty function just to make a class not abstract.
}
@@ -182,4 +186,4 @@ class tinymce_testplugin extends editor_tinymce_plugin {
public function test_add_button_before(array &$params, $row, $button, $before = '', $alwaysadd = true) {
return parent::add_button_before($params, $row, $button, $before, $alwaysadd);
}
}
}
+19 -22
View File
@@ -14,24 +14,21 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for file browser
*
* @package core_files
* @copyright 2017 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
defined('MOODLE_INTERNAL') || die();
use file_info_context_course;
use file_info_context_coursecat;
use file_info_context_module;
use file_info_stored;
/**
* Unit tests for file browser
*
* @package core_files
* @package core
* @copyright 2017 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class file_browser_testcase extends advanced_testcase {
class file_browser_test extends \advanced_testcase {
/** @var int */
protected $initialnonempty;
@@ -66,7 +63,7 @@ class file_browser_testcase extends advanced_testcase {
$this->setAdminUser();
$browser = get_file_browser();
$fileinfo = $browser->get_file_info(context_system::instance());
$fileinfo = $browser->get_file_info(\context_system::instance());
$this->initialnonempty = $fileinfo->count_non_empty_children();
$this->initialcategories = count(array_filter($fileinfo->get_children(), function($a) {
return $a instanceof file_info_context_coursecat;
@@ -83,7 +80,7 @@ class file_browser_testcase extends advanced_testcase {
$this->course2 = $this->getDataGenerator()->create_course();
// Add a file to course1 summary.
$coursecontext1 = context_course::instance($this->course1->id);
$coursecontext1 = \context_course::instance($this->course1->id);
$this->course1filerecord = array('contextid' => $coursecontext1->id,
'component' => 'course',
'filearea' => 'summary',
@@ -116,7 +113,7 @@ class file_browser_testcase extends advanced_testcase {
// There is one non-empty category child and two category children.
$browser = get_file_browser();
$fileinfo = $browser->get_file_info(context_system::instance());
$fileinfo = $browser->get_file_info(\context_system::instance());
$this->assertNotEmpty($fileinfo->count_non_empty_children());
$this->assertEquals($this->initialnonempty + 1, count($fileinfo->get_non_empty_children()));
$categorychildren = array_filter($fileinfo->get_children(), function($a) {
@@ -131,11 +128,11 @@ class file_browser_testcase extends advanced_testcase {
public function test_file_info_context_system_hidden() {
// Hide the course category that contains our two courses. Teacher does not have cap to view hidden categories.
core_course_category::get($this->course1->category)->update(['visible' => 0]);
\core_course_category::get($this->course1->category)->update(['visible' => 0]);
// We should have two non-empty children in system context (courses).
$browser = get_file_browser();
$fileinfo = $browser->get_file_info(context_system::instance());
$fileinfo = $browser->get_file_info(\context_system::instance());
$this->assertNotEmpty($fileinfo->count_non_empty_children());
$this->assertEquals($this->initialnonempty + 2, count($fileinfo->get_non_empty_children()));
@@ -160,7 +157,7 @@ class file_browser_testcase extends advanced_testcase {
// There are two non-empty courses.
$browser = get_file_browser();
$fileinfo = $browser->get_file_info(context_coursecat::instance($this->course2->category));
$fileinfo = $browser->get_file_info(\context_coursecat::instance($this->course2->category));
$this->assertNotEmpty($fileinfo->count_non_empty_children());
$this->assertEquals(2, count($fileinfo->get_non_empty_children()));
$coursechildren = array_filter($fileinfo->get_children(), function($a) {
@@ -177,7 +174,7 @@ class file_browser_testcase extends advanced_testcase {
// There is one non-empty category child and two category children.
$browser = get_file_browser();
$fileinfo = $browser->get_file_info(context_system::instance());
$fileinfo = $browser->get_file_info(\context_system::instance());
$this->assertNotEmpty($fileinfo->count_non_empty_children(['.jpg']));
$this->assertEquals($this->initialjpg + 1, count($fileinfo->get_non_empty_children(['.jpg'])));
}
@@ -188,7 +185,7 @@ class file_browser_testcase extends advanced_testcase {
public function test_file_info_context_course_1() {
$browser = get_file_browser();
$fileinfo = $browser->get_file_info(context_course::instance($this->course1->id));
$fileinfo = $browser->get_file_info(\context_course::instance($this->course1->id));
// Fileinfo element has only one non-empty child - "Course summary" file area.
$this->assertNotEmpty($fileinfo->count_non_empty_children());
$nonemptychildren = $fileinfo->get_non_empty_children();
@@ -209,7 +206,7 @@ class file_browser_testcase extends advanced_testcase {
// Admin can see seven course-level file areas.
$this->setAdminUser();
$fileinfo = $browser->get_file_info(context_course::instance($this->course1->id));
$fileinfo = $browser->get_file_info(\context_course::instance($this->course1->id));
$this->assertEquals(7, count($fileinfo->get_children()));
}
@@ -220,7 +217,7 @@ class file_browser_testcase extends advanced_testcase {
// 2. Start from the course level.
$browser = get_file_browser();
$fileinfo = $browser->get_file_info(context_course::instance($this->course2->id));
$fileinfo = $browser->get_file_info(\context_course::instance($this->course2->id));
$this->assertNotEmpty($fileinfo->count_non_empty_children());
$nonemptychildren = $fileinfo->get_non_empty_children();
$this->assertEquals(1, count($nonemptychildren));
@@ -240,7 +237,7 @@ class file_browser_testcase extends advanced_testcase {
*/
public function test_file_info_context_module_1() {
$module1context = context_module::instance($this->module1->cmid);
$module1context = \context_module::instance($this->module1->cmid);
$browser = get_file_browser();
$fileinfo = $browser->get_file_info($module1context);
$this->assertEquals($this->module1->name . ' (File)', $fileinfo->get_visible_name());
@@ -256,7 +253,7 @@ class file_browser_testcase extends advanced_testcase {
*/
public function test_file_info_context_module_2() {
$module2context = context_module::instance($this->module2->cmid);
$module2context = \context_module::instance($this->module2->cmid);
$browser = get_file_browser();
$fileinfo = $browser->get_file_info($module2context);
$this->assertEquals($this->module2->name.' (Assignment)', $fileinfo->get_visible_name());
+48 -38
View File
@@ -17,12 +17,20 @@
/**
* Unit tests for /lib/filestorage/file_storage.php
*
* @package core_files
* @category phpunit
* @package core
* @category test
* @copyright 2012 David Mudrak <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
use file_exception;
use file_reference_exception;
use repository;
use stored_file;
use stored_file_creation_exception;
defined('MOODLE_INTERNAL') || die();
global $CFG;
@@ -33,11 +41,13 @@ require_once($CFG->libdir . '/filestorage/stored_file.php');
/**
* Unit tests for /lib/filestorage/file_storage.php
*
* @package core
* @category test
* @copyright 2012 David Mudrak <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \file_storage
*/
class core_files_file_storage_testcase extends advanced_testcase {
class file_storage_test extends \advanced_testcase {
/**
* Files can be created from strings.
@@ -53,7 +63,7 @@ class core_files_file_storage_testcase extends advanced_testcase {
$installedfiles = $DB->count_records('files', array());
$content = 'abcd';
$syscontext = context_system::instance();
$syscontext = \context_system::instance();
$filerecord = array(
'contextid' => $syscontext->id,
'component' => 'core',
@@ -126,7 +136,7 @@ class core_files_file_storage_testcase extends advanced_testcase {
$installedfiles = $DB->count_records('files', array());
$filepath = $CFG->dirroot.'/lib/filestorage/tests/fixtures/testimage.jpg';
$syscontext = context_system::instance();
$syscontext = \context_system::instance();
$filerecord = array(
'contextid' => $syscontext->id,
'component' => 'core',
@@ -189,7 +199,7 @@ class core_files_file_storage_testcase extends advanced_testcase {
try {
$fs->create_file_from_pathname($filerecord, $filepath.'nonexistent');
$this->fail('Exception expected when trying to add non-existent stored file.');
} catch (Exception $e) {
} catch (\Exception $e) {
$this->assertInstanceOf('file_exception', $e);
}
}
@@ -205,7 +215,7 @@ class core_files_file_storage_testcase extends advanced_testcase {
$this->resetAfterTest(false);
$filepath = $CFG->dirroot.'/lib/filestorage/tests/fixtures/testimage.jpg';
$syscontext = context_system::instance();
$syscontext = \context_system::instance();
$filerecord = array(
'contextid' => $syscontext->id,
'component' => 'core',
@@ -264,7 +274,7 @@ class core_files_file_storage_testcase extends advanced_testcase {
*/
public function test_get_file_preview_nonimage() {
$this->resetAfterTest(true);
$syscontext = context_system::instance();
$syscontext = \context_system::instance();
$filerecord = array(
'contextid' => $syscontext->id,
'component' => 'core',
@@ -294,7 +304,7 @@ class core_files_file_storage_testcase extends advanced_testcase {
$this->resetAfterTest();
$fs = get_file_storage();
$syscontext = context_system::instance();
$syscontext = \context_system::instance();
$component = 'core';
$filearea = 'unittest';
$itemid = 0;
@@ -343,8 +353,8 @@ class core_files_file_storage_testcase extends advanced_testcase {
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$this->setUser($user);
$usercontext = context_user::instance($user->id);
$syscontext = context_system::instance();
$usercontext = \context_user::instance($user->id);
$syscontext = \context_system::instance();
$fs = get_file_storage();
@@ -429,8 +439,8 @@ class core_files_file_storage_testcase extends advanced_testcase {
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$this->setUser($user);
$usercontext = context_user::instance($user->id);
$syscontext = context_system::instance();
$usercontext = \context_user::instance($user->id);
$syscontext = \context_system::instance();
$fs = get_file_storage();
@@ -495,9 +505,9 @@ class core_files_file_storage_testcase extends advanced_testcase {
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$this->setUser($user->id);
$usercontext = context_user::instance($user->id);
$usercontext = \context_user::instance($user->id);
// Create a user private file.
$file1 = new stdClass;
$file1 = new \stdClass;
$file1->contextid = $usercontext->id;
$file1->component = 'user';
$file1->filearea = 'private';
@@ -782,7 +792,7 @@ class core_files_file_storage_testcase extends advanced_testcase {
*/
public function test_create_directory_component_invalid() {
$fs = get_file_storage();
$syscontext = context_system::instance();
$syscontext = \context_system::instance();
$this->expectException('file_exception');
$fs->create_directory($syscontext->id, 'bad/component', 'unittest', 0, '/');
@@ -795,7 +805,7 @@ class core_files_file_storage_testcase extends advanced_testcase {
*/
public function test_create_directory_filearea_invalid() {
$fs = get_file_storage();
$syscontext = context_system::instance();
$syscontext = \context_system::instance();
$this->expectException('file_exception');
$fs->create_directory($syscontext->id, 'core', 'bad-filearea', 0, '/');
@@ -808,7 +818,7 @@ class core_files_file_storage_testcase extends advanced_testcase {
*/
public function test_create_directory_itemid_negative() {
$fs = get_file_storage();
$syscontext = context_system::instance();
$syscontext = \context_system::instance();
$this->expectException('file_exception');
$fs->create_directory($syscontext->id, 'core', 'unittest', -1, '/');
@@ -821,7 +831,7 @@ class core_files_file_storage_testcase extends advanced_testcase {
*/
public function test_create_directory_itemid_invalid() {
$fs = get_file_storage();
$syscontext = context_system::instance();
$syscontext = \context_system::instance();
$this->expectException('file_exception');
$fs->create_directory($syscontext->id, 'core', 'unittest', 'notanint', '/');
@@ -834,7 +844,7 @@ class core_files_file_storage_testcase extends advanced_testcase {
*/
public function test_create_directory_filepath_invalid() {
$fs = get_file_storage();
$syscontext = context_system::instance();
$syscontext = \context_system::instance();
$this->expectException('file_exception');
$fs->create_directory($syscontext->id, 'core', 'unittest', 0, '/not-with-trailing/or-leading-slash');
@@ -931,7 +941,7 @@ class core_files_file_storage_testcase extends advanced_testcase {
'filename' => 'another-alias-to-1.txt'
);
$reference = file_storage::pack_reference(array(
$reference = \file_storage::pack_reference(array(
'contextid' => $user->ctxid,
'component' => 'user',
'filearea' => 'private',
@@ -1062,7 +1072,7 @@ class core_files_file_storage_testcase extends advanced_testcase {
public function test_create_file_from_url() {
$this->resetAfterTest(true);
$syscontext = context_system::instance();
$syscontext = \context_system::instance();
$filerecord = array(
'contextid' => $syscontext->id,
'component' => 'core',
@@ -1133,7 +1143,7 @@ class core_files_file_storage_testcase extends advanced_testcase {
// Create a course with a page resource.
$course = $this->getDataGenerator()->create_course();
$page1 = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
$page1context = context_module::instance($page1->cmid);
$page1context = \context_module::instance($page1->cmid);
// Add a file to the page.
$fs = get_file_storage();
@@ -1155,7 +1165,7 @@ class core_files_file_storage_testcase extends advanced_testcase {
// Create a new page.
$page2 = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
$page2context = context_module::instance($page2->cmid);
$page2context = \context_module::instance($page2->cmid);
// Newly created page area is empty.
$this->assertTrue($fs->is_area_empty($page2context->id, 'mod_page', 'content'));
@@ -1187,7 +1197,7 @@ class core_files_file_storage_testcase extends advanced_testcase {
$this->resetAfterTest(false);
$filepath = $CFG->dirroot.'/lib/filestorage/tests/fixtures/testimage.jpg';
$syscontext = context_system::instance();
$syscontext = \context_system::instance();
$filerecord = array(
'contextid' => $syscontext->id,
'component' => 'core',
@@ -1220,7 +1230,7 @@ class core_files_file_storage_testcase extends advanced_testcase {
$this->resetAfterTest(false);
$filepath = $CFG->dirroot.'/lib/filestorage/tests/fixtures/testimage.png';
$syscontext = context_system::instance();
$syscontext = \context_system::instance();
$filerecord = array(
'contextid' => $syscontext->id,
'component' => 'core',
@@ -1278,8 +1288,8 @@ class core_files_file_storage_testcase extends advanced_testcase {
}
private function generate_file_record() {
$syscontext = context_system::instance();
$filerecord = new stdClass();
$syscontext = \context_system::instance();
$filerecord = new \stdClass();
$filerecord->contextid = $syscontext->id;
$filerecord->component = 'core';
$filerecord->filearea = 'phpunit';
@@ -1490,9 +1500,9 @@ class core_files_file_storage_testcase extends advanced_testcase {
public function test_create_file_from_storedfile() {
$this->resetAfterTest(true);
$syscontext = context_system::instance();
$syscontext = \context_system::instance();
$filerecord = new stdClass();
$filerecord = new \stdClass();
$filerecord->contextid = $syscontext->id;
$filerecord->component = 'core';
$filerecord->filearea = 'phpunit';
@@ -1840,7 +1850,7 @@ class core_files_file_storage_testcase extends advanced_testcase {
}
/**
* Calling stored_file::delete_reference() on a non-reference file throws coding_exception
* Calling \stored_file::delete_reference() on a non-reference file throws coding_exception
*
* @covers \stored_file::delete_reference
*/
@@ -1867,7 +1877,7 @@ class core_files_file_storage_testcase extends advanced_testcase {
}
/**
* Calling stored_file::delete_reference() on a reference file does not affect other
* Calling \stored_file::delete_reference() on a reference file does not affect other
* symlinks to the same original
*
* @covers \stored_file::delete_reference
@@ -2047,14 +2057,14 @@ class core_files_file_storage_testcase extends advanced_testcase {
$fs = get_file_storage();
$this->setAdminUser();
$contextid = context_user::instance($USER->id)->id;
$contextid = \context_user::instance($USER->id)->id;
$component = 'user';
$filearea = 'private';
$itemid = 0;
$filepath = '/';
// Create some private files.
$file = new stdClass;
$file = new \stdClass;
$file->contextid = $contextid;
$file->component = 'user';
$file->filearea = 'private';
@@ -2110,7 +2120,7 @@ class core_files_file_storage_testcase extends advanced_testcase {
* @covers ::mimetype
*/
public function test_mimetype_not_found() {
$mimetype = file_storage::mimetype('/path/to/nonexistent/file');
$mimetype = \file_storage::mimetype('/path/to/nonexistent/file');
$this->assertEquals('document/unknown', $mimetype);
}
@@ -2142,7 +2152,7 @@ class core_files_file_storage_testcase extends advanced_testcase {
* @dataProvider filepath_mimetype_provider
*/
public function test_mimetype_known(string $filepath, string $expectedmimetype): void {
$mimetype = file_storage::mimetype($filepath);
$mimetype = \file_storage::mimetype($filepath);
$this->assertEquals($expectedmimetype, $mimetype);
}
@@ -2153,7 +2163,7 @@ class core_files_file_storage_testcase extends advanced_testcase {
* @covers ::mimetype_from_file
*/
public function test_mimetype_from_file_not_found() {
$mimetype = file_storage::mimetype_from_file('/path/to/nonexistent/file');
$mimetype = \file_storage::mimetype_from_file('/path/to/nonexistent/file');
$this->assertEquals('document/unknown', $mimetype);
}
@@ -2173,7 +2183,7 @@ class core_files_file_storage_testcase extends advanced_testcase {
* @dataProvider filepath_mimetype_provider
*/
public function test_mimetype_from_file_known(string $filepath, string $expectedmimetype): void {
$mimetype = file_storage::mimetype_from_file($filepath);
$mimetype = \file_storage::mimetype_from_file($filepath);
$this->assertEquals($expectedmimetype, $mimetype);
}
@@ -14,14 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for file_system_filedir.
*
* @package core_files
* @category phpunit
* @copyright 2017 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
use file_system_filedir;
defined('MOODLE_INTERNAL') || die();
@@ -32,13 +27,13 @@ require_once($CFG->libdir . '/filestorage/file_system_filedir.php');
/**
* Unit tests for file_system_filedir.
*
* @package core_files
* @category files
* @package core
* @category test
* @copyright 2017 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \file_system_filedir
*/
class core_files_file_system_filedir_testcase extends advanced_testcase {
class file_system_filedir_test extends \advanced_testcase {
/**
* Shared test setUp.
@@ -100,12 +95,12 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
* @return stored_file
*/
protected function get_stored_file($filecontent, $filename = null, $mockedmethods = []) {
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
if (empty($filename)) {
$filename = $contenthash;
}
$file = $this->getMockBuilder(stored_file::class)
$file = $this->getMockBuilder(\stored_file::class)
->onlyMethods($mockedmethods)
->setConstructorArgs([
get_file_storage(),
@@ -210,7 +205,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
*/
public function test_get_remote_path_from_hash() {
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$expectedresult = (object) [];
$fs = $this->get_testable_mock([
@@ -222,7 +217,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
->with($this->equalTo($contenthash), $this->equalTo(false))
->willReturn($expectedresult);
$method = new ReflectionMethod(file_system_filedir::class, 'get_remote_path_from_hash');
$method = new \ReflectionMethod(file_system_filedir::class, 'get_remote_path_from_hash');
$method->setAccessible(true);
$result = $method->invokeArgs($fs, [$contenthash]);
@@ -298,7 +293,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
global $CFG;
$fs = new file_system_filedir();
$method = new ReflectionMethod(file_system_filedir::class, 'get_fulldir_from_hash');
$method = new \ReflectionMethod(file_system_filedir::class, 'get_fulldir_from_hash');
$method->setAccessible(true);
$result = $method->invokeArgs($fs, array($hash));
@@ -330,7 +325,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
$file->method('get_contenthash')->willReturn($hash);
$fs = new file_system_filedir();
$method = new ReflectionMethod('file_system_filedir', 'get_fulldir_from_storedfile');
$method = new \ReflectionMethod('file_system_filedir', 'get_fulldir_from_storedfile');
$method->setAccessible(true);
$result = $method->invokeArgs($fs, array($file));
@@ -349,7 +344,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
* @covers ::get_contentdir_from_hash
*/
public function test_get_contentdir_from_hash($hash, $hashdir) {
$method = new ReflectionMethod(file_system_filedir::class, 'get_contentdir_from_hash');
$method = new \ReflectionMethod(file_system_filedir::class, 'get_contentdir_from_hash');
$method->setAccessible(true);
$fs = new file_system_filedir();
@@ -369,7 +364,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
* @covers ::get_contentpath_from_hash
*/
public function test_get_contentpath_from_hash($hash, $hashdir) {
$method = new ReflectionMethod(file_system_filedir::class, 'get_contentpath_from_hash');
$method = new \ReflectionMethod(file_system_filedir::class, 'get_contentpath_from_hash');
$method->setAccessible(true);
$fs = new file_system_filedir();
@@ -393,7 +388,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
global $CFG;
$fs = new file_system_filedir();
$method = new ReflectionMethod(file_system_filedir::class, 'get_trash_fullpath_from_hash');
$method = new \ReflectionMethod(file_system_filedir::class, 'get_trash_fullpath_from_hash');
$method->setAccessible(true);
$result = $method->invokeArgs($fs, array($hash));
@@ -415,7 +410,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
global $CFG;
$fs = new file_system_filedir();
$method = new ReflectionMethod(file_system_filedir::class, 'get_trash_fulldir_from_hash');
$method = new \ReflectionMethod(file_system_filedir::class, 'get_trash_fulldir_from_hash');
$method->setAccessible(true);
$result = $method->invokeArgs($fs, array($hash));
@@ -433,7 +428,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
global $CFG;
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$filedircontent = [
$contenthash => $filecontent,
];
@@ -446,7 +441,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
])
->getMock();
$file = $this->getMockBuilder(stored_file::class)
$file = $this->getMockBuilder(\stored_file::class)
->disableOriginalConstructor()
->getMock();
@@ -472,7 +467,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
// Setup the filedir.
// This contains a virtual file which has a cache mismatch.
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$trashdircontent = [
'0f' => [
@@ -484,13 +479,13 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
$vfileroot = $this->setup_vfile_root([], $trashdircontent);
$file = new stored_file(get_file_storage(), (object) [
$file = new \stored_file(get_file_storage(), (object) [
'contenthash' => $contenthash,
'filesize' => strlen($filecontent),
]);
$fs = new file_system_filedir();
$method = new ReflectionMethod(file_system_filedir::class, 'recover_file');
$method = new \ReflectionMethod(file_system_filedir::class, 'recover_file');
$method->setAccessible(true);
$result = $method->invokeArgs($fs, array($file));
@@ -512,7 +507,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
// Setup the filedir.
// This contains a virtual file which has a cache mismatch.
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$filedircontent = $trashdircontent = [
'0f' => [
@@ -524,13 +519,13 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
$vfileroot = $this->setup_vfile_root($filedircontent, $trashdircontent);
$file = new stored_file(get_file_storage(), (object) [
$file = new \stored_file(get_file_storage(), (object) [
'contenthash' => $contenthash,
'filesize' => strlen($filecontent),
]);
$fs = new file_system_filedir();
$method = new ReflectionMethod(file_system_filedir::class, 'recover_file');
$method = new \ReflectionMethod(file_system_filedir::class, 'recover_file');
$method->setAccessible(true);
$result = $method->invokeArgs($fs, array($file));
@@ -551,7 +546,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
// Setup the filedir.
// This contains a virtual file which has a cache mismatch.
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$trashdircontent = [
'0f' => [
@@ -562,13 +557,13 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
];
$vfileroot = $this->setup_vfile_root([], $trashdircontent);
$file = new stored_file(get_file_storage(), (object) [
$file = new \stored_file(get_file_storage(), (object) [
'contenthash' => $contenthash,
'filesize' => strlen($filecontent) + 1,
]);
$fs = new file_system_filedir();
$method = new ReflectionMethod(file_system_filedir::class, 'recover_file');
$method = new \ReflectionMethod(file_system_filedir::class, 'recover_file');
$method->setAccessible(true);
$result = $method->invokeArgs($fs, array($file));
@@ -588,7 +583,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
// Setup the filedir.
// This contains a virtual file which has a cache mismatch.
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$trashdircontent = [
'0f' => [
@@ -599,13 +594,13 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
];
$vfileroot = $this->setup_vfile_root([], $trashdircontent);
$file = new stored_file(get_file_storage(), (object) [
$file = new \stored_file(get_file_storage(), (object) [
'contenthash' => $contenthash . " different",
'filesize' => strlen($filecontent),
]);
$fs = new file_system_filedir();
$method = new ReflectionMethod(file_system_filedir::class, 'recover_file');
$method = new \ReflectionMethod(file_system_filedir::class, 'recover_file');
$method->setAccessible(true);
$result = $method->invokeArgs($fs, array($file));
@@ -626,20 +621,20 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
// Setup the filedir.
// This contains a virtual file which has a cache mismatch.
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$trashdircontent = [
$contenthash => $filecontent,
];
$vfileroot = $this->setup_vfile_root([], $trashdircontent);
$file = new stored_file(get_file_storage(), (object) [
$file = new \stored_file(get_file_storage(), (object) [
'contenthash' => $contenthash,
'filesize' => strlen($filecontent),
]);
$fs = new file_system_filedir();
$method = new ReflectionMethod(file_system_filedir::class, 'recover_file');
$method = new \ReflectionMethod(file_system_filedir::class, 'recover_file');
$method->setAccessible(true);
$result = $method->invokeArgs($fs, array($file));
@@ -659,7 +654,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
$this->resetAfterTest();
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$filedircontent = [
'0f' => [],
];
@@ -673,13 +668,13 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
->chmod(0444)
->chown(\org\bovigo\vfs\vfsStream::OWNER_USER_2);
$file = new stored_file(get_file_storage(), (object) [
$file = new \stored_file(get_file_storage(), (object) [
'contenthash' => $contenthash,
'filesize' => strlen($filecontent),
]);
$fs = new file_system_filedir();
$method = new ReflectionMethod(file_system_filedir::class, 'recover_file');
$method = new \ReflectionMethod(file_system_filedir::class, 'recover_file');
$method->setAccessible(true);
$result = $method->invokeArgs($fs, array($file));
@@ -699,7 +694,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
// Setup the filedir.
// This contains a virtual file which has a cache mismatch.
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$sourcedircontent = [
'file' => $filecontent,
];
@@ -716,7 +711,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
// Test the output.
$this->assertEquals($contenthash, $result[0]);
$this->assertEquals(core_text::strlen($filecontent), $result[1]);
$this->assertEquals(\core_text::strlen($filecontent), $result[1]);
$this->assertTrue($result[2]);
$this->assertEquals($filecontent, $vfileroot->getChild('filedir/0f/f3/' . $contenthash)->getContent());
@@ -752,7 +747,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
$this->resetAfterTest();
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$sourcedir = [
'file' => $filecontent,
];
@@ -774,7 +769,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
$this->resetAfterTest();
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$filedircontent = [
'0f' => [
'f3' => [
@@ -798,7 +793,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
// Test the output.
$this->assertEquals($contenthash, $result[0]);
$this->assertEquals(core_text::strlen($filecontent), $result[1]);
$this->assertEquals(\core_text::strlen($filecontent), $result[1]);
$this->assertFalse($result[2]);
// Fetch the new file structure.
@@ -819,7 +814,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
$this->resetAfterTest();
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$filedircontent = [
'0f' => [],
];
@@ -853,7 +848,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
global $CFG;
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$vfileroot = $this->setup_vfile_root();
// Note, the vfs file system does not support locks - prevent file locking here.
@@ -865,7 +860,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
// Test the output.
$this->assertEquals($contenthash, $result[0]);
$this->assertEquals(core_text::strlen($filecontent), $result[1]);
$this->assertEquals(\core_text::strlen($filecontent), $result[1]);
$this->assertTrue($result[2]);
}
@@ -879,7 +874,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
$this->resetAfterTest();
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$filedircontent = [
'0f' => [],
@@ -911,7 +906,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
global $CFG;
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$filedircontent = [
'0f' => [
'f3' => [
@@ -931,7 +926,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
// Test the output.
$this->assertEquals($contenthash, $result[0]);
$this->assertEquals(core_text::strlen($filecontent), $result[1]);
$this->assertEquals(\core_text::strlen($filecontent), $result[1]);
$this->assertFalse($result[2]);
}
@@ -944,7 +939,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
$this->resetAfterTest();
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$vfileroot = $this->setup_vfile_root();
$fs = new file_system_filedir();
@@ -967,7 +962,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
$this->resetAfterTest();
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$filedircontent = $trashdircontent = [
'0f' => [
@@ -1005,7 +1000,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
$fs = new file_system_filedir();
$result = $fs->remove_file(file_storage::hash_from_string(''));
$result = $fs->remove_file(\file_storage::hash_from_string(''));
$this->assertNull($result);
}
@@ -1020,7 +1015,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
global $DB;
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$filedircontent = [
'0f' => [
'f3' => [
@@ -1053,7 +1048,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
global $DB;
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$filedircontent = [
'0f' => [
'f3' => [
@@ -1084,7 +1079,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
$this->resetAfterTest();
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$filedircontent = $trashdircontent = [
'0f' => [
@@ -1096,7 +1091,7 @@ class core_files_file_system_filedir_testcase extends advanced_testcase {
$vfileroot = $this->setup_vfile_root($filedircontent, $trashdircontent);
$fs = new file_system_filedir();
$method = new ReflectionMethod(file_system_filedir::class, 'empty_trash');
$method = new \ReflectionMethod(file_system_filedir::class, 'empty_trash');
$method->setAccessible(true);
$result = $method->invoke($fs);
+35 -37
View File
@@ -14,14 +14,12 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for file_system.
*
* @package core_files
* @category phpunit
* @copyright 2017 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
use file_archive;
use file_packer;
use file_system;
use file_system_filedir;
defined('MOODLE_INTERNAL') || die();
@@ -31,13 +29,13 @@ require_once($CFG->libdir . '/filestorage/file_system.php');
/**
* Unit tests for file_system.
*
* @package core_files
* @category phpunit
* @package core
* @category test
* @copyright 2017 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \file_system
*/
class core_files_file_system_testcase extends advanced_testcase {
class file_system_test extends \advanced_testcase {
public function setUp(): void {
get_file_storage(true);
@@ -71,12 +69,12 @@ class core_files_file_system_testcase extends advanced_testcase {
* @return stored_file
*/
protected function get_stored_file($filecontent, $filename = null, $mockedmethods = []) {
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
if (empty($filename)) {
$filename = $contenthash;
}
$file = $this->getMockBuilder(stored_file::class)
$file = $this->getMockBuilder(\stored_file::class)
->onlyMethods($mockedmethods)
->setConstructorArgs([
get_file_storage(),
@@ -111,7 +109,7 @@ class core_files_file_system_testcase extends advanced_testcase {
*
*/
public function test_not_cloneable() {
$reflection = new ReflectionClass('file_system');
$reflection = new \ReflectionClass('file_system');
$this->assertFalse($reflection->isCloneable());
}
@@ -239,7 +237,7 @@ class core_files_file_system_testcase extends advanced_testcase {
]);
$fs->expects($this->once())
->method('get_local_path_from_hash')
->with($this->equalTo(file_storage::hash_from_string($filecontent)), $this->equalTo($fetch))
->with($this->equalTo(\file_storage::hash_from_string($filecontent)), $this->equalTo($fetch))
->willReturn($filepath);
$file = $this->get_stored_file($filecontent);
@@ -266,7 +264,7 @@ class core_files_file_system_testcase extends advanced_testcase {
$fs->expects($this->once())
->method('get_remote_path_from_hash')
->with($this->equalTo(file_storage::hash_from_string($filecontent)), $this->equalTo(false))
->with($this->equalTo(\file_storage::hash_from_string($filecontent)), $this->equalTo(false))
->willReturn($filepath);
$file = $this->get_stored_file($filecontent);
@@ -288,7 +286,7 @@ class core_files_file_system_testcase extends advanced_testcase {
*/
public function test_is_file_readable_locally_by_hash() {
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$filepath = __FILE__;
$fs = $this->get_testable_mock([
@@ -309,7 +307,7 @@ class core_files_file_system_testcase extends advanced_testcase {
*/
public function test_is_file_readable_locally_by_hash_empty() {
$filecontent = '';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$fs = $this->get_testable_mock([
'get_local_path_from_hash',
@@ -328,7 +326,7 @@ class core_files_file_system_testcase extends advanced_testcase {
*/
public function test_is_file_readable_remotely_by_hash() {
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$fs = $this->get_testable_mock([
'get_remote_path_from_hash',
@@ -348,7 +346,7 @@ class core_files_file_system_testcase extends advanced_testcase {
*/
public function test_is_file_readable_remotely_by_hash_empty() {
$filecontent = '';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$fs = $this->get_testable_mock([
'get_remote_path_from_hash',
@@ -367,7 +365,7 @@ class core_files_file_system_testcase extends advanced_testcase {
*/
public function test_is_file_readable_remotely_by_hash_not_found() {
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$fs = $this->get_testable_mock([
'get_remote_path_from_hash',
@@ -493,9 +491,9 @@ class core_files_file_system_testcase extends advanced_testcase {
*/
public function test_is_file_removable_empty() {
$filecontent = '';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$method = new ReflectionMethod(file_system::class, 'is_file_removable');
$method = new \ReflectionMethod(file_system::class, 'is_file_removable');
$method->setAccessible(true);
$result = $method->invokeArgs(null, [$contenthash]);
$this->assertFalse($result);
@@ -511,14 +509,14 @@ class core_files_file_system_testcase extends advanced_testcase {
global $DB;
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$DB = $this->getMockBuilder(\moodle_database::class)
->onlyMethods(['record_exists'])
->getMockForAbstractClass();
$DB->method('record_exists')->willReturn(true);
$method = new ReflectionMethod(file_system::class, 'is_file_removable');
$method = new \ReflectionMethod(file_system::class, 'is_file_removable');
$method->setAccessible(true);
$result = $method->invokeArgs(null, [$contenthash]);
@@ -535,14 +533,14 @@ class core_files_file_system_testcase extends advanced_testcase {
global $DB;
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$DB = $this->getMockBuilder(\moodle_database::class)
->onlyMethods(['record_exists'])
->getMockForAbstractClass();
$DB->method('record_exists')->willReturn(false);
$method = new ReflectionMethod(file_system::class, 'is_file_removable');
$method = new \ReflectionMethod(file_system::class, 'is_file_removable');
$method->setAccessible(true);
$result = $method->invokeArgs(null, [$contenthash]);
@@ -911,7 +909,7 @@ class core_files_file_system_testcase extends advanced_testcase {
// Get the filesystem mock.
$fs = $this->get_testable_mock();
$method = new ReflectionMethod(file_system::class, 'get_imageinfo_from_path');
$method = new \ReflectionMethod(file_system::class, 'get_imageinfo_from_path');
$method->setAccessible(true);
$result = $method->invokeArgs($fs, [$filepath]);
@@ -933,7 +931,7 @@ class core_files_file_system_testcase extends advanced_testcase {
// Get the filesystem mock.
$fs = $this->get_testable_mock();
$method = new ReflectionMethod(file_system::class, 'get_imageinfo_from_path');
$method = new \ReflectionMethod(file_system::class, 'get_imageinfo_from_path');
$method->setAccessible(true);
$result = $method->invokeArgs($fs, [$filepath]);
@@ -950,7 +948,7 @@ class core_files_file_system_testcase extends advanced_testcase {
// Get the filesystem mock.
$fs = $this->get_testable_mock();
$method = new ReflectionMethod(file_system::class, 'get_imageinfo_from_path');
$method = new \ReflectionMethod(file_system::class, 'get_imageinfo_from_path');
$method->setAccessible(true);
$result = $method->invokeArgs($fs, [$filepath]);
@@ -972,7 +970,7 @@ class core_files_file_system_testcase extends advanced_testcase {
// Get the filesystem mock.
$fs = $this->get_testable_mock();
$method = new ReflectionMethod(file_system::class, 'get_imageinfo_from_path');
$method = new \ReflectionMethod(file_system::class, 'get_imageinfo_from_path');
$method->setAccessible(true);
$result = $method->invokeArgs($fs, [$filepath]);
@@ -994,7 +992,7 @@ class core_files_file_system_testcase extends advanced_testcase {
// Get the filesystem mock.
$fs = $this->get_testable_mock();
$method = new ReflectionMethod(file_system::class, 'get_imageinfo_from_path');
$method = new \ReflectionMethod(file_system::class, 'get_imageinfo_from_path');
$method->setAccessible(true);
$result = $method->invokeArgs($fs, [$filepath]);
@@ -1016,7 +1014,7 @@ class core_files_file_system_testcase extends advanced_testcase {
// Get the filesystem mock.
$fs = $this->get_testable_mock();
$method = new ReflectionMethod(file_system::class, 'get_imageinfo_from_path');
$method = new \ReflectionMethod(file_system::class, 'get_imageinfo_from_path');
$method->setAccessible(true);
$result = $method->invokeArgs($fs, [$filepath]);
@@ -1057,7 +1055,7 @@ class core_files_file_system_testcase extends advanced_testcase {
->willReturn(__DIR__ . "/fixtures/test.tgz");
// Note: We are unable to determine the mode in which the $fh was opened.
$fh = $fs->get_content_file_handle($file, stored_file::FILE_HANDLE_GZOPEN);
$fh = $fs->get_content_file_handle($file, \stored_file::FILE_HANDLE_GZOPEN);
$this->assertTrue(is_resource($fh));
gzclose($fh);
}
@@ -1089,7 +1087,7 @@ class core_files_file_system_testcase extends advanced_testcase {
$filepath = '/path/to/file/not/currently/on/disk';
$filecontent = 'example content';
$filename = 'test.jpg';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$fs = $this->get_testable_mock(['get_remote_path_from_hash']);
$fs->method('get_remote_path_from_hash')->willReturn($filepath);
@@ -1106,7 +1104,7 @@ class core_files_file_system_testcase extends advanced_testcase {
*/
public function test_mimetype_from_hash_using_file_content() {
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$filename = 'example';
$filepath = __DIR__ . "/fixtures/testimage.jpg";
@@ -1126,7 +1124,7 @@ class core_files_file_system_testcase extends advanced_testcase {
public function test_mimetype_from_hash_using_file_content_remote() {
$filepath = '/path/to/file/not/currently/on/disk';
$filecontent = 'example content';
$contenthash = file_storage::hash_from_string($filecontent);
$contenthash = \file_storage::hash_from_string($filecontent);
$filename = 'example';
$filepath = __DIR__ . "/fixtures/testimage.jpg";
+11 -10
View File
@@ -14,20 +14,21 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for /lib/filestorage/mbz_packer.php.
*
* @package core_files
* @copyright 2013 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/filestorage/file_progress.php');
class core_files_mbz_packer_testcase extends advanced_testcase {
/**
* Unit tests for /lib/filestorage/mbz_packer.php.
*
* @package core
* @copyright 2013 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mbz_packer_test extends \advanced_testcase {
public function test_archive_with_both_options() {
global $CFG;
@@ -44,7 +45,7 @@ class core_files_mbz_packer_testcase extends advanced_testcase {
$CFG->usezipbackups = true;
$filefalse = $CFG->tempdir . '/false.mbz';
$this->assertNotEmpty($packer->archive_to_pathname($files, $filefalse));
$context = context_system::instance();
$context = \context_system::instance();
$this->assertNotEmpty($storagefalse = $packer->archive_to_storage(
$files, $context->id, 'phpunit', 'data', 0, '/', 'false.mbz'));
@@ -52,7 +53,7 @@ class core_files_mbz_packer_testcase extends advanced_testcase {
$CFG->usezipbackups = false;
$filetrue = $CFG->tempdir . '/true.mbz';
$this->assertNotEmpty($packer->archive_to_pathname($files, $filetrue));
$context = context_system::instance();
$context = \context_system::instance();
$this->assertNotEmpty($storagetrue = $packer->archive_to_storage(
$files, $context->id, 'phpunit', 'data', 0, '/', 'true.mbz'));
+18 -14
View File
@@ -14,20 +14,24 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for /lib/filestorage/tgz_packer.php and tgz_extractor.php.
*
* @package core_files
* @copyright 2013 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
use file_progress;
use tgz_packer;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/filestorage/file_progress.php');
class core_files_tgz_packer_testcase extends advanced_testcase implements file_progress {
/**
* Unit tests for /lib/filestorage/tgz_packer.php and tgz_extractor.php.
*
* @package core
* @copyright 2013 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tgz_packer_test extends \advanced_testcase implements file_progress {
/**
* @var array Progress information passed to the progress reporter
*/
@@ -68,7 +72,7 @@ class core_files_tgz_packer_testcase extends advanced_testcase implements file_p
$filelist['out2'] = $CFG->tempdir . '/dir1';
// Moodle stored_file.
$context = context_system::instance();
$context = \context_system::instance();
$filerecord = array('contextid' => $context->id, 'component' => 'phpunit',
'filearea' => 'data', 'itemid' => 0, 'filepath' => '/',
'filename' => 'file4.txt', 'timemodified' => 1377993604);
@@ -159,7 +163,7 @@ class core_files_tgz_packer_testcase extends advanced_testcase implements file_p
// Archive files.
$files = $this->prepare_file_list();
$archivefile = $CFG->tempdir . '/test.tar.gz';
$context = context_system::instance();
$context = \context_system::instance();
$sf = $packer->archive_to_storage($files,
$context->id, 'phpunit', 'archive', 1, '/', 'archive.tar.gz');
$this->assertInstanceOf('stored_file', $sf);
@@ -293,7 +297,7 @@ class core_files_tgz_packer_testcase extends advanced_testcase implements file_p
$filelist = $this->prepare_file_list();
$packer = get_file_packer('application/x-gzip');
$archive = "$CFG->tempdir/archive.tgz";
$context = context_system::instance();
$context = \context_system::instance();
// Archive to pathname.
$this->progress = array();
@@ -355,7 +359,7 @@ class core_files_tgz_packer_testcase extends advanced_testcase implements file_p
$packer = get_file_packer('application/x-gzip');
$result = $packer->archive_to_pathname($filelist, $archive, true, $this);
$this->assertTrue($result);
$hashwith = file_storage::hash_from_path($archive);
$hashwith = \file_storage::hash_from_path($archive);
// List files.
$files = $packer->list_files($archive);
@@ -382,7 +386,7 @@ class core_files_tgz_packer_testcase extends advanced_testcase implements file_p
$packer->set_include_index(false);
$result = $packer->archive_to_pathname($filelist, $archive, true, $this);
$this->assertTrue($result);
$hashwithout = file_storage::hash_from_path($archive);
$hashwithout = \file_storage::hash_from_path($archive);
$files = $packer->list_files($archive);
$this->assertEquals($expectedinfo, self::convert_info_for_assert($files));
@@ -420,7 +424,7 @@ class core_files_tgz_packer_testcase extends advanced_testcase implements file_p
$archive2 = "$CFG->tempdir/archive.zip";
// Archive in tgz and zip format.
$context = context_system::instance();
$context = \context_system::instance();
$archive1 = $packer1->archive_to_storage($filelist, $context->id,
'phpunit', 'test', 0, '/', 'archive.tgz', null, true, $this);
$this->assertInstanceOf('stored_file', $archive1);
+22 -17
View File
@@ -14,21 +14,26 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for /lib/filestorage/zip_packer.php and zip_archive.php
*
* @package core_files
* @category phpunit
* @copyright 2012 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
use file_archive;
use file_progress;
use zip_archive;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/filestorage/file_progress.php');
class core_files_zip_packer_testcase extends advanced_testcase implements file_progress {
/**
* Unit tests for /lib/filestorage/zip_packer.php and zip_archive.php
*
* @package core
* @category test
* @copyright 2012 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class zip_packer_test extends \advanced_testcase implements file_progress {
protected $testfile;
protected $files;
@@ -43,7 +48,7 @@ class core_files_zip_packer_testcase extends advanced_testcase implements file_p
$this->testfile = __DIR__.'/fixtures/test.txt';
$fs = get_file_storage();
$context = context_system::instance();
$context = \context_system::instance();
if (!$file = $fs->get_file($context->id, 'phpunit', 'data', 0, '/', 'test.txt')) {
$file = $fs->create_file_from_pathname(
array('contextid'=>$context->id, 'component'=>'phpunit', 'filearea'=>'data', 'itemid'=>0, 'filepath'=>'/', 'filename'=>'test.txt'),
@@ -198,7 +203,7 @@ class core_files_zip_packer_testcase extends advanced_testcase implements file_p
$packer = get_file_packer('application/zip');
$fs = get_file_storage();
$context = context_system::instance();
$context = \context_system::instance();
$this->assertFalse($fs->file_exists($context->id, 'phpunit', 'test', 0, '/', 'archive.zip'));
$result = $packer->archive_to_storage($this->files, $context->id, 'phpunit', 'test', 0, '/', 'archive.zip');
@@ -223,7 +228,7 @@ class core_files_zip_packer_testcase extends advanced_testcase implements file_p
$packer = get_file_packer('application/zip');
$fs = get_file_storage();
$context = context_system::instance();
$context = \context_system::instance();
$target = "$CFG->tempdir/test/";
$testcontent = file_get_contents($this->testfile);
@@ -337,7 +342,7 @@ class core_files_zip_packer_testcase extends advanced_testcase implements file_p
$packer = get_file_packer('application/zip');
$fs = get_file_storage();
$context = context_system::instance();
$context = \context_system::instance();
$target = "$CFG->tempdir/onlyfiles/";
$testcontent = file_get_contents($this->testfile);
@@ -412,7 +417,7 @@ class core_files_zip_packer_testcase extends advanced_testcase implements file_p
$packer = get_file_packer('application/zip');
$fs = get_file_storage();
$context = context_system::instance();
$context = \context_system::instance();
$testcontent = file_get_contents($this->testfile);
@@ -534,7 +539,7 @@ class core_files_zip_packer_testcase extends advanced_testcase implements file_p
try {
// Old PHP versions were not printing any warning.
$result = $zip_archive->close();
} catch (Exception $e) {
} catch (\Exception $e) {
// New PHP versions print PHP Warning.
$this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
$this->assertStringContainsString('ZipArchive::close', $e->getMessage());
@@ -543,7 +548,7 @@ class core_files_zip_packer_testcase extends advanced_testcase implements file_p
try {
// And some PHP versions do return correctly false (5.4.25, 5.6.14...)
$this->assertFalse($result);
} catch (Exception $e) {
} catch (\Exception $e) {
// But others do insist into returning true (5.6.13...). Only can accept them.
$this->assertInstanceOf('PHPUnit\Framework\ExpectationFailedException', $e);
$this->assertTrue($result);
@@ -626,7 +631,7 @@ class core_files_zip_packer_testcase extends advanced_testcase implements file_p
$this->resetAfterTest(true);
$packer = get_file_packer('application/zip');
$archive = "$CFG->tempdir/archive.zip";
$context = context_system::instance();
$context = \context_system::instance();
// Archive to pathname.
$this->progress = array();
+4 -10
View File
@@ -14,15 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for autocomplete forms element.
*
* This file contains all unit test related to autocomplete forms element.
*
* @package core_form
* @copyright 2015 Damyon Wiese <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_form;
use MoodleQuickForm_autocomplete;
defined('MOODLE_INTERNAL') || die();
@@ -38,7 +32,7 @@ require_once($CFG->libdir . '/form/autocomplete.php');
* @copyright 2015 Damyon Wiese <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_form_autocomplete_testcase extends basic_testcase {
class autocomplete_test extends \basic_testcase {
/**
* Testcase for validation
*/
+10 -5
View File
@@ -15,16 +15,21 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for dateselector form element
* Unit tests for MoodleQuickForm_date_selector
*
* This file contains unit test related to dateselector form element
* Contains test cases for testing MoodleQuickForm_date_selector
*
* @package core_form
* @category phpunit
* @category test
* @copyright 2012 Rajesh Taneja
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_form;
use moodleform;
use MoodleQuickForm_date_selector;
defined('MOODLE_INTERNAL') || die();
global $CFG;
@@ -37,11 +42,11 @@ require_once($CFG->libdir.'/formslib.php');
* Contains test cases for testing MoodleQuickForm_date_selector
*
* @package core_form
* @category phpunit
* @category test
* @copyright 2012 Rajesh Taneja
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_form_dateselector_testcase extends advanced_testcase {
class dateselector_test extends \advanced_testcase {
/** @var MoodleQuickForm Keeps reference of dummy form object */
private $mform;
/** @var array test fixtures */
+10 -5
View File
@@ -15,16 +15,21 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for datetimeselector form element
* Unit tests for MoodleQuickForm_date_time_selector
*
* This file contains unit test related to datetimeselector form element
* Contains test cases for testing MoodleQuickForm_date_time_selector
*
* @package core_form
* @category phpunit
* @category test
* @copyright 2012 Rajesh Taneja
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_form;
use moodleform;
use MoodleQuickForm_date_time_selector;
defined('MOODLE_INTERNAL') || die();
global $CFG;
@@ -37,11 +42,11 @@ require_once($CFG->libdir.'/formslib.php');
* Contains test cases for testing MoodleQuickForm_date_time_selector
*
* @package core_form
* @category phpunit
* @category test
* @copyright 2012 Rajesh Taneja
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_form_datetimeselector_testcase extends advanced_testcase {
class datetimeselector_test extends \advanced_testcase {
/** @var MoodleQuickForm Keeps reference of dummy form object */
private $mform;
/** @var array test fixtures */
+11 -6
View File
@@ -15,16 +15,21 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for forms lib.
* Unit tests for MoodleQuickForm_duration
*
* This file contains all unit test related to forms library.
* Contains test cases for testing MoodleQuickForm_duration
*
* @package core_form
* @category phpunit
* @category test
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_form;
use moodleform;
use MoodleQuickForm;
defined('MOODLE_INTERNAL') || die();
global $CFG;
@@ -36,11 +41,11 @@ require_once($CFG->libdir . '/form/duration.php');
* Contains test cases for testing MoodleQuickForm_duration
*
* @package core_form
* @category phpunit
* @category test
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_form_duration_testcase extends basic_testcase {
class duration_test extends \basic_testcase {
/**
* Get a form that can be used for testing.
@@ -69,7 +74,7 @@ class core_form_duration_testcase extends basic_testcase {
public function test_constructor_rejects_invalid_unit(): void {
// Test trying to create with an invalid unit.
$mform = $this->get_test_form();
$this->expectException(coding_exception::class);
$this->expectException(\coding_exception::class);
$mform->addElement('duration', 'testel', null, ['defaultunit' => 123, 'optional' => false]);
}
+5 -9
View File
@@ -14,14 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for float form element.
*
* @package core_form
* @category test
* @copyright 2019 Shamim Rezaie <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_form;
use MoodleQuickForm_float;
defined('MOODLE_INTERNAL') || die();
@@ -34,10 +29,11 @@ require_once($CFG->libdir . '/form/float.php');
* Contains test cases for testing MoodleQuickForm_float
*
* @package core_form
* @category test
* @copyright 2019 Shamim Rezaie <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_form_float_testcase extends advanced_testcase {
class float_test extends \advanced_testcase {
/**
* Define a local decimal separator.
+98 -95
View File
@@ -14,19 +14,22 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package core_grades
* @category phpunit
* @copyright [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/fixtures/lib.php');
class core_grade_category_testcase extends grade_base_testcase {
/**
* Test grade categories
*
* @package core
* @category test
* @copyright [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class grade_category_test extends \grade_base_testcase {
public function test_grade_category() {
$this->sub_test_grade_category_construct();
@@ -77,14 +80,14 @@ class core_grade_category_testcase extends grade_base_testcase {
// Adds 3 new grade categories at various depths.
protected function sub_test_grade_category_construct() {
$course_category = grade_category::fetch_course_category($this->courseid);
$course_category = \grade_category::fetch_course_category($this->courseid);
$params = new stdClass();
$params = new \stdClass();
$params->courseid = $this->courseid;
$params->fullname = 'unittestcategory4';
$grade_category = new grade_category($params, false);
$grade_category = new \grade_category($params, false);
$grade_category->insert();
$this->grade_categories[] = $grade_category;
@@ -97,7 +100,7 @@ class core_grade_category_testcase extends grade_base_testcase {
// Test a child category.
$params->parent = $grade_category->id;
$params->fullname = 'unittestcategory5';
$grade_category = new grade_category($params, false);
$grade_category = new \grade_category($params, false);
$grade_category->insert();
$this->grade_categories[] = $grade_category;
@@ -108,7 +111,7 @@ class core_grade_category_testcase extends grade_base_testcase {
// Test a third depth category.
$params->parent = $grade_category->id;
$params->fullname = 'unittestcategory6';
$grade_category = new grade_category($params, false);
$grade_category = new \grade_category($params, false);
$grade_category->insert();
$this->grade_categories[50] = $grade_category;// Going to delete this one later hence the special index.
@@ -117,32 +120,32 @@ class core_grade_category_testcase extends grade_base_testcase {
}
protected function sub_test_grade_category_build_path() {
$grade_category = new grade_category($this->grade_categories[1]);
$grade_category = new \grade_category($this->grade_categories[1]);
$this->assertTrue(method_exists($grade_category, 'build_path'));
$path = grade_category::build_path($grade_category);
$path = \grade_category::build_path($grade_category);
$this->assertEquals($grade_category->path, $path);
}
protected function sub_test_grade_category_fetch() {
$grade_category = new grade_category();
$grade_category = new \grade_category();
$this->assertTrue(method_exists($grade_category, 'fetch'));
$grade_category = grade_category::fetch(array('id'=>$this->grade_categories[0]->id));
$grade_category = \grade_category::fetch(array('id'=>$this->grade_categories[0]->id));
$this->assertEquals($this->grade_categories[0]->id, $grade_category->id);
$this->assertEquals($this->grade_categories[0]->fullname, $grade_category->fullname);
}
protected function sub_test_grade_category_fetch_all() {
$grade_category = new grade_category();
$grade_category = new \grade_category();
$this->assertTrue(method_exists($grade_category, 'fetch_all'));
$grade_categories = grade_category::fetch_all(array('courseid'=>$this->courseid));
$grade_categories = \grade_category::fetch_all(array('courseid'=>$this->courseid));
$this->assertEquals(count($this->grade_categories), count($grade_categories)-1);
}
protected function sub_test_grade_category_update() {
global $DB;
$grade_category = new grade_category($this->grade_categories[0]);
$grade_category = new \grade_category($this->grade_categories[0]);
$this->assertTrue(method_exists($grade_category, 'update'));
$grade_category->fullname = 'Updated info for this unittest grade_category';
@@ -171,7 +174,7 @@ class core_grade_category_testcase extends grade_base_testcase {
protected function sub_test_grade_category_delete() {
global $DB;
$grade_category = new grade_category($this->grade_categories[50]);
$grade_category = new \grade_category($this->grade_categories[50]);
$this->assertTrue(method_exists($grade_category, 'delete'));
$this->assertTrue($grade_category->delete());
@@ -179,9 +182,9 @@ class core_grade_category_testcase extends grade_base_testcase {
}
protected function sub_test_grade_category_insert() {
$course_category = grade_category::fetch_course_category($this->courseid);
$course_category = \grade_category::fetch_course_category($this->courseid);
$grade_category = new grade_category();
$grade_category = new \grade_category();
$this->assertTrue(method_exists($grade_category, 'insert'));
$grade_category->fullname = 'unittestcategory4';
@@ -210,24 +213,24 @@ class core_grade_category_testcase extends grade_base_testcase {
}
protected function sub_test_grade_category_qualifies_for_regrading() {
$grade_category = new grade_category($this->grade_categories[1]);
$grade_category = new \grade_category($this->grade_categories[1]);
$this->assertTrue(method_exists($grade_category, 'qualifies_for_regrading'));
$this->assertFalse($grade_category->qualifies_for_regrading());
$grade_category->aggregation = GRADE_AGGREGATE_MAX;
$this->assertTrue($grade_category->qualifies_for_regrading());
$grade_category = new grade_category($this->grade_categories[1]);
$grade_category = new \grade_category($this->grade_categories[1]);
$grade_category->droplow = 99;
$this->assertTrue($grade_category->qualifies_for_regrading());
$grade_category = new grade_category($this->grade_categories[1]);
$grade_category = new \grade_category($this->grade_categories[1]);
$grade_category->keephigh = 99;
$this->assertTrue($grade_category->qualifies_for_regrading());
}
protected function sub_test_grade_category_force_regrading() {
$grade_category = new grade_category($this->grade_categories[1]);
$grade_category = new \grade_category($this->grade_categories[1]);
$this->assertTrue(method_exists($grade_category, 'force_regrading'));
$grade_category->load_grade_item();
@@ -253,13 +256,13 @@ class core_grade_category_testcase extends grade_base_testcase {
// Start of regression test for MDL-51715.
// grade_categories [1] and [2] are child categories of [0]
// Ensure that grades have been generated with fixture data.
$childcat1 = new grade_category($this->grade_categories[1]);
$childcat1 = new \grade_category($this->grade_categories[1]);
$childcat1itemid = $childcat1->load_grade_item()->id;
$childcat1->generate_grades();
$childcat2 = new grade_category($this->grade_categories[2]);
$childcat2 = new \grade_category($this->grade_categories[2]);
$childcat2itemid = $childcat2->load_grade_item()->id;
$childcat2->generate_grades();
$parentcat = new grade_category($this->grade_categories[0]);
$parentcat = new \grade_category($this->grade_categories[0]);
$parentcat->generate_grades();
// Drop low and and re-generate to produce 'dropped' aggregation status.
@@ -311,12 +314,12 @@ class core_grade_category_testcase extends grade_base_testcase {
global $DB;
// Inserting some special grade items to make testing the final grade calculation easier.
$params = new stdClass();
$params = new \stdClass();
$params->courseid = $this->courseid;
$params->fullname = 'unittestgradecalccategory';
$params->aggregation = GRADE_AGGREGATE_MEAN;
$params->aggregateonlygraded = 0;
$grade_category = new grade_category($params, false);
$grade_category = new \grade_category($params, false);
$grade_category->insert();
$this->assertTrue(method_exists($grade_category, 'generate_grades'));
@@ -330,7 +333,7 @@ class core_grade_category_testcase extends grade_base_testcase {
// 3 grade items each with a maximum grade of 10.
$grade_items = array();
for ($i=0; $i<3; $i++) {
$grade_items[$i] = new grade_item();
$grade_items[$i] = new \grade_item();
$grade_items[$i]->courseid = $this->courseid;
$grade_items[$i]->categoryid = $grade_category->id;
$grade_items[$i]->itemname = 'manual grade_item '.$i;
@@ -354,7 +357,7 @@ class core_grade_category_testcase extends grade_base_testcase {
// A grade for each grade item.
$grade_grades = array();
for ($i=0; $i<3; $i++) {
$grade_grades[$i] = new grade_grade();
$grade_grades[$i] = new \grade_grade();
$grade_grades[$i]->itemid = $grade_items[$i]->id;
$grade_grades[$i]->userid = $this->userid;
$grade_grades[$i]->rawgrade = ($i+1)*2; // Produce grade grades of 2, 4 and 6.
@@ -462,19 +465,19 @@ class core_grade_category_testcase extends grade_base_testcase {
}
protected function sub_test_grade_category_aggregate_grades() {
$category = new grade_category($this->grade_categories[0]);
$category = new \grade_category($this->grade_categories[0]);
$this->assertTrue(method_exists($category, 'aggregate_grades'));
// Tested more fully via test_grade_category_generate_grades().
}
protected function sub_test_grade_category_apply_limit_rules() {
$items[$this->grade_items[0]->id] = new grade_item($this->grade_items[0], false);
$items[$this->grade_items[1]->id] = new grade_item($this->grade_items[1], false);
$items[$this->grade_items[2]->id] = new grade_item($this->grade_items[2], false);
$items[$this->grade_items[4]->id] = new grade_item($this->grade_items[4], false);
$items[$this->grade_items[0]->id] = new \grade_item($this->grade_items[0], false);
$items[$this->grade_items[1]->id] = new \grade_item($this->grade_items[1], false);
$items[$this->grade_items[2]->id] = new \grade_item($this->grade_items[2], false);
$items[$this->grade_items[4]->id] = new \grade_item($this->grade_items[4], false);
// Test excluding the lowest 2 out of 4 grades from aggregation with no 0 grades.
$category = new grade_category();
$category = new \grade_category();
$category->droplow = 2;
$grades = array($this->grade_items[0]->id=>5.374,
$this->grade_items[1]->id=>9.4743,
@@ -486,7 +489,7 @@ class core_grade_category_testcase extends grade_base_testcase {
$this->assertEquals($grades[$this->grade_items[4]->id], 7.3754);
// Test aggregating only the highest 1 out of 4 grades.
$category = new grade_category();
$category = new \grade_category();
$category->keephigh = 1;
$category->droplow = 0;
$grades = array($this->grade_items[0]->id=>5.374,
@@ -500,7 +503,7 @@ class core_grade_category_testcase extends grade_base_testcase {
// Test excluding the lowest 2 out of 4 grades from aggregation with no 0 grades.
// An extra credit grade item should be kept even if droplow means it would otherwise be excluded.
$category = new grade_category();
$category = new \grade_category();
$category->droplow = 2;
$category->aggregation = GRADE_AGGREGATE_SUM;
$items[$this->grade_items[2]->id]->aggregationcoef = 1; // Mark grade item 2 as "extra credit".
@@ -515,7 +518,7 @@ class core_grade_category_testcase extends grade_base_testcase {
// Test only aggregating the highest 1 out of 4 grades.
// An extra credit grade item is retained in addition to the highest grade.
$category = new grade_category();
$category = new \grade_category();
$category->keephigh = 1;
$category->droplow = 0;
$category->aggregation = GRADE_AGGREGATE_SUM;
@@ -531,7 +534,7 @@ class core_grade_category_testcase extends grade_base_testcase {
// Test excluding the lowest 1 out of 4 grades from aggregation with two 0 grades.
$items[$this->grade_items[2]->id]->aggregationcoef = 0; // Undo marking grade item 2 as "extra credit".
$category = new grade_category();
$category = new \grade_category();
$category->droplow = 1;
$category->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN2; // Simple weighted mean.
$grades = array($this->grade_items[0]->id=>0, // 0 out of 110. Should be excluded from aggregation.
@@ -545,7 +548,7 @@ class core_grade_category_testcase extends grade_base_testcase {
$this->assertEquals($grades[$this->grade_items[4]->id], 0);
// Test excluding the lowest 2 out of 4 grades from aggregation with three 0 grades.
$category = new grade_category();
$category = new \grade_category();
$category->droplow = 2;
$category->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN2; // Simple weighted mean.
$grades = array($this->grade_items[0]->id=>0, // 0 out of 110. Should be excluded from aggregation.
@@ -559,7 +562,7 @@ class core_grade_category_testcase extends grade_base_testcase {
// Test excluding the lowest 5 out of 4 grades from aggregation.
// Just to check we handle this sensibly.
$category = new grade_category();
$category = new \grade_category();
$category->droplow = 5;
$category->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN2; // Simple weighted mean.
$grades = array($this->grade_items[0]->id=>0, // 0 out of 110. Should be excluded from aggregation.
@@ -570,7 +573,7 @@ class core_grade_category_testcase extends grade_base_testcase {
$this->assertEquals(count($grades), 0);
// Test excluding the lowest 4 out of 4 grades from aggregation with one marked as extra credit.
$category = new grade_category();
$category = new \grade_category();
$category->droplow = 4;
$category->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN2; // Simple weighted mean.
$items[$this->grade_items[2]->id]->aggregationcoef = 1; // Mark grade item 2 as "extra credit".
@@ -583,7 +586,7 @@ class core_grade_category_testcase extends grade_base_testcase {
$this->assertEquals($grades[$this->grade_items[2]->id], 6);
// MDL-35667 - There was an infinite loop if several items had the same grade and at least one was extra credit.
$category = new grade_category();
$category = new \grade_category();
$category->droplow = 1;
$category->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN2; // Simple weighted mean.
$items[$this->grade_items[1]->id]->aggregationcoef = 1; // Mark grade item 1 as "extra credit".
@@ -600,7 +603,7 @@ class core_grade_category_testcase extends grade_base_testcase {
}
protected function sub_test_grade_category_is_aggregationcoef_used() {
$category = new grade_category();
$category = new \grade_category();
// Following use aggregationcoef.
$category->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN;
$this->assertTrue($category->is_aggregationcoef_used());
@@ -626,28 +629,28 @@ class core_grade_category_testcase extends grade_base_testcase {
protected function sub_test_grade_category_aggregation_uses_aggregationcoef() {
$this->assertTrue(grade_category::aggregation_uses_aggregationcoef(GRADE_AGGREGATE_WEIGHTED_MEAN));
$this->assertTrue(grade_category::aggregation_uses_aggregationcoef(GRADE_AGGREGATE_WEIGHTED_MEAN2));
$this->assertTrue(grade_category::aggregation_uses_aggregationcoef(GRADE_AGGREGATE_EXTRACREDIT_MEAN));
$this->assertTrue(grade_category::aggregation_uses_aggregationcoef(GRADE_AGGREGATE_SUM));
$this->assertTrue(\grade_category::aggregation_uses_aggregationcoef(GRADE_AGGREGATE_WEIGHTED_MEAN));
$this->assertTrue(\grade_category::aggregation_uses_aggregationcoef(GRADE_AGGREGATE_WEIGHTED_MEAN2));
$this->assertTrue(\grade_category::aggregation_uses_aggregationcoef(GRADE_AGGREGATE_EXTRACREDIT_MEAN));
$this->assertTrue(\grade_category::aggregation_uses_aggregationcoef(GRADE_AGGREGATE_SUM));
$this->assertFalse(grade_category::aggregation_uses_aggregationcoef(GRADE_AGGREGATE_MAX));
$this->assertFalse(grade_category::aggregation_uses_aggregationcoef(GRADE_AGGREGATE_MEAN));
$this->assertFalse(grade_category::aggregation_uses_aggregationcoef(GRADE_AGGREGATE_MEDIAN));
$this->assertFalse(grade_category::aggregation_uses_aggregationcoef(GRADE_AGGREGATE_MIN));
$this->assertFalse(grade_category::aggregation_uses_aggregationcoef(GRADE_AGGREGATE_MODE));
$this->assertFalse(\grade_category::aggregation_uses_aggregationcoef(GRADE_AGGREGATE_MAX));
$this->assertFalse(\grade_category::aggregation_uses_aggregationcoef(GRADE_AGGREGATE_MEAN));
$this->assertFalse(\grade_category::aggregation_uses_aggregationcoef(GRADE_AGGREGATE_MEDIAN));
$this->assertFalse(\grade_category::aggregation_uses_aggregationcoef(GRADE_AGGREGATE_MIN));
$this->assertFalse(\grade_category::aggregation_uses_aggregationcoef(GRADE_AGGREGATE_MODE));
}
protected function sub_test_grade_category_fetch_course_tree() {
$category = new grade_category();
$category = new \grade_category();
$this->assertTrue(method_exists($category, 'fetch_course_tree'));
// TODO: add some tests.
}
protected function sub_test_grade_category_get_children() {
$course_category = grade_category::fetch_course_category($this->courseid);
$course_category = \grade_category::fetch_course_category($this->courseid);
$category = new grade_category($this->grade_categories[0]);
$category = new \grade_category($this->grade_categories[0]);
$this->assertTrue(method_exists($category, 'get_children'));
$children_array = $category->get_children(0);
@@ -664,7 +667,7 @@ class core_grade_category_testcase extends grade_base_testcase {
}
protected function sub_test_grade_category_load_grade_item() {
$category = new grade_category($this->grade_categories[0]);
$category = new \grade_category($this->grade_categories[0]);
$this->assertTrue(method_exists($category, 'load_grade_item'));
$this->assertEquals(null, $category->grade_item);
$category->load_grade_item();
@@ -672,14 +675,14 @@ class core_grade_category_testcase extends grade_base_testcase {
}
protected function sub_test_grade_category_get_grade_item() {
$category = new grade_category($this->grade_categories[0]);
$category = new \grade_category($this->grade_categories[0]);
$this->assertTrue(method_exists($category, 'get_grade_item'));
$grade_item = $category->get_grade_item();
$this->assertEquals($this->grade_items[3]->id, $grade_item->id);
}
protected function sub_test_grade_category_load_parent_category() {
$category = new grade_category($this->grade_categories[1]);
$category = new \grade_category($this->grade_categories[1]);
$this->assertTrue(method_exists($category, 'load_parent_category'));
$this->assertEquals(null, $category->parent_category);
$category->load_parent_category();
@@ -687,7 +690,7 @@ class core_grade_category_testcase extends grade_base_testcase {
}
protected function sub_test_grade_category_get_parent_category() {
$category = new grade_category($this->grade_categories[1]);
$category = new \grade_category($this->grade_categories[1]);
$this->assertTrue(method_exists($category, 'get_parent_category'));
$parent_category = $category->get_parent_category();
$this->assertEquals($this->grade_categories[0]->id, $parent_category->id);
@@ -697,7 +700,7 @@ class core_grade_category_testcase extends grade_base_testcase {
* Tests the getter of the category fullname with escaped HTML.
*/
protected function sub_test_grade_category_get_name_escaped() {
$category = new grade_category($this->grade_categories[0]);
$category = new \grade_category($this->grade_categories[0]);
$this->assertTrue(method_exists($category, 'get_name'));
$this->assertEquals(format_string($this->grade_categories[0]->fullname, true, ['escape' => true]),
$category->get_name(true));
@@ -707,60 +710,60 @@ class core_grade_category_testcase extends grade_base_testcase {
* Tests the getter of the category fullname with unescaped HTML.
*/
protected function sub_test_grade_category_get_name_unescaped() {
$category = new grade_category($this->grade_categories[0]);
$category = new \grade_category($this->grade_categories[0]);
$this->assertTrue(method_exists($category, 'get_name'));
$this->assertEquals(format_string($this->grade_categories[0]->fullname, true, ['escape' => false]),
$category->get_name(false));
}
protected function sub_test_grade_category_set_parent() {
$category = new grade_category($this->grade_categories[1]);
$category = new \grade_category($this->grade_categories[1]);
$this->assertTrue(method_exists($category, 'set_parent'));
// TODO: implement detailed tests.
$course_category = grade_category::fetch_course_category($this->courseid);
$course_category = \grade_category::fetch_course_category($this->courseid);
$this->assertTrue($category->set_parent($course_category->id));
$this->assertEquals($course_category->id, $category->parent);
}
protected function sub_test_grade_category_get_final() {
$category = new grade_category($this->grade_categories[0]);
$category = new \grade_category($this->grade_categories[0]);
$this->assertTrue(method_exists($category, 'get_final'));
$category->load_grade_item();
$this->assertEquals($category->get_final(), $category->grade_item->get_final());
}
protected function sub_test_grade_category_get_sortorder() {
$category = new grade_category($this->grade_categories[0]);
$category = new \grade_category($this->grade_categories[0]);
$this->assertTrue(method_exists($category, 'get_sortorder'));
$category->load_grade_item();
$this->assertEquals($category->get_sortorder(), $category->grade_item->get_sortorder());
}
protected function sub_test_grade_category_set_sortorder() {
$category = new grade_category($this->grade_categories[0]);
$category = new \grade_category($this->grade_categories[0]);
$this->assertTrue(method_exists($category, 'set_sortorder'));
$category->load_grade_item();
$this->assertEquals($category->set_sortorder(10), $category->grade_item->set_sortorder(10));
}
protected function sub_test_grade_category_move_after_sortorder() {
$category = new grade_category($this->grade_categories[0]);
$category = new \grade_category($this->grade_categories[0]);
$this->assertTrue(method_exists($category, 'move_after_sortorder'));
$category->load_grade_item();
$this->assertEquals($category->move_after_sortorder(10), $category->grade_item->move_after_sortorder(10));
}
protected function sub_test_grade_category_is_course_category() {
$category = grade_category::fetch_course_category($this->courseid);
$category = \grade_category::fetch_course_category($this->courseid);
$this->assertTrue(method_exists($category, 'is_course_category'));
$this->assertTrue($category->is_course_category());
}
protected function sub_test_grade_category_fetch_course_category() {
$category = new grade_category();
$category = new \grade_category();
$this->assertTrue(method_exists($category, 'fetch_course_category'));
$category = grade_category::fetch_course_category($this->courseid);
$category = \grade_category::fetch_course_category($this->courseid);
$this->assertTrue(empty($category->parent));
}
/**
@@ -771,14 +774,14 @@ class core_grade_category_testcase extends grade_base_testcase {
}
protected function sub_test_grade_category_is_locked() {
$category = new grade_category($this->grade_categories[0]);
$category = new \grade_category($this->grade_categories[0]);
$this->assertTrue(method_exists($category, 'is_locked'));
$category->load_grade_item();
$this->assertEquals($category->is_locked(), $category->grade_item->is_locked());
}
protected function sub_test_grade_category_set_locked() {
$category = new grade_category($this->grade_categories[0]);
$category = new \grade_category($this->grade_categories[0]);
$this->assertTrue(method_exists($category, 'set_locked'));
// Will return false as cannot lock a grade that needs updating.
@@ -786,19 +789,19 @@ class core_grade_category_testcase extends grade_base_testcase {
grade_regrade_final_grades($this->courseid);
// Get the category from the db again.
$category = new grade_category($this->grade_categories[0]);
$category = new \grade_category($this->grade_categories[0]);
$this->assertTrue($category->set_locked(1));
}
protected function sub_test_grade_category_is_hidden() {
$category = new grade_category($this->grade_categories[0]);
$category = new \grade_category($this->grade_categories[0]);
$this->assertTrue(method_exists($category, 'is_hidden'));
$category->load_grade_item();
$this->assertEquals($category->is_hidden(), $category->grade_item->is_hidden());
}
protected function sub_test_grade_category_set_hidden() {
$category = new grade_category($this->grade_categories[0]);
$category = new \grade_category($this->grade_categories[0]);
$this->assertTrue(method_exists($category, 'set_hidden'));
$category->set_hidden(1, true);
$category->load_grade_item();
@@ -806,13 +809,13 @@ class core_grade_category_testcase extends grade_base_testcase {
}
protected function sub_test_grade_category_can_control_visibility() {
$category = new grade_category($this->grade_categories[0]);
$category = new \grade_category($this->grade_categories[0]);
$this->assertTrue($category->can_control_visibility());
}
protected function sub_test_grade_category_insert_course_category() {
// Beware: adding a duplicate course category messes up the data in a way that's hard to recover from.
$grade_category = new grade_category();
$grade_category = new \grade_category();
$this->assertTrue(method_exists($grade_category, 'insert_course_category'));
$id = $grade_category->insert_course_category($this->courseid);
@@ -825,7 +828,7 @@ class core_grade_category_testcase extends grade_base_testcase {
}
protected function generate_random_raw_grade($item, $userid) {
$grade = new grade_grade();
$grade = new \grade_grade();
$grade->itemid = $item->id;
$grade->userid = $userid;
$grade->grademin = 0;
@@ -837,7 +840,7 @@ class core_grade_category_testcase extends grade_base_testcase {
}
protected function sub_test_grade_category_is_extracredit_used() {
$category = new grade_category();
$category = new \grade_category();
// Following use aggregationcoef.
$category->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN2;
$this->assertTrue($category->is_extracredit_used());
@@ -863,16 +866,16 @@ class core_grade_category_testcase extends grade_base_testcase {
protected function sub_test_grade_category_aggregation_uses_extracredit() {
$this->assertTrue(grade_category::aggregation_uses_extracredit(GRADE_AGGREGATE_WEIGHTED_MEAN2));
$this->assertTrue(grade_category::aggregation_uses_extracredit(GRADE_AGGREGATE_EXTRACREDIT_MEAN));
$this->assertTrue(grade_category::aggregation_uses_extracredit(GRADE_AGGREGATE_SUM));
$this->assertTrue(\grade_category::aggregation_uses_extracredit(GRADE_AGGREGATE_WEIGHTED_MEAN2));
$this->assertTrue(\grade_category::aggregation_uses_extracredit(GRADE_AGGREGATE_EXTRACREDIT_MEAN));
$this->assertTrue(\grade_category::aggregation_uses_extracredit(GRADE_AGGREGATE_SUM));
$this->assertFalse(grade_category::aggregation_uses_extracredit(GRADE_AGGREGATE_WEIGHTED_MEAN));
$this->assertFalse(grade_category::aggregation_uses_extracredit(GRADE_AGGREGATE_MAX));
$this->assertFalse(grade_category::aggregation_uses_extracredit(GRADE_AGGREGATE_MEAN));
$this->assertFalse(grade_category::aggregation_uses_extracredit(GRADE_AGGREGATE_MEDIAN));
$this->assertFalse(grade_category::aggregation_uses_extracredit(GRADE_AGGREGATE_MIN));
$this->assertFalse(grade_category::aggregation_uses_extracredit(GRADE_AGGREGATE_MODE));
$this->assertFalse(\grade_category::aggregation_uses_extracredit(GRADE_AGGREGATE_WEIGHTED_MEAN));
$this->assertFalse(\grade_category::aggregation_uses_extracredit(GRADE_AGGREGATE_MAX));
$this->assertFalse(\grade_category::aggregation_uses_extracredit(GRADE_AGGREGATE_MEAN));
$this->assertFalse(\grade_category::aggregation_uses_extracredit(GRADE_AGGREGATE_MEDIAN));
$this->assertFalse(\grade_category::aggregation_uses_extracredit(GRADE_AGGREGATE_MIN));
$this->assertFalse(\grade_category::aggregation_uses_extracredit(GRADE_AGGREGATE_MODE));
}
/**
@@ -880,8 +883,8 @@ class core_grade_category_testcase extends grade_base_testcase {
*/
protected function sub_test_grade_category_total_visibility() {
// 15 is a manual grade item in grade_categories[5].
$category = new grade_category($this->grade_categories[5], true);
$gradeitem = new grade_item($this->grade_items[15], true);
$category = new \grade_category($this->grade_categories[5], true);
$gradeitem = new \grade_item($this->grade_items[15], true);
// Hide grade category.
$category->set_hidden(true, true);
+83 -81
View File
@@ -14,19 +14,21 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package core_grades
* @category phpunit
* @copyright [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/fixtures/lib.php');
class core_grade_grade_testcase extends grade_base_testcase {
/**
* Test grade grades
*
* @package core
* @category test
* @copyright [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class grade_grade_test extends \grade_base_testcase {
public function test_grade_grade() {
$this->sub_test_grade_grade_construct();
@@ -44,7 +46,7 @@ class core_grade_grade_testcase extends grade_base_testcase {
}
protected function sub_test_grade_grade_construct() {
$params = new stdClass();
$params = new \stdClass();
$params->itemid = $this->grade_items[0]->id;
$params->userid = 1;
@@ -52,13 +54,13 @@ class core_grade_grade_testcase extends grade_base_testcase {
$params->rawgrademax = 110;
$params->rawgrademin = 18;
$grade_grade = new grade_grade($params, false);
$grade_grade = new \grade_grade($params, false);
$this->assertEquals($params->itemid, $grade_grade->itemid);
$this->assertEquals($params->rawgrade, $grade_grade->rawgrade);
}
protected function sub_test_grade_grade_insert() {
$grade_grade = new grade_grade();
$grade_grade = new \grade_grade();
$this->assertTrue(method_exists($grade_grade, 'insert'));
$grade_grade->itemid = $this->grade_items[0]->id;
@@ -87,29 +89,29 @@ class core_grade_grade_testcase extends grade_base_testcase {
}
protected function sub_test_grade_grade_update() {
$grade_grade = new grade_grade($this->grade_grades[0], false);
$grade_grade = new \grade_grade($this->grade_grades[0], false);
$this->assertTrue(method_exists($grade_grade, 'update'));
}
protected function sub_test_grade_grade_fetch() {
$grade_grade = new grade_grade();
$grade_grade = new \grade_grade();
$this->assertTrue(method_exists($grade_grade, 'fetch'));
$grades = grade_grade::fetch(array('id'=>$this->grade_grades[0]->id));
$grades = \grade_grade::fetch(array('id'=>$this->grade_grades[0]->id));
$this->assertEquals($this->grade_grades[0]->id, $grades->id);
$this->assertEquals($this->grade_grades[0]->rawgrade, $grades->rawgrade);
}
protected function sub_test_grade_grade_fetch_all() {
$grade_grade = new grade_grade();
$grade_grade = new \grade_grade();
$this->assertTrue(method_exists($grade_grade, 'fetch_all'));
$grades = grade_grade::fetch_all(array());
$grades = \grade_grade::fetch_all(array());
$this->assertEquals(count($this->grade_grades), count($grades));
}
protected function sub_test_grade_grade_load_grade_item() {
$grade_grade = new grade_grade($this->grade_grades[0], false);
$grade_grade = new \grade_grade($this->grade_grades[0], false);
$this->assertTrue(method_exists($grade_grade, 'load_grade_item'));
$this->assertNull($grade_grade->grade_item);
$this->assertNotEmpty($grade_grade->itemid);
@@ -120,8 +122,8 @@ class core_grade_grade_testcase extends grade_base_testcase {
protected function sub_test_grade_grade_standardise_score() {
$this->assertEquals(4, round(grade_grade::standardise_score(6, 0, 7, 0, 5)));
$this->assertEquals(40, grade_grade::standardise_score(50, 30, 80, 0, 100));
$this->assertEquals(4, round(\grade_grade::standardise_score(6, 0, 7, 0, 5)));
$this->assertEquals(40, \grade_grade::standardise_score(50, 30, 80, 0, 100));
}
@@ -129,8 +131,8 @@ class core_grade_grade_testcase extends grade_base_testcase {
* Disabling this test: the set_locked() arguments have been modified, rendering these tests useless until they are re-written
protected function test_grade_grade_set_locked() {
$grade_item = new grade_item($this->grade_items[0]);
$grade = new grade_grade($grade_item->get_final(1));
$grade_item = new \grade_item($this->grade_items[0]);
$grade = new \grade_grade($grade_item->get_final(1));
$this->assertTrue(method_exists($grade, 'set_locked'));
$this->assertTrue(empty($grade_item->locked));
@@ -142,20 +144,20 @@ class core_grade_grade_testcase extends grade_base_testcase {
$this->assertTrue(empty($grade->locked));
$this->assertTrue($grade_item->set_locked(true, true));
$grade = new grade_grade($grade_item->get_final(1));
$grade = new \grade_grade($grade_item->get_final(1));
$this->assertFalse(empty($grade->locked));
$this->assertFalse($grade->set_locked(true, false));
$this->assertTrue($grade_item->set_locked(true, false));
$grade = new grade_grade($grade_item->get_final(1));
$grade = new \grade_grade($grade_item->get_final(1));
$this->assertTrue($grade->set_locked(true, false));
}
*/
protected function sub_test_grade_grade_is_locked() {
$grade = new grade_grade($this->grade_grades[0], false);
$grade = new \grade_grade($this->grade_grades[0], false);
$this->assertTrue(method_exists($grade, 'is_locked'));
$this->assertFalse($grade->is_locked());
@@ -164,8 +166,8 @@ class core_grade_grade_testcase extends grade_base_testcase {
}
protected function sub_test_grade_grade_set_hidden() {
$grade = new grade_grade($this->grade_grades[0], false);
$grade_item = new grade_item($this->grade_items[0], false);
$grade = new \grade_grade($this->grade_grades[0], false);
$grade_item = new \grade_item($this->grade_items[0], false);
$this->assertTrue(method_exists($grade, 'set_hidden'));
$this->assertEquals(0, $grade_item->hidden);
@@ -182,7 +184,7 @@ class core_grade_grade_testcase extends grade_base_testcase {
}
protected function sub_test_grade_grade_is_hidden() {
$grade = new grade_grade($this->grade_grades[0], false);
$grade = new \grade_grade($this->grade_grades[0], false);
$this->assertTrue(method_exists($grade, 'is_hidden'));
$this->assertFalse($grade->is_hidden());
@@ -197,7 +199,7 @@ class core_grade_grade_testcase extends grade_base_testcase {
}
/**
* Test grade_grade::flatten_dependencies_array()
* Test \grade_grade::flatten_dependencies_array()
*
* @covers \grade_grade::flatten_dependencies_array()
*/
@@ -208,14 +210,14 @@ class core_grade_grade_testcase extends grade_base_testcase {
$expecteda = array(1 => array(2, 3, 4), 2 => array(), 3 => array(4), 4 => array());
$expectedb = array(1 => 1);
test_grade_grade_flatten_dependencies_array::test_flatten_dependencies_array($a, $b);
\test_grade_grade_flatten_dependencies_array::test_flatten_dependencies_array($a, $b);
$this->assertSame($expecteda, $a);
$this->assertSame($expectedb, $b);
// Edge case - empty arrays.
$a = $b = $expecteda = $expectedb = array();
test_grade_grade_flatten_dependencies_array::test_flatten_dependencies_array($a, $b);
\test_grade_grade_flatten_dependencies_array::test_flatten_dependencies_array($a, $b);
$this->assertSame($expecteda, $a);
$this->assertSame($expectedb, $b);
@@ -224,7 +226,7 @@ class core_grade_grade_testcase extends grade_base_testcase {
$b = array();
$expecteda = array(1 => array(1, 2, 3), 2 => array(1, 2, 3), 3 => array(1, 2, 3));
test_grade_grade_flatten_dependencies_array::test_flatten_dependencies_array($a, $b);
\test_grade_grade_flatten_dependencies_array::test_flatten_dependencies_array($a, $b);
$this->assertSame($expecteda, $a);
// Note - we don't test the depth when we got circular dependencies - the main thing we wanted to test was that there was
// no ka-boom. The result would be hard to understand and doesn't matter.
@@ -234,7 +236,7 @@ class core_grade_grade_testcase extends grade_base_testcase {
$b = array();
$expecteda = array(1 => array(1, 2, 3, 4), 2 => array(1, 2, 3, 4), 3 => array(1, 2, 3, 4), 4 => array(1, 2, 3, 4));
test_grade_grade_flatten_dependencies_array::test_flatten_dependencies_array($a, $b);
\test_grade_grade_flatten_dependencies_array::test_flatten_dependencies_array($a, $b);
$this->assertSame($expecteda, $a);
// Missing first level dependency.
@@ -243,7 +245,7 @@ class core_grade_grade_testcase extends grade_base_testcase {
$expecteda = array(1 => array(2, 3, 4), 3 => array(4), 4 => array());
$expectedb = array(1 => 1);
test_grade_grade_flatten_dependencies_array::test_flatten_dependencies_array($a, $b);
\test_grade_grade_flatten_dependencies_array::test_flatten_dependencies_array($a, $b);
$this->assertSame($expecteda, $a);
$this->assertSame($expectedb, $b);
@@ -253,7 +255,7 @@ class core_grade_grade_testcase extends grade_base_testcase {
$expecteda = array(1 => array(2, 3, 4), 2 => array(), 3 => array(4));
$expectedb = array(1 => 1);
test_grade_grade_flatten_dependencies_array::test_flatten_dependencies_array($a, $b);
\test_grade_grade_flatten_dependencies_array::test_flatten_dependencies_array($a, $b);
$this->assertSame($expecteda, $a);
$this->assertSame($expectedb, $b);
@@ -263,7 +265,7 @@ class core_grade_grade_testcase extends grade_base_testcase {
$expecteda = array(1 => array(2, 3, 4), 2 => array(3, 4), 3 => array(4), 4 => array());
$expectedb = array(1 => 2, 2 => 1);
test_grade_grade_flatten_dependencies_array::test_flatten_dependencies_array($a, $b);
\test_grade_grade_flatten_dependencies_array::test_flatten_dependencies_array($a, $b);
$this->assertSame($expecteda, $a);
$this->assertSame($expectedb, $b);
@@ -273,7 +275,7 @@ class core_grade_grade_testcase extends grade_base_testcase {
$expecteda = array(1 => array(2, 3, 4), 2 => array(), 3 => array(4), 4 => array());
$expectedb = array(1 => 1);
test_grade_grade_flatten_dependencies_array::test_flatten_dependencies_array($a, $b);
\test_grade_grade_flatten_dependencies_array::test_flatten_dependencies_array($a, $b);
$this->assertSame($expecteda, $a);
$this->assertSame($expectedb, $b);
@@ -283,7 +285,7 @@ class core_grade_grade_testcase extends grade_base_testcase {
$expecteda = array(1 => array(2, 3, 4), 2 => array(), 3 => array(4), 4 => array());
$expectedb = array(1 => 1);
test_grade_grade_flatten_dependencies_array::test_flatten_dependencies_array($a, $b);
\test_grade_grade_flatten_dependencies_array::test_flatten_dependencies_array($a, $b);
$this->assertSame($expecteda, $a);
$this->assertSame($expectedb, $b);
@@ -293,7 +295,7 @@ class core_grade_grade_testcase extends grade_base_testcase {
$expecteda = array(1 => array(2, 3, 4), 2 => array(), 3 => array(4), 4 => array());
$expectedb = array(1 => 1);
test_grade_grade_flatten_dependencies_array::test_flatten_dependencies_array($a, $b);
\test_grade_grade_flatten_dependencies_array::test_flatten_dependencies_array($a, $b);
$this->assertSame($expecteda, $a);
$this->assertSame($expectedb, $b);
}
@@ -307,13 +309,13 @@ class core_grade_grade_testcase extends grade_base_testcase {
$user = $this->getDataGenerator()->create_user();
$assignrecord = $this->getDataGenerator()->create_module('assign', array('course' => $course, 'grade' => 100));
$cm = get_coursemodule_from_instance('assign', $assignrecord->id);
$assigncontext = context_module::instance($cm->id);
$assign = new assign($assigncontext, $cm, $course);
$assigncontext = \context_module::instance($cm->id);
$assign = new \assign($assigncontext, $cm, $course);
// Fetch the assignment item.
$giparams = array('itemtype' => 'mod', 'itemmodule' => 'assign', 'iteminstance' => $assignrecord->id,
'courseid' => $course->id, 'itemnumber' => 0);
$gi = grade_item::fetch($giparams);
$gi = \grade_item::fetch($giparams);
$this->assertEquals(0, $gi->grademin);
$this->assertEquals(100, $gi->grademax);
@@ -323,7 +325,7 @@ class core_grade_grade_testcase extends grade_base_testcase {
$assign->update_grade($usergrade);
// Check the grade stored in gradebook.
$gg = grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$gg = \grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$this->assertEquals(10, $gg->rawgrade);
$this->assertEquals(0, $gg->get_grade_min());
$this->assertEquals(100, $gg->get_grade_max());
@@ -334,33 +336,33 @@ class core_grade_grade_testcase extends grade_base_testcase {
$gi->update();
// Fetch the updated item.
$gi = grade_item::fetch($giparams);
$gi = \grade_item::fetch($giparams);
// Now check the grade grade min/max with system setting.
$CFG->grade_minmaxtouse = GRADE_MIN_MAX_FROM_GRADE_ITEM;
grade_set_setting($course->id, 'minmaxtouse', null); // Ensure no course setting.
$gg = grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$gg = \grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$this->assertEquals(2, $gg->get_grade_min());
$this->assertEquals(50, $gg->get_grade_max());
// Now with other system setting.
$CFG->grade_minmaxtouse = GRADE_MIN_MAX_FROM_GRADE_GRADE;
grade_set_setting($course->id, 'minmaxtouse', null); // Ensure no course setting, and reset static cache.
$gg = grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$gg = \grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$this->assertEquals(0, $gg->get_grade_min());
$this->assertEquals(100, $gg->get_grade_max());
// Now with overriden setting in course.
$CFG->grade_minmaxtouse = GRADE_MIN_MAX_FROM_GRADE_ITEM;
grade_set_setting($course->id, 'minmaxtouse', GRADE_MIN_MAX_FROM_GRADE_GRADE);
$gg = grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$gg = \grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$this->assertEquals(0, $gg->get_grade_min());
$this->assertEquals(100, $gg->get_grade_max());
$CFG->grade_minmaxtouse = GRADE_MIN_MAX_FROM_GRADE_GRADE;
grade_set_setting($course->id, 'minmaxtouse', GRADE_MIN_MAX_FROM_GRADE_ITEM);
$gg = grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$gg = \grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$this->assertEquals(2, $gg->get_grade_min());
$this->assertEquals(50, $gg->get_grade_max());
@@ -374,7 +376,7 @@ class core_grade_grade_testcase extends grade_base_testcase {
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_user();
$gi = grade_item::fetch_course_item($course->id);
$gi = \grade_item::fetch_course_item($course->id);
// Fetch the category item.
$this->assertEquals(0, $gi->grademin);
@@ -384,7 +386,7 @@ class core_grade_grade_testcase extends grade_base_testcase {
$gi->update_final_grade($user->id, 10);
// Check the grade min/max stored in gradebook.
$gg = grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$gg = \grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$this->assertEquals(0, $gg->get_grade_min());
$this->assertEquals(100, $gg->get_grade_max());
@@ -394,33 +396,33 @@ class core_grade_grade_testcase extends grade_base_testcase {
$gi->update();
// Fetch the updated item.
$gi = grade_item::fetch_course_item($course->id);
$gi = \grade_item::fetch_course_item($course->id);
// Now check the grade grade min/max with system setting.
$CFG->grade_minmaxtouse = GRADE_MIN_MAX_FROM_GRADE_ITEM;
grade_set_setting($course->id, 'minmaxtouse', null); // Ensure no course setting.
$gg = grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$gg = \grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$this->assertEquals(0, $gg->get_grade_min());
$this->assertEquals(100, $gg->get_grade_max());
// Now with other system setting.
$CFG->grade_minmaxtouse = GRADE_MIN_MAX_FROM_GRADE_GRADE;
grade_set_setting($course->id, 'minmaxtouse', null); // Ensure no course setting, and reset static cache.
$gg = grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$gg = \grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$this->assertEquals(0, $gg->get_grade_min());
$this->assertEquals(100, $gg->get_grade_max());
// Now with overriden setting in course.
$CFG->grade_minmaxtouse = GRADE_MIN_MAX_FROM_GRADE_ITEM;
grade_set_setting($course->id, 'minmaxtouse', GRADE_MIN_MAX_FROM_GRADE_GRADE);
$gg = grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$gg = \grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$this->assertEquals(0, $gg->get_grade_min());
$this->assertEquals(100, $gg->get_grade_max());
$CFG->grade_minmaxtouse = GRADE_MIN_MAX_FROM_GRADE_GRADE;
grade_set_setting($course->id, 'minmaxtouse', GRADE_MIN_MAX_FROM_GRADE_ITEM);
$gg = grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$gg = \grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$this->assertEquals(0, $gg->get_grade_min());
$this->assertEquals(100, $gg->get_grade_max());
@@ -434,10 +436,10 @@ class core_grade_grade_testcase extends grade_base_testcase {
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_user();
$coursegi = grade_item::fetch_course_item($course->id);
$coursegi = \grade_item::fetch_course_item($course->id);
// Create a category item.
$gc = new grade_category(array('courseid' => $course->id, 'fullname' => 'test'), false);
$gc = new \grade_category(array('courseid' => $course->id, 'fullname' => 'test'), false);
$gc->insert();
$gi = $gc->get_grade_item();
$gi->grademax = 100;
@@ -446,7 +448,7 @@ class core_grade_grade_testcase extends grade_base_testcase {
// Fetch the category item.
$giparams = array('itemtype' => 'category', 'iteminstance' => $gc->id);
$gi = grade_item::fetch($giparams);
$gi = \grade_item::fetch($giparams);
$this->assertEquals(0, $gi->grademin);
$this->assertEquals(100, $gi->grademax);
@@ -454,7 +456,7 @@ class core_grade_grade_testcase extends grade_base_testcase {
$gi->update_final_grade($user->id, 10);
// Check the grade min/max stored in gradebook.
$gg = grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$gg = \grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$this->assertEquals(0, $gg->get_grade_min());
$this->assertEquals(100, $gg->get_grade_max());
@@ -464,33 +466,33 @@ class core_grade_grade_testcase extends grade_base_testcase {
$gi->update();
// Fetch the updated item.
$gi = grade_item::fetch($giparams);
$gi = \grade_item::fetch($giparams);
// Now check the grade grade min/max with system setting.
$CFG->grade_minmaxtouse = GRADE_MIN_MAX_FROM_GRADE_ITEM;
grade_set_setting($course->id, 'minmaxtouse', null); // Ensure no course setting.
$gg = grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$gg = \grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$this->assertEquals(0, $gg->get_grade_min());
$this->assertEquals(100, $gg->get_grade_max());
// Now with other system setting.
$CFG->grade_minmaxtouse = GRADE_MIN_MAX_FROM_GRADE_GRADE;
grade_set_setting($course->id, 'minmaxtouse', null); // Ensure no course setting, and reset static cache.
$gg = grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$gg = \grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$this->assertEquals(0, $gg->get_grade_min());
$this->assertEquals(100, $gg->get_grade_max());
// Now with overriden setting in course.
$CFG->grade_minmaxtouse = GRADE_MIN_MAX_FROM_GRADE_ITEM;
grade_set_setting($course->id, 'minmaxtouse', GRADE_MIN_MAX_FROM_GRADE_GRADE);
$gg = grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$gg = \grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$this->assertEquals(0, $gg->get_grade_min());
$this->assertEquals(100, $gg->get_grade_max());
$CFG->grade_minmaxtouse = GRADE_MIN_MAX_FROM_GRADE_GRADE;
grade_set_setting($course->id, 'minmaxtouse', GRADE_MIN_MAX_FROM_GRADE_ITEM);
$gg = grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$gg = \grade_grade::fetch(array('userid' => $user->id, 'itemid' => $gi->id));
$this->assertEquals(0, $gg->get_grade_min());
$this->assertEquals(100, $gg->get_grade_max());
@@ -504,13 +506,13 @@ class core_grade_grade_testcase extends grade_base_testcase {
$dg = $this->getDataGenerator();
// Create the data we need for the tests.
$fs = new file_storage();
$fs = new \file_storage();
$u1 = $dg->create_user();
$c1 = $dg->create_course();
$a1 = $dg->create_module('assign', ['course' => $c1->id]);
$a1context = context_module::instance($a1->cmid);
$a1context = \context_module::instance($a1->cmid);
$gi = new grade_item($dg->create_grade_item(
$gi = new \grade_item($dg->create_grade_item(
[
'courseid' => $c1->id,
'itemtype' => 'mod',
@@ -543,7 +545,7 @@ class core_grade_grade_testcase extends grade_base_testcase {
$files = $fs->get_area_files($a1context->id, GRADE_FILE_COMPONENT, GRADE_HISTORY_FEEDBACK_FILEAREA);
$this->assertEquals(2, count($files));
$gg = grade_grade::fetch(array('userid' => $u1->id, 'itemid' => $gi->id));
$gg = \grade_grade::fetch(array('userid' => $u1->id, 'itemid' => $gi->id));
$gg->delete();
@@ -586,7 +588,7 @@ class core_grade_grade_testcase extends grade_base_testcase {
$c1 = $dg->create_course();
$a1 = $dg->create_module('assign', ['course' => $c1->id]);
$gi = new grade_item($dg->create_grade_item(
$gi = new \grade_item($dg->create_grade_item(
[
'courseid' => $c1->id,
'itemtype' => 'mod',
@@ -600,7 +602,7 @@ class core_grade_grade_testcase extends grade_base_testcase {
grade_update('mod/assign', $gi->courseid, $gi->itemtype, $gi->itemmodule, $gi->iteminstance,
$gi->itemnumber, ['userid' => $u2->id]);
$gg = grade_grade::fetch(array('userid' => $u1->id, 'itemid' => $gi->id));
$gg = \grade_grade::fetch(array('userid' => $u1->id, 'itemid' => $gi->id));
$this->assertEquals($u1->id, $gg->userid);
$gg->load_grade_item();
$this->assertEquals($gi->id, $gg->grade_item->id);
@@ -613,14 +615,14 @@ class core_grade_grade_testcase extends grade_base_testcase {
$sink->close();
$this->assertInstanceOf('core\event\grade_deleted', $event);
$gg = grade_grade::fetch(array('userid' => $u2->id, 'itemid' => $gi->id));
$gg = \grade_grade::fetch(array('userid' => $u2->id, 'itemid' => $gi->id));
$this->assertEquals($u2->id, $gg->userid);
$gg->load_grade_item();
$this->assertEquals($gi->id, $gg->grade_item->id);
// Delete grade item, mock up orphaned grade_grades.
$DB->delete_records('grade_items', ['id' => $gi->id]);
$gg = grade_grade::fetch(array('userid' => $u2->id, 'itemid' => $gi->id));
$gg = \grade_grade::fetch(array('userid' => $u2->id, 'itemid' => $gi->id));
$this->assertEquals($u2->id, $gg->userid);
// No event is triggered and there is a debugging message.
@@ -632,7 +634,7 @@ class core_grade_grade_testcase extends grade_base_testcase {
$this->assertEmpty($events);
// The grade should be deleted.
$gg = grade_grade::fetch(array('userid' => $u2->id, 'itemid' => $gi->id));
$gg = \grade_grade::fetch(array('userid' => $u2->id, 'itemid' => $gi->id));
$this->assertEmpty($gg);
}
@@ -648,11 +650,11 @@ class core_grade_grade_testcase extends grade_base_testcase {
$assignment2 = $generator->create_module('assign', ['course' => $course1->id]);
// Create a category item.
$gradecategory = new grade_category(array('courseid' => $course1->id, 'fullname' => 'test'), false);
$gradecategory = new \grade_category(array('courseid' => $course1->id, 'fullname' => 'test'), false);
$gradecategoryid = $gradecategory->insert();
// Create one hidden grade item.
$gradeitem1a = new grade_item($generator->create_grade_item(
$gradeitem1a = new \grade_item($generator->create_grade_item(
[
'courseid' => $course1->id,
'itemtype' => 'mod',
@@ -688,7 +690,7 @@ class core_grade_grade_testcase extends grade_base_testcase {
grade_regrade_final_grades($course1->id);
// Set grade override.
$gradegrade = grade_grade::fetch([
$gradegrade = \grade_grade::fetch([
'userid' => $user1->id,
'itemid' => $gradeitem->id,
]);
@@ -703,14 +705,14 @@ class core_grade_grade_testcase extends grade_base_testcase {
/**
* Call get_hiding_affected().
* @param stdClass $course The course object
* @param stdClass $user The student object
* @param \stdClass $course The course object
* @param \stdClass $user The student object
* @return array
*/
private function call_get_hiding_affected($course, $user) {
global $DB;
$items = grade_item::fetch_all(array('courseid' => $course->id));
$items = \grade_item::fetch_all(array('courseid' => $course->id));
$grades = array();
$sql = "SELECT g.*
FROM {grade_grades} g
@@ -718,13 +720,13 @@ class core_grade_grade_testcase extends grade_base_testcase {
WHERE g.userid = :userid AND gi.courseid = :courseid";
if ($gradesrecords = $DB->get_records_sql($sql, ['userid' => $user->id, 'courseid' => $course->id])) {
foreach ($gradesrecords as $grade) {
$grades[$grade->itemid] = new grade_grade($grade, false);
$grades[$grade->itemid] = new \grade_grade($grade, false);
}
unset($gradesrecords);
}
foreach ($items as $itemid => $gradeitem) {
if (!isset($grades[$itemid])) {
$gradegrade = new grade_grade();
$gradegrade = new \grade_grade();
$gradegrade->userid = $user->id;
$gradegrade->itemid = $gradeitem->id;
$grades[$itemid] = $gradegrade;
@@ -732,6 +734,6 @@ class core_grade_grade_testcase extends grade_base_testcase {
$gradeitem->grade_item = $gradeitem;
}
return grade_grade::get_hiding_affected($grades, $items);
return \grade_grade::get_hiding_affected($grades, $items);
}
}
+111 -108
View File
@@ -14,18 +14,21 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package core_grades
* @category phpunit
* @copyright [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/fixtures/lib.php');
class core_grade_item_testcase extends grade_base_testcase {
/**
* Test grade items
*
* @package core
* @category test
* @copyright [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class grade_item_test extends \grade_base_testcase {
public function test_grade_item() {
$this->sub_test_grade_item_construct();
$this->sub_test_grade_item_insert();
@@ -73,7 +76,7 @@ class core_grade_item_testcase extends grade_base_testcase {
}
protected function sub_test_grade_item_construct() {
$params = new stdClass();
$params = new \stdClass();
$params->courseid = $this->courseid;
$params->categoryid = $this->grade_categories[1]->id;
@@ -82,7 +85,7 @@ class core_grade_item_testcase extends grade_base_testcase {
$params->itemmodule = 'database';
$params->iteminfo = 'Grade item used for unit testing';
$grade_item = new grade_item($params, false);
$grade_item = new \grade_item($params, false);
$this->assertEquals($params->courseid, $grade_item->courseid);
$this->assertEquals($params->categoryid, $grade_item->categoryid);
@@ -90,7 +93,7 @@ class core_grade_item_testcase extends grade_base_testcase {
}
protected function sub_test_grade_item_insert() {
$grade_item = new grade_item();
$grade_item = new \grade_item();
$this->assertTrue(method_exists($grade_item, 'insert'));
$grade_item->courseid = $this->courseid;
@@ -113,7 +116,7 @@ class core_grade_item_testcase extends grade_base_testcase {
protected function sub_test_grade_item_delete() {
global $DB;
$grade_item = new grade_item($this->grade_items[7], false); // Use a grade item not touched by previous (or future) tests.
$grade_item = new \grade_item($this->grade_items[7], false); // Use a grade item not touched by previous (or future) tests.
$this->assertTrue(method_exists($grade_item, 'delete'));
// Add two files.
@@ -149,7 +152,7 @@ class core_grade_item_testcase extends grade_base_testcase {
protected function sub_test_grade_item_update() {
global $DB;
$grade_item = new grade_item($this->grade_items[0], false);
$grade_item = new \grade_item($this->grade_items[0], false);
$this->assertTrue(method_exists($grade_item, 'update'));
$grade_item->iteminfo = 'Updated info for this unittest grade_item';
@@ -165,7 +168,7 @@ class core_grade_item_testcase extends grade_base_testcase {
}
protected function sub_test_grade_item_load_scale() {
$grade_item = new grade_item($this->grade_items[2], false);
$grade_item = new \grade_item($this->grade_items[2], false);
$this->assertTrue(method_exists($grade_item, 'load_scale'));
$scale = $grade_item->load_scale();
$this->assertFalse(empty($grade_item->scale));
@@ -173,13 +176,13 @@ class core_grade_item_testcase extends grade_base_testcase {
}
protected function sub_test_grade_item_load_outcome() {
$grade_item = new grade_item($this->grade_items[0], false);
$grade_item = new \grade_item($this->grade_items[0], false);
$this->assertTrue(method_exists($grade_item, 'load_outcome'));
// TODO: add tests.
}
protected function sub_test_grade_item_qualifies_for_regrading() {
$grade_item = new grade_item($this->grade_items[3], false); // Use a grade item not touched by previous tests.
$grade_item = new \grade_item($this->grade_items[3], false); // Use a grade item not touched by previous tests.
$this->assertTrue(method_exists($grade_item, 'qualifies_for_regrading'));
$this->assertFalse($grade_item->qualifies_for_regrading());
@@ -194,7 +197,7 @@ class core_grade_item_testcase extends grade_base_testcase {
}
protected function sub_test_grade_item_force_regrading() {
$grade_item = new grade_item($this->grade_items[3], false); // Use a grade item not touched by previous tests.
$grade_item = new \grade_item($this->grade_items[3], false); // Use a grade item not touched by previous tests.
$this->assertTrue(method_exists($grade_item, 'force_regrading'));
$this->assertEquals(0, $grade_item->needsupdate);
@@ -206,30 +209,30 @@ class core_grade_item_testcase extends grade_base_testcase {
}
protected function sub_test_grade_item_fetch() {
$grade_item = new grade_item();
$grade_item = new \grade_item();
$this->assertTrue(method_exists($grade_item, 'fetch'));
// Not using $this->grade_items[0] as it's iteminfo was modified by sub_test_grade_item_qualifies_for_regrading().
$grade_item = grade_item::fetch(array('id'=>$this->grade_items[1]->id));
$grade_item = \grade_item::fetch(array('id'=>$this->grade_items[1]->id));
$this->assertEquals($this->grade_items[1]->id, $grade_item->id);
$this->assertEquals($this->grade_items[1]->iteminfo, $grade_item->iteminfo);
$grade_item = grade_item::fetch(array('itemtype'=>$this->grade_items[1]->itemtype, 'itemmodule'=>$this->grade_items[1]->itemmodule));
$grade_item = \grade_item::fetch(array('itemtype'=>$this->grade_items[1]->itemtype, 'itemmodule'=>$this->grade_items[1]->itemmodule));
$this->assertEquals($this->grade_items[1]->id, $grade_item->id);
$this->assertEquals($this->grade_items[1]->iteminfo, $grade_item->iteminfo);
}
protected function sub_test_grade_item_fetch_all() {
$grade_item = new grade_item();
$grade_item = new \grade_item();
$this->assertTrue(method_exists($grade_item, 'fetch_all'));
$grade_items = grade_item::fetch_all(array('courseid'=>$this->courseid));
$grade_items = \grade_item::fetch_all(array('courseid'=>$this->courseid));
$this->assertEquals(count($this->grade_items), count($grade_items)-1); // -1 to account for the course grade item.
}
// Retrieve all final scores for a given grade_item.
protected function sub_test_grade_item_get_all_finals() {
$grade_item = new grade_item($this->grade_items[0], false);
$grade_item = new \grade_item($this->grade_items[0], false);
$this->assertTrue(method_exists($grade_item, 'get_final'));
$final_grades = $grade_item->get_final();
@@ -239,36 +242,36 @@ class core_grade_item_testcase extends grade_base_testcase {
// Retrieve all final scores for a specific userid.
protected function sub_test_grade_item_get_final() {
$grade_item = new grade_item($this->grade_items[0], false);
$grade_item = new \grade_item($this->grade_items[0], false);
$this->assertTrue(method_exists($grade_item, 'get_final'));
$final_grade = $grade_item->get_final($this->user[1]->id);
$this->assertEquals($this->grade_grades[0]->finalgrade, $final_grade->finalgrade);
}
protected function sub_test_grade_item_get_sortorder() {
$grade_item = new grade_item($this->grade_items[0], false);
$grade_item = new \grade_item($this->grade_items[0], false);
$this->assertTrue(method_exists($grade_item, 'get_sortorder'));
$sortorder = $grade_item->get_sortorder();
$this->assertEquals($this->grade_items[0]->sortorder, $sortorder);
}
protected function sub_test_grade_item_set_sortorder() {
$grade_item = new grade_item($this->grade_items[0], false);
$grade_item = new \grade_item($this->grade_items[0], false);
$this->assertTrue(method_exists($grade_item, 'set_sortorder'));
$grade_item->set_sortorder(999);
$this->assertEquals($grade_item->sortorder, 999);
}
protected function sub_test_grade_item_move_after_sortorder() {
$grade_item = new grade_item($this->grade_items[0], false);
$grade_item = new \grade_item($this->grade_items[0], false);
$this->assertTrue(method_exists($grade_item, 'move_after_sortorder'));
$grade_item->move_after_sortorder(5);
$this->assertEquals($grade_item->sortorder, 6);
$grade_item = grade_item::fetch(array('id'=>$this->grade_items[0]->id));
$grade_item = \grade_item::fetch(array('id'=>$this->grade_items[0]->id));
$this->assertEquals($grade_item->sortorder, 6);
$after = grade_item::fetch(array('id'=>$this->grade_items[6]->id));
$after = \grade_item::fetch(array('id'=>$this->grade_items[6]->id));
$this->assertEquals($after->sortorder, 8);
}
@@ -276,7 +279,7 @@ class core_grade_item_testcase extends grade_base_testcase {
* Tests the getter of the item name with escaped HTML.
*/
protected function sub_test_grade_item_get_name_escaped() {
$gradeitem = new grade_item($this->grade_items[0], false);
$gradeitem = new \grade_item($this->grade_items[0], false);
$this->assertTrue(method_exists($gradeitem, 'get_name'));
$this->assertEquals(format_string($this->grade_items[0]->itemname, true, ['escape' => true]),
$gradeitem->get_name(false, true));
@@ -286,18 +289,18 @@ class core_grade_item_testcase extends grade_base_testcase {
* Tests the getter of the item name with unescaped HTML.
*/
protected function sub_test_grade_item_get_name_unescaped() {
$gradeitem = new grade_item($this->grade_items[0], false);
$gradeitem = new \grade_item($this->grade_items[0], false);
$this->assertTrue(method_exists($gradeitem, 'get_name'));
$this->assertEquals(format_string($this->grade_items[0]->itemname, true, ['escape' => false]),
$gradeitem->get_name(false, false));
}
protected function sub_test_grade_item_set_parent() {
$grade_item = new grade_item($this->grade_items[0], false);
$grade_item = new \grade_item($this->grade_items[0], false);
$this->assertTrue(method_exists($grade_item, 'set_parent'));
$old = $grade_item->get_parent_category();
$new = new grade_category($this->grade_categories[3], false);
$new = new \grade_category($this->grade_categories[3], false);
$new_item = $new->get_grade_item();
$this->assertTrue($grade_item->set_parent($new->id));
@@ -309,7 +312,7 @@ class core_grade_item_testcase extends grade_base_testcase {
}
protected function sub_test_grade_item_get_parent_category() {
$grade_item = new grade_item($this->grade_items[0], false);
$grade_item = new \grade_item($this->grade_items[0], false);
$this->assertTrue(method_exists($grade_item, 'get_parent_category'));
$category = $grade_item->get_parent_category();
@@ -317,7 +320,7 @@ class core_grade_item_testcase extends grade_base_testcase {
}
protected function sub_test_grade_item_load_parent_category() {
$grade_item = new grade_item($this->grade_items[0], false);
$grade_item = new \grade_item($this->grade_items[0], false);
$this->assertTrue(method_exists($grade_item, 'load_parent_category'));
$category = $grade_item->load_parent_category();
@@ -326,7 +329,7 @@ class core_grade_item_testcase extends grade_base_testcase {
}
protected function sub_test_grade_item_get_item_category() {
$grade_item = new grade_item($this->grade_items[3], false);
$grade_item = new \grade_item($this->grade_items[3], false);
$this->assertTrue(method_exists($grade_item, 'get_item_category'));
$category = $grade_item->get_item_category();
@@ -334,7 +337,7 @@ class core_grade_item_testcase extends grade_base_testcase {
}
protected function sub_test_grade_item_load_item_category() {
$grade_item = new grade_item($this->grade_items[3], false);
$grade_item = new \grade_item($this->grade_items[3], false);
$this->assertTrue(method_exists($grade_item, 'load_item_category'));
$category = $grade_item->load_item_category();
@@ -343,17 +346,17 @@ class core_grade_item_testcase extends grade_base_testcase {
}
protected function sub_test_grade_item_regrade_final_grades() {
$grade_item = new grade_item($this->grade_items[0], false);
$grade_item = new \grade_item($this->grade_items[0], false);
$this->assertTrue(method_exists($grade_item, 'regrade_final_grades'));
$this->assertEquals(true, $grade_item->regrade_final_grades());
// TODO: add more tests.
}
protected function sub_test_grade_item_adjust_raw_grade() {
$grade_item = new grade_item($this->grade_items[2], false); // Anything but assignment module!
$grade_item = new \grade_item($this->grade_items[2], false); // Anything but assignment module!
$this->assertTrue(method_exists($grade_item, 'adjust_raw_grade'));
$grade_raw = new stdClass();
$grade_raw = new \stdClass();
$grade_raw->rawgrade = 40;
$grade_raw->grademax = 100;
$grade_raw->grademin = 0;
@@ -412,11 +415,11 @@ class core_grade_item_testcase extends grade_base_testcase {
protected function sub_test_grade_item_rescale_grades_keep_percentage() {
global $DB;
$gradeitem = new grade_item($this->grade_items[10], false); // 10 is the manual grade item.
$gradeitem = new \grade_item($this->grade_items[10], false); // 10 is the manual grade item.
// Create some grades to go with the grade item.
$gradeids = array();
$grade = new stdClass();
$grade = new \stdClass();
$grade->itemid = $gradeitem->id;
$grade->userid = $this->user[2]->id;
$grade->finalgrade = 10;
@@ -453,30 +456,30 @@ class core_grade_item_testcase extends grade_base_testcase {
protected function sub_test_grade_item_set_locked() {
// Getting a grade_item from the DB as set_locked() will fail if the grade items needs to be updated
// also needs to have at least one grade_grade or $grade_item->get_final(1) returns null.
// $grade_item = new grade_item($this->grade_items[8]);
$grade_item = grade_item::fetch(array('id'=>$this->grade_items[8]->id));
// $grade_item = new \grade_item($this->grade_items[8]);
$grade_item = \grade_item::fetch(array('id'=>$this->grade_items[8]->id));
$this->assertTrue(method_exists($grade_item, 'set_locked'));
$grade_grade = new grade_grade($grade_item->get_final($this->user[1]->id), false);
$grade_grade = new \grade_grade($grade_item->get_final($this->user[1]->id), false);
$this->assertTrue(empty($grade_item->locked));// Not locked.
$this->assertTrue(empty($grade_grade->locked));// Not locked.
$this->assertTrue($grade_item->set_locked(true, true, false));
$grade_grade = new grade_grade($grade_item->get_final($this->user[1]->id), false);
$grade_grade = new \grade_grade($grade_item->get_final($this->user[1]->id), false);
$this->assertFalse(empty($grade_item->locked));// Locked.
$this->assertFalse(empty($grade_grade->locked)); // Individual grades should be locked too.
$this->assertTrue($grade_item->set_locked(false, true, false));
$grade = new grade_grade($grade_item->get_final($this->user[1]->id), false);
$grade = new \grade_grade($grade_item->get_final($this->user[1]->id), false);
$this->assertTrue(empty($grade_item->locked));
$this->assertTrue(empty($grade->locked)); // Individual grades should be unlocked too.
}
protected function sub_test_grade_item_is_locked() {
$grade_item = new grade_item($this->grade_items[10], false);
$grade_item = new \grade_item($this->grade_items[10], false);
$this->assertTrue(method_exists($grade_item, 'is_locked'));
$this->assertFalse($grade_item->is_locked());
@@ -487,22 +490,22 @@ class core_grade_item_testcase extends grade_base_testcase {
}
protected function sub_test_grade_item_set_hidden() {
$grade_item = new grade_item($this->grade_items[0], false);
$grade_item = new \grade_item($this->grade_items[0], false);
$this->assertTrue(method_exists($grade_item, 'set_hidden'));
$grade = new grade_grade($grade_item->get_final($this->user[1]->id), false);
$grade = new \grade_grade($grade_item->get_final($this->user[1]->id), false);
$this->assertEquals(0, $grade_item->hidden);
$this->assertEquals(0, $grade->hidden);
$grade_item->set_hidden(666, true);
$grade = new grade_grade($grade_item->get_final($this->user[1]->id), false);
$grade = new \grade_grade($grade_item->get_final($this->user[1]->id), false);
$this->assertEquals(666, $grade_item->hidden);
$this->assertEquals(666, $grade->hidden);
}
protected function sub_test_grade_item_is_hidden() {
$grade_item = new grade_item($this->grade_items[0], false);
$grade_item = new \grade_item($this->grade_items[0], false);
$this->assertTrue(method_exists($grade_item, 'is_hidden'));
$this->assertFalse($grade_item->is_hidden());
@@ -522,19 +525,19 @@ class core_grade_item_testcase extends grade_base_testcase {
}
protected function sub_test_grade_item_is_category_item() {
$grade_item = new grade_item($this->grade_items[3], false);
$grade_item = new \grade_item($this->grade_items[3], false);
$this->assertTrue(method_exists($grade_item, 'is_category_item'));
$this->assertTrue($grade_item->is_category_item());
}
protected function sub_test_grade_item_is_course_item() {
$grade_item = grade_item::fetch_course_item($this->courseid);
$grade_item = \grade_item::fetch_course_item($this->courseid);
$this->assertTrue(method_exists($grade_item, 'is_course_item'));
$this->assertTrue($grade_item->is_course_item());
}
protected function sub_test_grade_item_fetch_course_item() {
$grade_item = grade_item::fetch_course_item($this->courseid);
$grade_item = \grade_item::fetch_course_item($this->courseid);
$this->assertTrue(method_exists($grade_item, 'fetch_course_item'));
$this->assertEquals($grade_item->itemtype, 'course');
}
@@ -544,7 +547,7 @@ class core_grade_item_testcase extends grade_base_testcase {
$origenableoutcomes = $CFG->enableoutcomes;
$CFG->enableoutcomes = 0;
$grade_item = new grade_item($this->grade_items[1], false);
$grade_item = new \grade_item($this->grade_items[1], false);
// Calculated grade dependency.
$deps = $grade_item->depends_on();
@@ -559,7 +562,7 @@ class core_grade_item_testcase extends grade_base_testcase {
$this->assertEquals(array(), $deps);
// Category dependency.
$grade_item = new grade_item($this->grade_items[3], false);
$grade_item = new \grade_item($this->grade_items[3], false);
$deps = $grade_item->depends_on();
sort($deps, SORT_NUMERIC); // For comparison.
$res = array($this->grade_items[4]->id, $this->grade_items[5]->id);
@@ -572,7 +575,7 @@ class core_grade_item_testcase extends grade_base_testcase {
$CFG->grade_includescalesinaggregation = 1;
// Scale item in category with $CFG->grade_includescalesinaggregation = 1.
$grade_item = new grade_item($this->grade_items[14], false);
$grade_item = new \grade_item($this->grade_items[14], false);
$deps = $grade_item->depends_on();
sort($deps, SORT_NUMERIC);
$res = array($this->grade_items[16]->id);
@@ -580,7 +583,7 @@ class core_grade_item_testcase extends grade_base_testcase {
// Scale item in category with $CFG->grade_includescalesinaggregation = 0.
$CFG->grade_includescalesinaggregation = 0;
$grade_item = new grade_item($this->grade_items[14], false);
$grade_item = new \grade_item($this->grade_items[14], false);
$deps = $grade_item->depends_on();
$res = array();
$this->assertEquals($res, $deps);
@@ -588,7 +591,7 @@ class core_grade_item_testcase extends grade_base_testcase {
// Outcome item in category with outcomes disabled.
$CFG->enableoutcomes = 0;
$grade_item = new grade_item($this->grade_items[14], false);
$grade_item = new \grade_item($this->grade_items[14], false);
$deps = $grade_item->depends_on();
sort($deps, SORT_NUMERIC);
$res = array($this->grade_items[16]->id, $this->grade_items[17]->id);
@@ -600,7 +603,7 @@ class core_grade_item_testcase extends grade_base_testcase {
protected function sub_test_refresh_grades() {
// Testing with the grade item for a mod_assignment instance.
$grade_item = new grade_item($this->grade_items[0], false);
$grade_item = new \grade_item($this->grade_items[0], false);
$this->assertTrue(method_exists($grade_item, 'refresh_grades'));
$this->assertTrue($grade_item->refresh_grades());
@@ -611,18 +614,18 @@ class core_grade_item_testcase extends grade_base_testcase {
}
protected function sub_test_grade_item_is_calculated() {
$grade_item = new grade_item($this->grade_items[1], false);
$grade_item = new \grade_item($this->grade_items[1], false);
$this->assertTrue(method_exists($grade_item, 'is_calculated'));
$this->assertTrue($grade_item->is_calculated());
$grade_item = new grade_item($this->grade_items[0], false);
$grade_item = new \grade_item($this->grade_items[0], false);
$this->assertFalse($grade_item->is_calculated());
}
protected function sub_test_grade_item_set_calculation() {
$grade_item = new grade_item($this->grade_items[1], false);
$grade_item = new \grade_item($this->grade_items[1], false);
$this->assertTrue(method_exists($grade_item, 'set_calculation'));
$grade_itemsource = new grade_item($this->grade_items[0], false);
$grade_itemsource = new \grade_item($this->grade_items[0], false);
$grade_item->set_calculation('=[['.$grade_itemsource->idnumber.']]');
@@ -631,9 +634,9 @@ class core_grade_item_testcase extends grade_base_testcase {
}
protected function sub_test_grade_item_get_calculation() {
$grade_item = new grade_item($this->grade_items[1], false);
$grade_item = new \grade_item($this->grade_items[1], false);
$this->assertTrue(method_exists($grade_item, 'get_calculation'));
$grade_itemsource = new grade_item($this->grade_items[0], false);
$grade_itemsource = new \grade_item($this->grade_items[0], false);
$denormalizedformula = str_replace('##gi'.$grade_itemsource->id.'##', '[['.$grade_itemsource->idnumber.']]', $this->grade_items[1]->calculation);
@@ -643,32 +646,32 @@ class core_grade_item_testcase extends grade_base_testcase {
}
public function sub_test_grade_item_compute() {
$grade_item = grade_item::fetch(array('id'=>$this->grade_items[1]->id));
$grade_item = \grade_item::fetch(array('id'=>$this->grade_items[1]->id));
$this->assertTrue(method_exists($grade_item, 'compute'));
// Check the grade_grades in the array match those in the DB then delete $this->grade_items[1]'s grade_grades.
$this->grade_grades[3] = grade_grade::fetch(array('id'=>$this->grade_grades[3]->id));
$grade_grade = grade_grade::fetch(array('id'=>$this->grade_grades[3]->id));
$this->grade_grades[3] = \grade_grade::fetch(array('id'=>$this->grade_grades[3]->id));
$grade_grade = \grade_grade::fetch(array('id'=>$this->grade_grades[3]->id));
$grade_grade->delete();
$this->grade_grades[4] = grade_grade::fetch(array('id'=>$this->grade_grades[4]->id));
$grade_grade = grade_grade::fetch(array('id'=>$this->grade_grades[4]->id));
$this->grade_grades[4] = \grade_grade::fetch(array('id'=>$this->grade_grades[4]->id));
$grade_grade = \grade_grade::fetch(array('id'=>$this->grade_grades[4]->id));
$grade_grade->delete();
$this->grade_grades[5] = grade_grade::fetch(array('id'=>$this->grade_grades[5]->id));
$grade_grade = grade_grade::fetch(array('id'=>$this->grade_grades[5]->id));
$this->grade_grades[5] = \grade_grade::fetch(array('id'=>$this->grade_grades[5]->id));
$grade_grade = \grade_grade::fetch(array('id'=>$this->grade_grades[5]->id));
$grade_grade->delete();
// Recalculate the grades (its a calculation so pulls values from other grade_items) and reinsert them.
$grade_item->compute();
$grade_grade = grade_grade::fetch(array('userid'=>$this->grade_grades[3]->userid, 'itemid'=>$this->grade_grades[3]->itemid));
$grade_grade = \grade_grade::fetch(array('userid'=>$this->grade_grades[3]->userid, 'itemid'=>$this->grade_grades[3]->itemid));
$this->assertEquals($this->grade_grades[3]->finalgrade, $grade_grade->finalgrade);
$grade_grade = grade_grade::fetch(array('userid'=>$this->grade_grades[4]->userid, 'itemid'=>$this->grade_grades[4]->itemid));
$grade_grade = \grade_grade::fetch(array('userid'=>$this->grade_grades[4]->userid, 'itemid'=>$this->grade_grades[4]->itemid));
$this->assertEquals($this->grade_grades[4]->finalgrade, $grade_grade->finalgrade);
$grade_grade = grade_grade::fetch(array('userid'=>$this->grade_grades[5]->userid, 'itemid'=>$this->grade_grades[5]->itemid));
$grade_grade = \grade_grade::fetch(array('userid'=>$this->grade_grades[5]->userid, 'itemid'=>$this->grade_grades[5]->itemid));
$this->assertEquals($this->grade_grades[5]->finalgrade, $grade_grade->finalgrade);
}
@@ -680,7 +683,7 @@ class core_grade_item_testcase extends grade_base_testcase {
$max = 8;
// Create a brand new grade item.
$grade_item = new grade_item();
$grade_item = new \grade_item();
$this->assertTrue(method_exists($grade_item, 'insert'));
$grade_item->courseid = $this->courseid;
@@ -698,23 +701,23 @@ class core_grade_item_testcase extends grade_base_testcase {
$grade_item->update_final_grade($this->user[1]->id, 7, 'gradebook', '', FORMAT_MOODLE);
// Check the student's grade has the correct min and max grade.
$grade_grade = grade_grade::fetch(array('userid'=>$this->user[1]->id, 'itemid'=>$grade_item->id));
$grade_grade = \grade_grade::fetch(array('userid'=>$this->user[1]->id, 'itemid'=>$grade_item->id));
$this->assertEquals($min, $grade_grade->rawgrademin);
$this->assertEquals($max, $grade_grade->rawgrademax);
}
protected function sub_test_grade_item_can_control_visibility() {
// Grade item 0 == Course module 0 == Assignment.
$grade_item = new grade_item($this->grade_items[0], false);
$grade_item = new \grade_item($this->grade_items[0], false);
$this->assertTrue($grade_item->can_control_visibility());
// Grade item == Course module 7 == Quiz.
$grade_item = new grade_item($this->grade_items[11], false);
$grade_item = new \grade_item($this->grade_items[11], false);
$this->assertFalse($grade_item->can_control_visibility());
}
/**
* Test the {@link grade_item::fix_duplicate_sortorder() function with
* Test the {@link \grade_item::fix_duplicate_sortorder() function with
* faked duplicate sortorder data.
*/
public function sub_test_grade_item_fix_sortorder() {
@@ -760,7 +763,7 @@ class core_grade_item_testcase extends grade_base_testcase {
// Do the work.
foreach ($origsequence as $courseid => $ignore) {
grade_item::fix_duplicate_sortorder($courseid);
\grade_item::fix_duplicate_sortorder($courseid);
// Verify that no duplicates are left in the database.
$dupes = $DB->record_exists_sql($duplicatedetectionsql, array('courseid' => $courseid));
$this->assertFalse($dupes);
@@ -799,7 +802,7 @@ class core_grade_item_testcase extends grade_base_testcase {
global $DB, $CFG;
require_once($CFG->libdir.'/gradelib.php');
$item = new stdClass();
$item = new \stdClass();
$item->courseid = $courseid;
$item->sortorder = $sortorder;
$item->gradetype = GRADE_TYPE_VALUE;
@@ -817,7 +820,7 @@ class core_grade_item_testcase extends grade_base_testcase {
public function test_set_aggregation_fields_for_aggregation() {
$course = $this->getDataGenerator()->create_course();
$gi = new grade_item(array('courseid' => $course->id, 'itemtype' => 'manual'), false);
$gi = new \grade_item(array('courseid' => $course->id, 'itemtype' => 'manual'), false);
$methods = array(GRADE_AGGREGATE_MEAN, GRADE_AGGREGATE_MEDIAN, GRADE_AGGREGATE_MIN, GRADE_AGGREGATE_MAX,
GRADE_AGGREGATE_MODE, GRADE_AGGREGATE_WEIGHTED_MEAN, GRADE_AGGREGATE_WEIGHTED_MEAN2,
@@ -825,7 +828,7 @@ class core_grade_item_testcase extends grade_base_testcase {
// Switching from and to the same aggregation using the defaults.
foreach ($methods as $method) {
$defaults = grade_category::get_default_aggregation_coefficient_values($method);
$defaults = \grade_category::get_default_aggregation_coefficient_values($method);
$gi->aggregationcoef = $defaults['aggregationcoef'];
$gi->aggregationcoef2 = $defaults['aggregationcoef2'];
$gi->weightoverride = $defaults['weightoverride'];
@@ -837,12 +840,12 @@ class core_grade_item_testcase extends grade_base_testcase {
// Extra credit is kept across aggregation methods that support it.
foreach ($methods as $from) {
$fromsupportsec = grade_category::aggregation_uses_extracredit($from);
$fromdefaults = grade_category::get_default_aggregation_coefficient_values($from);
$fromsupportsec = \grade_category::aggregation_uses_extracredit($from);
$fromdefaults = \grade_category::get_default_aggregation_coefficient_values($from);
foreach ($methods as $to) {
$tosupportsec = grade_category::aggregation_uses_extracredit($to);
$todefaults = grade_category::get_default_aggregation_coefficient_values($to);
$tosupportsec = \grade_category::aggregation_uses_extracredit($to);
$todefaults = \grade_category::get_default_aggregation_coefficient_values($to);
// Set the item to be extra credit, if supported.
if ($fromsupportsec) {
@@ -884,14 +887,14 @@ class core_grade_item_testcase extends grade_base_testcase {
// Extra credit can be higher than one for GRADE_AGGREGATE_EXTRACREDIT_MEAN, but will be normalised for others.
$from = GRADE_AGGREGATE_EXTRACREDIT_MEAN;
$fromdefaults = grade_category::get_default_aggregation_coefficient_values($from);
$fromdefaults = \grade_category::get_default_aggregation_coefficient_values($from);
foreach ($methods as $to) {
if (!grade_category::aggregation_uses_extracredit($to)) {
if (!\grade_category::aggregation_uses_extracredit($to)) {
continue;
}
$todefaults = grade_category::get_default_aggregation_coefficient_values($to);
$todefaults = \grade_category::get_default_aggregation_coefficient_values($to);
$gi->aggregationcoef = 8;
// Ignore those fields, they are not used for extra credit.
@@ -909,14 +912,14 @@ class core_grade_item_testcase extends grade_base_testcase {
// Weights are reset.
$from = GRADE_AGGREGATE_SUM;
$fromdefaults = grade_category::get_default_aggregation_coefficient_values($from);
$fromdefaults = \grade_category::get_default_aggregation_coefficient_values($from);
$gi->aggregationcoef = $fromdefaults['aggregationcoef'];
$gi->aggregationcoef2 = 0.321;
$gi->weightoverride = $fromdefaults['weightoverride'];
$to = GRADE_AGGREGATE_WEIGHTED_MEAN;
$todefaults = grade_category::get_default_aggregation_coefficient_values($to);
$todefaults = \grade_category::get_default_aggregation_coefficient_values($to);
$this->assertTrue($gi->set_aggregation_fields_for_aggregation($from, $to), "From: $from, to: $to");
$this->assertEquals($todefaults['aggregationcoef'], $gi->aggregationcoef);
@@ -928,7 +931,7 @@ class core_grade_item_testcase extends grade_base_testcase {
$gi->weightoverride = $fromdefaults['weightoverride'];
$to = GRADE_AGGREGATE_SUM;
$todefaults = grade_category::get_default_aggregation_coefficient_values($to);
$todefaults = \grade_category::get_default_aggregation_coefficient_values($to);
$this->assertTrue($gi->set_aggregation_fields_for_aggregation($from, $to), "From: $from, to: $to");
$this->assertEquals($todefaults['aggregationcoef'], $gi->aggregationcoef);
@@ -937,14 +940,14 @@ class core_grade_item_testcase extends grade_base_testcase {
// Weight is kept when using SUM with weight override.
$from = GRADE_AGGREGATE_SUM;
$fromdefaults = grade_category::get_default_aggregation_coefficient_values($from);
$fromdefaults = \grade_category::get_default_aggregation_coefficient_values($from);
$gi->aggregationcoef = $fromdefaults['aggregationcoef'];
$gi->aggregationcoef2 = 0.321;
$gi->weightoverride = 1;
$to = GRADE_AGGREGATE_SUM;
$todefaults = grade_category::get_default_aggregation_coefficient_values($to);
$todefaults = \grade_category::get_default_aggregation_coefficient_values($to);
$this->assertFalse($gi->set_aggregation_fields_for_aggregation($from, $to), "From: $from, to: $to");
$this->assertEquals($todefaults['aggregationcoef'], $gi->aggregationcoef);
@@ -956,7 +959,7 @@ class core_grade_item_testcase extends grade_base_testcase {
$gi->weightoverride = 1;
$to = GRADE_AGGREGATE_WEIGHTED_MEAN;
$todefaults = grade_category::get_default_aggregation_coefficient_values($to);
$todefaults = \grade_category::get_default_aggregation_coefficient_values($to);
$this->assertTrue($gi->set_aggregation_fields_for_aggregation($from, $to), "From: $from, to: $to");
$this->assertEquals($todefaults['aggregationcoef'], $gi->aggregationcoef);
@@ -965,14 +968,14 @@ class core_grade_item_testcase extends grade_base_testcase {
// Weight is kept when staying in weighted mean.
$from = GRADE_AGGREGATE_WEIGHTED_MEAN;
$fromdefaults = grade_category::get_default_aggregation_coefficient_values($from);
$fromdefaults = \grade_category::get_default_aggregation_coefficient_values($from);
$gi->aggregationcoef = 18;
$gi->aggregationcoef2 = $fromdefaults['aggregationcoef2'];
$gi->weightoverride = $fromdefaults['weightoverride'];
$to = GRADE_AGGREGATE_WEIGHTED_MEAN;
$todefaults = grade_category::get_default_aggregation_coefficient_values($to);
$todefaults = \grade_category::get_default_aggregation_coefficient_values($to);
$this->assertFalse($gi->set_aggregation_fields_for_aggregation($from, $to), "From: $from, to: $to");
$this->assertEquals(18, $gi->aggregationcoef);
@@ -984,7 +987,7 @@ class core_grade_item_testcase extends grade_base_testcase {
$gi->weightoverride = $fromdefaults['weightoverride'];
$to = GRADE_AGGREGATE_SUM;
$todefaults = grade_category::get_default_aggregation_coefficient_values($to);
$todefaults = \grade_category::get_default_aggregation_coefficient_values($to);
$this->assertTrue($gi->set_aggregation_fields_for_aggregation($from, $to), "From: $from, to: $to");
$this->assertEquals($todefaults['aggregationcoef'], $gi->aggregationcoef);
@@ -998,7 +1001,7 @@ class core_grade_item_testcase extends grade_base_testcase {
protected function sub_test_grade_item_created_event() {
$sink = $this->redirectEvents();
$gradeitem = new grade_item();
$gradeitem = new \grade_item();
$gradeitem->courseid = $this->courseid;
$gradeitem->categoryid = $this->grade_categories[1]->id;
@@ -1031,7 +1034,7 @@ class core_grade_item_testcase extends grade_base_testcase {
* Test that grade item event triggered when a grade item is updated.
*/
protected function sub_test_grade_item_updated_event() {
$gradeitem = new grade_item();
$gradeitem = new \grade_item();
$gradeitem->courseid = $this->courseid;
$gradeitem->categoryid = $this->grade_categories[1]->id;
@@ -1069,13 +1072,13 @@ class core_grade_item_testcase extends grade_base_testcase {
* Test grade item duplication expecting success.
*/
public function test_grade_duplicate_grade_item_success() {
$cat = new grade_category();
$cat = new \grade_category();
$cat->courseid = $this->courseid;
$cat->fullname = 'Grade category';
$cat->insert();
// Method exists.
$gi = new grade_item();
$gi = new \grade_item();
$this->assertTrue(method_exists($gi, 'duplicate'));
// Grade item is inserted and valid for duplication.
@@ -1110,7 +1113,7 @@ class core_grade_item_testcase extends grade_base_testcase {
*/
public function test_grade_duplicate_grade_item_incomplete() {
// Grade item is not valid because it is empty.
$gi = new grade_item();
$gi = new \grade_item();
$gi->courseid = $this->courseid;
$this->expectException("moodle_exception");
$gi2 = $gi->duplicate();
@@ -1120,14 +1123,14 @@ class core_grade_item_testcase extends grade_base_testcase {
* Test grade item duplication exception expected because item must be in db.
*/
public function test_grade_duplicate_grade_item_not_in_db() {
$cat = new grade_category();
$cat = new \grade_category();
$cat->courseid = $this->courseid;
$cat->fullname = 'Grade category';
$cat->insert();
// Grade item is valid for insertion but is not inserted into db.
// Duplicate method throws an exception.
$gi = new grade_item();
$gi = new \grade_item();
$gi->courseid = $this->courseid;
$gi->categoryid = $cat->id;
$gi->itemtype = 'manual';
+8 -15
View File
@@ -14,14 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Grade object tests.
*
* @package core_grades
* @category phpunit
* @copyright 2014 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
defined('MOODLE_INTERNAL') || die();
@@ -30,17 +23,17 @@ require_once(__DIR__ . '/fixtures/lib.php');
/**
* Grade object testcase.
*
* @package core_grades
* @category phpunit
* @package core
* @category test
* @copyright 2014 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_grade_object_testcase extends grade_base_testcase {
class grade_object_test extends \grade_base_testcase {
public function test_fetch_all_helper() {
// Simple ID lookup.
$params = array('id' => $this->grade_items[0]->id);
$items = grade_object::fetch_all_helper('grade_items', 'grade_item', $params);
$items = \grade_object::fetch_all_helper('grade_items', 'grade_item', $params);
$this->assertCount(1, $items);
$item = array_shift($items);
$this->assertInstanceOf('grade_item', $item);
@@ -48,7 +41,7 @@ class core_grade_object_testcase extends grade_base_testcase {
// Various parameters lookup, multiple results.
$params = array('courseid' => $this->course->id, 'categoryid' => $this->grade_categories[1]->id);
$items = grade_object::fetch_all_helper('grade_items', 'grade_item', $params);
$items = \grade_object::fetch_all_helper('grade_items', 'grade_item', $params);
$this->assertCount(2, $items);
$expecteditems = array($this->grade_items[0]->id => true, $this->grade_items[1]->id => true);
foreach ($items as $item) {
@@ -59,7 +52,7 @@ class core_grade_object_testcase extends grade_base_testcase {
// Text column lookup.
$params = array('iteminfo' => $this->grade_items[2]->iteminfo);
$items = grade_object::fetch_all_helper('grade_items', 'grade_item', $params);
$items = \grade_object::fetch_all_helper('grade_items', 'grade_item', $params);
$this->assertCount(1, $items);
$item = array_shift($items);
$this->assertInstanceOf('grade_item', $item);
@@ -67,7 +60,7 @@ class core_grade_object_testcase extends grade_base_testcase {
// Lookup using non-existing columns.
$params = array('doesnotexist' => 'ignoreme', 'id' => $this->grade_items[0]->id);
$items = grade_object::fetch_all_helper('grade_items', 'grade_item', $params);
$items = \grade_object::fetch_all_helper('grade_items', 'grade_item', $params);
$this->assertCount(1, $items);
$item = array_shift($items);
$this->assertInstanceOf('grade_item', $item);
+19 -17
View File
@@ -14,19 +14,21 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package core_grades
* @category phpunit
* @copyright [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/fixtures/lib.php');
class core_grade_outcome_testcase extends grade_base_testcase {
/**
* Unit tests for grade_outcome
*
* @package core
* @category test
* @copyright [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class grade_outcome_test extends \grade_base_testcase {
public function test_grade_outcome() {
$this->sub_test_grade_outcome_construct();
@@ -38,18 +40,18 @@ class core_grade_outcome_testcase extends grade_base_testcase {
}
protected function sub_test_grade_outcome_construct() {
$params = new stdClass();
$params = new \stdClass();
$params->courseid = $this->courseid;
$params->shortname = 'Team work';
$grade_outcome = new grade_outcome($params, false);
$grade_outcome = new \grade_outcome($params, false);
$this->assertEquals($params->courseid, $grade_outcome->courseid);
$this->assertEquals($params->shortname, $grade_outcome->shortname);
}
protected function sub_test_grade_outcome_insert() {
$grade_outcome = new grade_outcome();
$grade_outcome = new \grade_outcome();
$this->assertTrue(method_exists($grade_outcome, 'insert'));
$grade_outcome->courseid = $this->courseid;
@@ -67,7 +69,7 @@ class core_grade_outcome_testcase extends grade_base_testcase {
protected function sub_test_grade_outcome_update() {
global $DB;
$grade_outcome = new grade_outcome($this->grade_outcomes[0], false);
$grade_outcome = new \grade_outcome($this->grade_outcomes[0], false);
$this->assertTrue(method_exists($grade_outcome, 'update'));
$grade_outcome->shortname = 'Team work';
$this->assertTrue($grade_outcome->update());
@@ -77,7 +79,7 @@ class core_grade_outcome_testcase extends grade_base_testcase {
protected function sub_test_grade_outcome_delete() {
global $DB;
$grade_outcome = new grade_outcome($this->grade_outcomes[0], false);
$grade_outcome = new \grade_outcome($this->grade_outcomes[0], false);
$this->assertTrue(method_exists($grade_outcome, 'delete'));
$this->assertTrue($grade_outcome->delete());
@@ -85,10 +87,10 @@ class core_grade_outcome_testcase extends grade_base_testcase {
}
protected function sub_test_grade_outcome_fetch() {
$grade_outcome = new grade_outcome();
$grade_outcome = new \grade_outcome();
$this->assertTrue(method_exists($grade_outcome, 'fetch'));
$grade_outcome = grade_outcome::fetch(array('id'=>$this->grade_outcomes[0]->id));
$grade_outcome = \grade_outcome::fetch(array('id'=>$this->grade_outcomes[0]->id));
$grade_outcome->load_scale();
$this->assertEquals($this->grade_outcomes[0]->id, $grade_outcome->id);
$this->assertEquals($this->grade_outcomes[0]->shortname, $grade_outcome->shortname);
@@ -97,10 +99,10 @@ class core_grade_outcome_testcase extends grade_base_testcase {
}
protected function sub_test_grade_outcome_fetch_all() {
$grade_outcome = new grade_outcome();
$grade_outcome = new \grade_outcome();
$this->assertTrue(method_exists($grade_outcome, 'fetch_all'));
$grade_outcomes = grade_outcome::fetch_all(array());
$grade_outcomes = \grade_outcome::fetch_all(array());
$this->assertEquals(count($this->grade_outcomes), count($grade_outcomes));
}
}
+23 -21
View File
@@ -14,19 +14,21 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package core_grades
* @category phpunit
* @copyright [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/fixtures/lib.php');
class core_grade_scale_testcase extends grade_base_testcase {
/**
* Unit tests for grade_scale
*
* @package core
* @category test
* @copyright [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class grade_scale_test extends \grade_base_testcase {
public function test_grade_scale() {
$this->sub_test_scale_construct();
@@ -40,7 +42,7 @@ class core_grade_scale_testcase extends grade_base_testcase {
}
protected function sub_test_scale_construct() {
$params = new stdClass();
$params = new \stdClass();
$params->name = 'unittestscale3';
$params->courseid = $this->course->id;
$params->userid = $this->userid;
@@ -48,7 +50,7 @@ class core_grade_scale_testcase extends grade_base_testcase {
$params->description = 'This scale is used to mark standard assignments.';
$params->timemodified = time();
$scale = new grade_scale($params, false);
$scale = new \grade_scale($params, false);
$this->assertEquals($params->name, $scale->name);
$this->assertEquals($params->scale, $scale->scale);
@@ -57,7 +59,7 @@ class core_grade_scale_testcase extends grade_base_testcase {
}
protected function sub_test_grade_scale_insert() {
$grade_scale = new grade_scale();
$grade_scale = new \grade_scale();
$this->assertTrue(method_exists($grade_scale, 'insert'));
$grade_scale->name = 'unittestscale3';
@@ -77,7 +79,7 @@ class core_grade_scale_testcase extends grade_base_testcase {
protected function sub_test_grade_scale_update() {
global $DB;
$grade_scale = new grade_scale($this->scale[1], false);
$grade_scale = new \grade_scale($this->scale[1], false);
$this->assertTrue(method_exists($grade_scale, 'update'));
$grade_scale->name = 'Updated info for this unittest grade_scale';
@@ -88,7 +90,7 @@ class core_grade_scale_testcase extends grade_base_testcase {
protected function sub_test_grade_scale_delete() {
global $DB;
$grade_scale = new grade_scale($this->scale[4], false); // Choose one we're not using elsewhere.
$grade_scale = new \grade_scale($this->scale[4], false); // Choose one we're not using elsewhere.
$this->assertTrue(method_exists($grade_scale, 'delete'));
$this->assertTrue($grade_scale->delete());
@@ -99,16 +101,16 @@ class core_grade_scale_testcase extends grade_base_testcase {
}
protected function sub_test_grade_scale_fetch() {
$grade_scale = new grade_scale();
$grade_scale = new \grade_scale();
$this->assertTrue(method_exists($grade_scale, 'fetch'));
$grade_scale = grade_scale::fetch(array('id'=>$this->scale[0]->id));
$grade_scale = \grade_scale::fetch(array('id'=>$this->scale[0]->id));
$this->assertEquals($this->scale[0]->id, $grade_scale->id);
$this->assertEquals($this->scale[0]->name, $grade_scale->name);
}
protected function sub_test_scale_load_items() {
$scale = new grade_scale($this->scale[0], false);
$scale = new \grade_scale($this->scale[0], false);
$this->assertTrue(method_exists($scale, 'load_items'));
$scale->load_items();
@@ -118,7 +120,7 @@ class core_grade_scale_testcase extends grade_base_testcase {
}
protected function sub_test_scale_compact_items() {
$scale = new grade_scale($this->scale[0], false);
$scale = new \grade_scale($this->scale[0], false);
$this->assertTrue(method_exists($scale, 'compact_items'));
$scale->load_items();
@@ -130,7 +132,7 @@ class core_grade_scale_testcase extends grade_base_testcase {
}
protected function sub_test_scale_one_item() {
$params = new stdClass();
$params = new \stdClass();
$params->name = 'unittestscale1i';
$params->courseid = $this->course->id;
$params->userid = $this->userid;
@@ -138,7 +140,7 @@ class core_grade_scale_testcase extends grade_base_testcase {
$params->description = 'This scale is used to like something.';
$params->timemodified = time();
$scale = new grade_scale($params, false);
$scale = new \grade_scale($params, false);
$scale->load_items();
$this->assertCount(1, $scale->scale_items);
@@ -148,7 +150,7 @@ class core_grade_scale_testcase extends grade_base_testcase {
$scale->insert();
// Manual grade item with 1 item scale.
$grade_item = new stdClass();
$grade_item = new \stdClass();
$grade_item->courseid = $this->course->id;
$grade_item->categoryid = $this->grade_categories[0]->id;
$grade_item->itemname = 'manual grade_item scale_1';
@@ -161,7 +163,7 @@ class core_grade_scale_testcase extends grade_base_testcase {
$grade_item->timecreated = time();
$grade_item->timemodified = time();
$grade_item = new grade_item($grade_item);
$grade_item = new \grade_item($grade_item);
$grade_item->insert();
$this->assertNotEmpty($grade_item->id);
+23 -33
View File
@@ -14,27 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* PHPUnit integration tests
*
* @package core
* @category phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
namespace core;
/**
* Test advanced_testcase extra features.
*
* @package core
* @category phpunit
* @category test
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_phpunit_advanced_testcase extends advanced_testcase {
class advanced_test extends \advanced_testcase {
public function test_debugging() {
global $CFG;
@@ -222,7 +212,7 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
$DB->set_field('user', 'confirmed', 0, array('id'=>2));
try {
self::resetAllData(true);
} catch (Exception $e) {
} catch (\Exception $e) {
$this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
}
$this->assertEquals(1, $DB->get_field('user', 'confirmed', array('id'=>2)));
@@ -233,7 +223,7 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
$CFG->rolesactive = 0;
try {
self::resetAllData(true);
} catch (Exception $e) {
} catch (\Exception $e) {
$this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
$this->assertStringContainsString('xx', $e->getMessage());
$this->assertStringContainsString('admin', $e->getMessage());
@@ -271,11 +261,11 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
// COURSE change.
$SITE->id = 10;
$COURSE = new stdClass();
$COURSE = new \stdClass();
$COURSE->id = 7;
try {
self::resetAllData(true);
} catch (Exception $e) {
} catch (\Exception $e) {
$this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
$this->assertEquals(1, $SITE->id);
$this->assertSame($SITE, $COURSE);
@@ -286,7 +276,7 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
$this->setUser(2);
try {
self::resetAllData(true);
} catch (Exception $e) {
} catch (\Exception $e) {
$this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
$this->assertEquals(0, $USER->id);
}
@@ -303,7 +293,7 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
try {
$DB->get_record('pokus', array());
$this->fail('Exception expected when accessing non existent table');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('dml_exception', $e);
}
$DB = $this->createMock(get_class($DB));
@@ -403,7 +393,7 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
$this->setCurrentTimeStart();
$this->assertTimeCurrent(time()+10);
$this->fail('Failed assert expected');
} catch (Exception $e) {
} catch (\Exception $e) {
$this->assertInstanceOf('PHPUnit\Framework\ExpectationFailedException', $e);
}
@@ -411,7 +401,7 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
$this->setCurrentTimeStart();
$this->assertTimeCurrent(time()-10);
$this->fail('Failed assert expected');
} catch (Exception $e) {
} catch (\Exception $e) {
$this->assertInstanceOf('PHPUnit\Framework\ExpectationFailedException', $e);
}
}
@@ -425,7 +415,7 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
$processors1 = get_message_processors();
// Add a new message processor and get all processors again.
$processor = new stdClass();
$processor = new \stdClass();
$processor->name = 'test_processor';
$processor->enabled = 1;
$DB->insert_record('message_processors', $processor);
@@ -558,7 +548,7 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
$this->assertEquals(1, $sink->count());
// Test if sink can be carried over to next test.
$this->assertTrue(phpunit_util::is_redirecting_messages());
$this->assertTrue(\phpunit_util::is_redirecting_messages());
return $sink;
}
@@ -573,7 +563,7 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
$this->preventResetByRollback(); // Messaging is not compatible with transactions...
$this->resetAfterTest();
$this->assertTrue(phpunit_util::is_redirecting_messages());
$this->assertTrue(\phpunit_util::is_redirecting_messages());
$this->assertEquals(1, $sink->count());
$message = new \core\message\message();
@@ -597,7 +587,7 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
* @depends test_message_redirection_noreset
*/
public function test_message_redirection_reset() {
$this->assertFalse(phpunit_util::is_redirecting_messages(), 'Test reset must stop message redirection.');
$this->assertFalse(\phpunit_util::is_redirecting_messages(), 'Test reset must stop message redirection.');
}
public function test_set_timezone() {
@@ -625,19 +615,19 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
try {
$this->setTimezone('Pacific/Auckland', '');
} catch (Exception $e) {
} catch (\Exception $e) {
$this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
}
try {
$this->setTimezone('Pacific/Auckland', 'xxxx');
} catch (Exception $e) {
} catch (\Exception $e) {
$this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
}
try {
$this->setTimezone('Pacific/Auckland', null);
} catch (Exception $e) {
} catch (\Exception $e) {
$this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
}
@@ -662,7 +652,7 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
try {
self::resetAllData(true);
} catch (Exception $e) {
} catch (\Exception $e) {
$this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
}
@@ -697,14 +687,14 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
$fakeagent = 'New user agent set.';
// Sanity check: it should not be set when test begins.
self::assertFalse(core_useragent::get_user_agent_string(), 'It should not be set at first.');
self::assertFalse(\core_useragent::get_user_agent_string(), 'It should not be set at first.');
// Set a fake useragent and check it was set properly.
core_useragent::instance(true, $fakeagent);
self::assertSame($fakeagent, core_useragent::get_user_agent_string(), 'It should be the forced agent.');
\core_useragent::instance(true, $fakeagent);
self::assertSame($fakeagent, \core_useragent::get_user_agent_string(), 'It should be the forced agent.');
// Reset test data and ansure the useragent was cleaned.
self::resetAllData(false);
self::assertFalse(core_useragent::get_user_agent_string(), 'It should not be set again, data was reset.');
self::assertFalse(\core_useragent::get_user_agent_string(), 'It should not be set again, data was reset.');
}
}
+7 -17
View File
@@ -14,27 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* PHPUnit integration tests
*
* @package core
* @category phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
namespace core;
/**
* Test basic_testcase extra features and PHPUnit Moodle integration.
*
* @package core
* @category phpunit
* @category test
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_phpunit_basic_testcase extends basic_testcase {
class basic_test extends \basic_testcase {
protected $testassertexecuted = false;
protected function setUp(): void {
@@ -75,14 +65,14 @@ class core_phpunit_basic_testcase extends basic_testcase {
$this->assertEqualsCanonicalizing($a, $b);
// Objects.
$a = new stdClass();
$a = new \stdClass();
$a->x = 'x';
$a->y = 'y';
$b = new stdClass(); // Switched order.
$b = new \stdClass(); // Switched order.
$b->y = 'y';
$b->x = 'x';
$c = $a;
$d = new stdClass();
$d = new \stdClass();
$d->x = 'x';
$d->y = 'y';
$d->z = 'z';
@@ -121,7 +111,7 @@ class core_phpunit_basic_testcase extends basic_testcase {
$this->assertNotEmpty('0 ');
$this->assertNotEmpty(true);
$this->assertNotEmpty(array(null));
$this->assertNotEmpty(new stdClass());
$this->assertNotEmpty(new \stdClass());
}
/**
+6 -14
View File
@@ -14,33 +14,25 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* PHPUnit Util tests
*
* @package core
* @category phpunit
* @copyright 2015 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
namespace core;
/**
* Test util extra features.
*
* @package core
* @category phpunit
* @category test
* @copyright 2015 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_phpunit_util_testcase extends advanced_testcase {
class util_test extends \advanced_testcase {
/**
* @dataProvider set_table_modified_by_sql_provider
*/
public function test_set_table_modified_by_sql($sql, $expectations) {
phpunit_util::reset_updated_table_list();
phpunit_util::set_table_modified_by_sql($sql);
\phpunit_util::reset_updated_table_list();
\phpunit_util::set_table_modified_by_sql($sql);
foreach ($expectations as $table => $present) {
$this->assertEquals($present, !empty(phpunit_util::$tableupdated[$table]));
$this->assertEquals($present, !empty(\phpunit_util::$tableupdated[$table]));
}
}
+3 -9
View File
@@ -14,14 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for parts of adminlib.php.
*
* @package core
* @subpackage admin
* @copyright 2020 Brendan Heywood <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
defined('MOODLE_INTERNAL') || die();
@@ -31,10 +24,11 @@ require_once($CFG->libdir.'/adminlib.php');
/**
* Unit tests for parts of adminlib.php.
*
* @package core
* @copyright 2020 Brendan Heywood <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_adminlib_testcase extends advanced_testcase {
class adminlib_test extends \advanced_testcase {
/**
* Data provider of serialized string.
+23 -14
View File
@@ -14,15 +14,19 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for those parts of adminlib.php that implement the admin tree
* functionality.
*
* @package core
* @category phpunit
* @copyright 2013 David Mudrak <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
use admin_category;
use admin_externalpage;
use admin_root;
use admin_settingpage;
use admin_setting_configdirectory;
use admin_setting_configduration;
use admin_setting_configexecutable;
use admin_setting_configfile;
use admin_setting_configmixedhostiplist;
use admin_setting_configpasswordunmask;
use admin_setting_configtext;
defined('MOODLE_INTERNAL') || die();
@@ -31,8 +35,13 @@ require_once($CFG->libdir.'/adminlib.php');
/**
* Provides the unit tests for admin tree functionality.
*
* @package core
* @category test
* @copyright 2013 David Mudrak <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_admintree_testcase extends advanced_testcase {
class admintree_test extends \advanced_testcase {
/**
* Adding nodes into the admin tree.
@@ -97,13 +106,13 @@ class core_admintree_testcase extends advanced_testcase {
public function test_add_nodes_before_invalid1() {
$tree = new admin_root(true);
$this->expectException(coding_exception::class);
$this->expectException(\coding_exception::class);
$tree->add('root', new admin_externalpage('foo', 'Foo', 'http://foo.bar'), array('moodle:site/config'));
}
public function test_add_nodes_before_invalid2() {
$tree = new admin_root(true);
$this->expectException(coding_exception::class);
$this->expectException(\coding_exception::class);
$tree->add('root', new admin_category('bar', 'Bar'), '');
}
@@ -267,7 +276,7 @@ class core_admintree_testcase extends advanced_testcase {
$original = $setting->get_setting();
$error = $setting->write_setting($data[$fullname]);
if ($error !== '') {
$adminroot->errors[$fullname] = new stdClass();
$adminroot->errors[$fullname] = new \stdClass();
$adminroot->errors[$fullname]->data = $data[$fullname];
$adminroot->errors[$fullname]->id = $setting->get_id();
$adminroot->errors[$fullname]->error = $error;
@@ -430,7 +439,7 @@ class core_admintree_testcase extends advanced_testcase {
global $DB;
// Current user is a manager at site context, which won't have access to the 'debugging' section of the admin tree.
$manageruser = $this->getDataGenerator()->create_user();
$context = context_system::instance();
$context = \context_system::instance();
$managerrole = $DB->get_record('role', array('shortname' => 'manager'));
role_assign($managerrole->id, $manageruser->id, $context->id);
$this->setUser($manageruser);
+4 -7
View File
@@ -14,22 +14,19 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core;
/**
* Code quality unit tests that are fast enough to run each time.
*
* @package core
* @category phpunit
* @category test
* @copyright 2013 Andrew Nicols
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
defined('MOODLE_INTERNAL') || die();
/**
* @covers ::ajax_capture_output
* @covers ::ajax_check_captured_output
*/
class core_ajaxlib_testcase extends advanced_testcase {
class ajaxlib_test extends \advanced_testcase {
/** @var string Original error log */
protected $oldlog;
+8 -12
View File
@@ -14,14 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for core analysers.
*
* @package core
* @category test
* @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
use test_target_course_level_shortname;
use test_target_shortname;
defined('MOODLE_INTERNAL') || die();
@@ -37,7 +33,7 @@ require_once(__DIR__ . '/../../lib/enrollib.php');
* @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_analytics_analysers_testcase extends advanced_testcase {
class analysers_test extends \advanced_testcase {
/**
* test_courses_analyser
@@ -59,7 +55,7 @@ class core_analytics_analysers_testcase extends advanced_testcase {
$this->assertInstanceOf('\context_course', $analyser->sample_access_context($course1->id));
// Just 1 sample per course.
$class = new ReflectionClass('\core\analytics\analyser\courses');
$class = new \ReflectionClass('\core\analytics\analyser\courses');
$method = $class->getMethod('get_all_samples');
$method->setAccessible(true);
list($sampleids, $samplesdata) = $method->invoke($analyser, $analysable);
@@ -109,7 +105,7 @@ class core_analytics_analysers_testcase extends advanced_testcase {
$this->assertInstanceOf('\context_system', $analyser->sample_access_context($course1->id));
$this->assertInstanceOf('\context_system', $analyser->sample_access_context($course3->id));
$class = new ReflectionClass('\core\analytics\analyser\site_courses');
$class = new \ReflectionClass('\core\analytics\analyser\site_courses');
$method = $class->getMethod('get_all_samples');
$method->setAccessible(true);
list($sampleids, $samplesdata) = $method->invoke($analyser, $analysable);
@@ -161,7 +157,7 @@ class core_analytics_analysers_testcase extends advanced_testcase {
$this->assertInstanceOf('\core_analytics\course', $analyser->get_sample_analysable($ue1->id));
$this->assertInstanceOf('\context_course', $analyser->sample_access_context($ue1->id));
$class = new ReflectionClass('\core\analytics\analyser\student_enrolments');
$class = new \ReflectionClass('\core\analytics\analyser\student_enrolments');
$method = $class->getMethod('get_all_samples');
$method->setAccessible(true);
list($sampleids, $samplesdata) = $method->invoke($analyser, $analysable);
+14 -18
View File
@@ -14,22 +14,18 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core;
/**
* Authentication related tests.
*
* @package core_auth
* @category phpunit
* @package core
* @category test
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Functional test for authentication related APIs.
*/
class core_authlib_testcase extends advanced_testcase {
class authlib_test extends \advanced_testcase {
public function test_lockout() {
global $CFG;
require_once("$CFG->libdir/authlib.php");
@@ -347,7 +343,7 @@ class core_authlib_testcase extends advanced_testcase {
$result = authenticate_user_login('username4', 'a', false, $reason);
$events = $sink->get_events();
$sink->close();
$notifications = \core\notification::fetch();
$notifications = notification::fetch();
$this->assertInstanceOf('stdClass', $result);
$this->assertEquals(AUTH_LOGIN_OK, $reason);
$this->assertEquals(get_user_preferences('auth_forcepasswordchange', false, $result), false);
@@ -370,7 +366,7 @@ class core_authlib_testcase extends advanced_testcase {
$this->assertEquals(count($events), 1);
$this->assertEquals(reset($events)->eventname, '\core\event\user_password_policy_failed');
// Check notification fired.
$notifications = \core\notification::fetch();
$notifications = notification::fetch();
$this->assertEquals(count($notifications), 1);
// Now the same tests with a user that passes the password policy.
@@ -381,7 +377,7 @@ class core_authlib_testcase extends advanced_testcase {
$result = authenticate_user_login('username5', 'ThisPassword1sSecure!', false, $reason);
$events = $sink->get_events();
$sink->close();
$notifications = \core\notification::fetch();
$notifications = notification::fetch();
$this->assertInstanceOf('stdClass', $result);
$this->assertEquals(AUTH_LOGIN_OK, $reason);
$this->assertEquals(get_user_preferences('auth_forcepasswordchange', false, $result), false);
@@ -396,7 +392,7 @@ class core_authlib_testcase extends advanced_testcase {
$result = authenticate_user_login('username5', 'ThisPassword1sSecure!', false, $reason);
$events = $sink->get_events();
$sink->close();
$notifications = \core\notification::fetch();
$notifications = notification::fetch();
$this->assertInstanceOf('stdClass', $result);
$this->assertEquals(AUTH_LOGIN_OK, $reason);
$this->assertEquals(get_user_preferences('auth_forcepasswordchange', false, $result), false);
@@ -410,7 +406,7 @@ class core_authlib_testcase extends advanced_testcase {
try {
$event = \core\event\user_loggedin::create(array('objectid' => 1));
$this->fail('\core\event\user_loggedin requires other[\'username\']');
} catch(Exception $e) {
} catch(\Exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
}
@@ -434,8 +430,8 @@ class core_authlib_testcase extends advanced_testcase {
// inject our own validation method here and revert it back once we are done. This custom validator method is
// identical to the default 'php' validator with the only difference: it has the FILTER_FLAG_EMAIL_UNICODE set
// so that it allows to use non-ASCII characters in email addresses.
$defaultvalidator = moodle_phpmailer::$validator;
moodle_phpmailer::$validator = function($address) {
$defaultvalidator = \moodle_phpmailer::$validator;
\moodle_phpmailer::$validator = function($address) {
return (bool) filter_var($address, FILTER_VALIDATE_EMAIL, FILTER_FLAG_EMAIL_UNICODE);
};
@@ -472,6 +468,6 @@ class core_authlib_testcase extends advanced_testcase {
$this->assertArrayNotHasKey('email', $errors);
// Restore the original email address validator.
moodle_phpmailer::$validator = $defaultvalidator;
\moodle_phpmailer::$validator = $defaultvalidator;
}
}
+4 -12
View File
@@ -14,18 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Check API unit tests
*
* @package core
* @category check
* @copyright 2020 Brendan Heywood <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
namespace core;
use core\check\result;
use core\check\security\passwordpolicy;
/**
* Example unit tests for check API
@@ -35,7 +27,7 @@ use core\check\result;
* @copyright 2020 Brendan Heywood <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class check_testcase extends advanced_testcase {
class check_test extends \advanced_testcase {
/**
* A simple example showing how a check and result object works
@@ -48,7 +40,7 @@ class check_testcase extends advanced_testcase {
global $CFG;
$prior = $CFG->passwordpolicy;
$check = new core\check\security\passwordpolicy();
$check = new passwordpolicy();
$CFG->passwordpolicy = false;
$result = $check->get_result();
+10 -6
View File
@@ -15,25 +15,29 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Collator unit tests.
* Unit tests for our utf-8 aware collator which is used for sorting.
*
* @package core
* @category phpunit
* @category test
* @copyright 2011 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
use core_collator;
defined('MOODLE_INTERNAL') || die();
/**
* Unit tests for our utf-8 aware collator which is used for sorting.
*
* @package core
* @category phpunit
* @category test
* @copyright 2011 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_collator_testcase extends advanced_testcase {
class collator_test extends \advanced_testcase {
/**
* @var string The initial lang, stored because we change it during testing
@@ -268,7 +272,7 @@ class core_collator_testcase extends advanced_testcase {
$this->assertSame(array('cc', 'aa', 'ab'), array_values($arr));
$this->assertTrue($result);
$obj = new stdClass();
$obj = new \stdClass();
$arr = array('1.1.1'=>array(), '1.2'=>$obj, '1.20.2'=>null);
$result = core_collator::ksort($arr, core_collator::SORT_NATURAL);
$this->assertSame(array('1.1.1', '1.2', '1.20.2'), array_keys($arr));
@@ -294,7 +298,7 @@ class core_collator_testcase extends advanced_testcase {
* @copyright 2011 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class string_test_class extends stdClass {
class string_test_class extends \stdClass {
/**
* @var string A public property
*/
+17 -3
View File
@@ -18,17 +18,31 @@
* Unit tests for /lib/componentlib.class.php.
*
* @package core
* @category phpunit
* @category test
* @copyright 2011 Tomasz Muras
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
use component_installer;
use lang_installer;
use lang_installer_exception;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir.'/componentlib.class.php');
class core_componentlib_testcase extends advanced_testcase {
/**
* Unit tests for /lib/componentlib.class.php.
*
* @package core
* @category test
* @copyright 2011 Tomasz Muras
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class componentlib_test extends \advanced_testcase {
public function test_component_installer() {
global $CFG;
@@ -107,7 +121,7 @@ class core_componentlib_testcase extends advanced_testcase {
try {
$installer->run();
$this->fail('lang_installer_exception exception expected');
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
$this->assertInstanceOf('lang_installer_exception', $e);
}
}
+3 -10
View File
@@ -14,14 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for config only library functions-
*
* @package core
* @category phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
defined('MOODLE_INTERNAL') || die();
@@ -33,11 +26,11 @@ require_once(__DIR__ . '/../configonlylib.php');
* Unit tests for config only library functions.
*
* @package core
* @category phpunit
* @category test
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_configonlylib_testcase extends advanced_testcase {
class configonlylib_test extends \advanced_testcase {
/**
* Test cleaning of invalid utf-8 entities.
+11 -16
View File
@@ -14,14 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Test for core_media_player_native.
*
* @package core
* @category test
* @copyright 2019 Ruslan Kabalin
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
use media_test_native_plugin;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/fixtures/testable_core_media_player_native.php');
@@ -35,7 +30,7 @@ require_once(__DIR__ . '/fixtures/testable_core_media_player_native.php');
* @copyright 2019 Ruslan Kabalin
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_media_player_native_testcase extends advanced_testcase {
class core_media_player_native_test extends \advanced_testcase {
/**
* Pre-test setup.
@@ -70,7 +65,7 @@ class core_media_player_native_testcase extends advanced_testcase {
// Create list of URLs for each extension.
$urls = array_map(function($ext){
return new moodle_url('http://example.org/video.' . $ext);
return new \moodle_url('http://example.org/video.' . $ext);
}, $nativeextensions);
// Make sure that the list of supported URLs is not filtering permitted extensions.
@@ -83,8 +78,8 @@ class core_media_player_native_testcase extends advanced_testcase {
*/
public function test_get_attribute() {
$urls = [
new moodle_url('http://example.org/some_filename.mp4'),
new moodle_url('http://example.org/some_filename_hires.mp4'),
new \moodle_url('http://example.org/some_filename.mp4'),
new \moodle_url('http://example.org/some_filename_hires.mp4'),
];
$player = new media_test_native_plugin();
@@ -101,8 +96,8 @@ class core_media_player_native_testcase extends advanced_testcase {
*/
public function test_add_remove_attributes() {
$urls = [
new moodle_url('http://example.org/some_filename.mp4'),
new moodle_url('http://example.org/some_filename_hires.mp4'),
new \moodle_url('http://example.org/some_filename.mp4'),
new \moodle_url('http://example.org/some_filename_hires.mp4'),
];
$player = new media_test_native_plugin();
@@ -140,8 +135,8 @@ class core_media_player_native_testcase extends advanced_testcase {
*/
public function test_replace_sources() {
$urls = [
new moodle_url('http://example.org/some_filename.mp4'),
new moodle_url('http://example.org/some_filename_hires.mp4'),
new \moodle_url('http://example.org/some_filename.mp4'),
new \moodle_url('http://example.org/some_filename_hires.mp4'),
];
$player = new media_test_native_plugin();
@@ -14,19 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core;
/**
* Unit tests for core renderer render template exploit.
*
* @package core
* @category test
* @copyright 2019 Ryan Wyllie <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Unit tests for core renderer render template exploit.
*/
class core_renderer_template_exploit_testcase extends advanced_testcase {
class core_renderer_template_exploit_test extends \advanced_testcase {
/**
* Test cases to confirm that blacklisted helpers are stripped from the source
* text by the helper before being passed to other another helper. This prevents
+11 -10
View File
@@ -14,6 +14,13 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir.'/cronlib.php');
/**
* Unit tests for the cron.
*
@@ -22,13 +29,7 @@
* @copyright 2013 Tim Gusak <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir.'/cronlib.php');
class cronlib_testcase extends basic_testcase {
class cronlib_test extends \basic_testcase {
/**
* Data provider for cron_delete_from_temp.
@@ -130,7 +131,7 @@ class cronlib_testcase extends basic_testcase {
* @param bool $keep Should the node exist after the delete function has run
*/
private function generate_test_path($path, $isdir = false, $time = 0, $keep = false) {
$node = new stdClass();
$node = new \stdClass();
$node->path = $path;
$node->isdir = $isdir;
$node->time = $time;
@@ -163,8 +164,8 @@ class cronlib_testcase extends basic_testcase {
$task = new \core\task\file_temp_cleanup_task();
$task->execute();
$dir = new RecursiveDirectoryIterator($tmpdir);
$iter = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::CHILD_FIRST);
$dir = new \RecursiveDirectoryIterator($tmpdir);
$iter = new \RecursiveIteratorIterator($dir, \RecursiveIteratorIterator::CHILD_FIRST);
$actual = array();
for ($iter->rewind(); $iter->valid(); $iter->next()) {
+4 -12
View File
@@ -14,30 +14,22 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the unittests for the css optimiser in csslib.php
*
* @package core_css
* @category phpunit
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/csslib.php');
/**
* CSS optimiser test class.
*
* @package core_css
* @category phpunit
* @package core
* @category test
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_csslib_testcase extends advanced_testcase {
class csslib_test extends \advanced_testcase {
/**
* Test that css_is_colour function throws an exception.
+13 -9
View File
@@ -14,21 +14,25 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Tests csv import and export functions.
*
* @package core
* @category phpunit
* @copyright 2012 Adrian Greeve
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
use csv_export_writer;
use csv_import_reader;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/lib/csvlib.class.php');
class core_csvclass_testcase extends advanced_testcase {
/**
* Tests csv import and export functions.
*
* @package core
* @category test
* @copyright 2012 Adrian Greeve
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class csvclass_test extends \advanced_testcase {
protected $testdata = array();
protected $teststring = '';
+4 -12
View File
@@ -14,15 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for /lib/classes/curl/curl_security_helper.php.
*
* @package core
* @copyright 2016 Jake Dallimore <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
namespace core;
/**
* cURL security test suite.
@@ -34,7 +26,7 @@ defined('MOODLE_INTERNAL') || die();
* @copyright 2016 Jake Dallimore <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_curl_security_helper_testcase extends advanced_testcase {
class curl_security_helper_test extends \advanced_testcase {
/**
* Test for \core\files\curl_security_helper::url_is_blocked().
*
@@ -206,7 +198,7 @@ class core_curl_security_helper_testcase extends advanced_testcase {
$this->resetAfterTest(true);
$helper = new \core\files\curl_security_helper();
set_config('curlsecurityblockedhosts', $blockedhosts);
$this->assertEquals($expected, phpunit_util::call_internal_method($helper, 'host_is_blocked', [$host],
$this->assertEquals($expected, \phpunit_util::call_internal_method($helper, 'host_is_blocked', [$host],
'\core\files\curl_security_helper'));
}
@@ -260,7 +252,7 @@ class core_curl_security_helper_testcase extends advanced_testcase {
$this->resetAfterTest(true);
$helper = new \core\files\curl_security_helper();
set_config('curlsecurityallowedport', $allowedports);
$this->assertEquals($expected, phpunit_util::call_internal_method($helper, 'port_is_blocked', [$port],
$this->assertEquals($expected, \phpunit_util::call_internal_method($helper, 'port_is_blocked', [$port],
'\core\files\curl_security_helper'));
}
+55 -40
View File
@@ -18,14 +18,68 @@
* Code quality unit tests that are fast enough to run each time.
*
* @package core
* @category phpunit
* @category test
* @copyright (C) 2013 onwards Remote Learner.net Inc (http://www.remote-learner.net)
* @author Brent Boghosian ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
use context;
use context_helper;
defined('MOODLE_INTERNAL') || die();
/**
* Code quality unit tests that are fast enough to run each time.
*
* @package core
* @category test
* @copyright (C) 2013 onwards Remote Learner.net Inc (http://www.remote-learner.net)
* @author Brent Boghosian ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class customcontext_test extends \advanced_testcase {
/**
* Perform setup before every test. This tells Moodle's phpunit to reset the database after every test.
*/
protected function setUp(): void {
parent::setUp();
$this->resetAfterTest(true);
}
/**
* Test case for custom context classes
*/
public function test_customcontexts() {
global $CFG;
static $customcontexts = array(
11 => 'context_bogus1',
12 => 'context_bogus2',
13 => 'context_bogus3'
);
// save any existing custom contexts
$existingcustomcontexts = get_config(null, 'custom_context_classes');
set_config('custom_context_classes', serialize($customcontexts));
initialise_cfg();
context_helper::reset_levels();
$alllevels = context_helper::get_all_levels();
$this->assertEquals($alllevels[11], 'context_bogus1');
$this->assertEquals($alllevels[12], 'context_bogus2');
$this->assertEquals($alllevels[13], 'context_bogus3');
// clean-up & restore any custom contexts
set_config('custom_context_classes', ($existingcustomcontexts === false) ? null : $existingcustomcontexts);
initialise_cfg();
context_helper::reset_levels();
}
}
/**
* Bogus custom context class for testing
*/
@@ -97,42 +151,3 @@ class context_bogus3 extends context {
return array();
}
}
class customcontext_testcase extends advanced_testcase {
/**
* Perform setup before every test. This tells Moodle's phpunit to reset the database after every test.
*/
protected function setUp(): void {
parent::setUp();
$this->resetAfterTest(true);
}
/**
* Test case for custom context classes
*/
public function test_customcontexts() {
global $CFG;
static $customcontexts = array(
11 => 'context_bogus1',
12 => 'context_bogus2',
13 => 'context_bogus3'
);
// save any existing custom contexts
$existingcustomcontexts = get_config(null, 'custom_context_classes');
set_config('custom_context_classes', serialize($customcontexts));
initialise_cfg();
context_helper::reset_levels();
$alllevels = context_helper::get_all_levels();
$this->assertEquals($alllevels[11], 'context_bogus1');
$this->assertEquals($alllevels[12], 'context_bogus2');
$this->assertEquals($alllevels[13], 'context_bogus3');
// clean-up & restore any custom contexts
set_config('custom_context_classes', ($existingcustomcontexts === false) ? null : $existingcustomcontexts);
initialise_cfg();
context_helper::reset_levels();
}
}
@@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core;
/**
* Tests for {@link decompose_update_into_safe_changes()} and
* {@link update_field_with_unique_index()}.
@@ -22,18 +24,7 @@
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Tests for {@link decompose_update_into_safe_changes()} and
* {@link update_field_with_unique_index()}.
*
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class datalib_update_with_unique_index_testcase extends advanced_testcase {
class datalib_update_with_unique_index_test extends \advanced_testcase {
public function test_decompose_update_into_safe_changes_identity() {
$this->assertEquals(array(), decompose_update_into_safe_changes(
@@ -72,7 +63,7 @@ class datalib_update_with_unique_index_testcase extends advanced_testcase {
try {
decompose_update_into_safe_changes(array(1 => 1), 1);
$this->fail('Expected exception was not thrown');
} catch (coding_exception $e) {
} catch (\coding_exception $e) {
$this->assertEquals('Supposedly unused value 1 is actually used!', $e->a);
}
}
@@ -90,7 +81,7 @@ class datalib_update_with_unique_index_testcase extends advanced_testcase {
$dbman = $DB->get_manager();
$this->resetAfterTest();
$table = new xmldb_table('test_table');
$table = new \xmldb_table('test_table');
$table->setComment("This is a test'n drop table. You can drop it safely");
$tablename = $table->getName();
+8 -17
View File
@@ -14,16 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Tests legacy Moodle date/time functions.
*
* @package core
* @copyright 2015 Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Petr Skoda <[email protected]>
*/
defined('MOODLE_INTERNAL') || die();
namespace core;
/**
* Tests legacy Moodle date/time functions.
@@ -33,7 +24,7 @@ defined('MOODLE_INTERNAL') || die();
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Petr Skoda <[email protected]>
*/
class core_date_legacy_testcase extends advanced_testcase {
class date_legacy_test extends \advanced_testcase {
public function test_settings() {
global $CFG;
$this->resetAfterTest();
@@ -156,7 +147,7 @@ class core_date_legacy_testcase extends advanced_testcase {
foreach ($years as $year) {
foreach ($dates as $date) {
$result = make_timestamp($year, $date[0], $date[1], $date[2], $date[3], $date[4], $tz, true);
$expected = new DateTime('now', new DateTimeZone(($tz == 99 ? 'Pacific/Auckland' : $tz)));
$expected = new \DateTime('now', new \DateTimeZone(($tz == 99 ? 'Pacific/Auckland' : $tz)));
$expected->setDate($year, $date[0], $date[1]);
$expected->setTime($date[2], $date[3], $date[4]);
$this->assertSame($expected->getTimestamp(), $result,
@@ -170,7 +161,7 @@ class core_date_legacy_testcase extends advanced_testcase {
foreach ($years as $year) {
foreach ($dates as $date) {
$result = make_timestamp($year, $date[0], $date[1], $date[2], $date[3], $date[4], $tz, true);
$expected = new DateTime('now', new DateTimeZone(($tz == 99 ? 'Pacific/Auckland' : $tz)));
$expected = new \DateTime('now', new \DateTimeZone(($tz == 99 ? 'Pacific/Auckland' : $tz)));
$expected->setDate($year, $date[0], $date[1]);
$expected->setTime($date[2], $date[3], $date[4]);
$this->assertSame($expected->getTimestamp(), $result,
@@ -200,7 +191,7 @@ class core_date_legacy_testcase extends advanced_testcase {
foreach ($timezones as $tz) {
foreach ($years as $year) {
foreach ($dates as $date) {
$expected = new DateTime('now', new DateTimeZone(($tz == 99 ? 'Pacific/Auckland' : $tz)));
$expected = new \DateTime('now', new \DateTimeZone(($tz == 99 ? 'Pacific/Auckland' : $tz)));
$expected->setDate($year, $date[0], $date[1]);
$expected->setTime($date[2], $date[3], $date[4]);
$result = usergetdate($expected->getTimestamp(), $tz);
@@ -227,7 +218,7 @@ class core_date_legacy_testcase extends advanced_testcase {
foreach ($timezones as $tz) {
foreach ($years as $year) {
foreach ($dates as $date) {
$expected = new DateTime('now', new DateTimeZone(($tz == 99 ? 'Pacific/Auckland' : $tz)));
$expected = new \DateTime('now', new \DateTimeZone(($tz == 99 ? 'Pacific/Auckland' : $tz)));
$expected->setDate($year, $date[0], $date[1]);
$expected->setTime($date[2], $date[3], $date[4]);
$result = usergetdate($expected->getTimestamp(), $tz);
@@ -276,13 +267,13 @@ class core_date_legacy_testcase extends advanced_testcase {
$this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
foreach ($years as $year) {
foreach ($dates as $date) {
$expected = new DateTime('now', new DateTimeZone('UTC'));
$expected = new \DateTime('now', new \DateTimeZone('UTC'));
$expected->setDate($year, $date[0], $date[1]);
$expected->setTime($date[2], $date[3], $date[4]);
foreach ($users as $user) {
$this->setUser($user);
$expected->setTimezone(new DateTimeZone(($user->timezone == 99 ? 'Pacific/Auckland' : $user->timezone)));
$expected->setTimezone(new \DateTimeZone(($user->timezone == 99 ? 'Pacific/Auckland' : $user->timezone)));
$result = userdate($expected->getTimestamp(), '', 99, false, false);
date_default_timezone_set($expected->getTimezone()->getName());
$ex = strftime($format, $expected->getTimestamp());
+4 -7
View File
@@ -14,20 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core;
/**
* Tests editors subsystem.
*
* @package core_editors
* @subpackage phpunit
* @package core
* @copyright 2013 onwards Martin Dougiamas (http://dougiamas.com)
* @author Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
class core_editorslib_testcase extends advanced_testcase {
class editorlib_test extends \advanced_testcase {
/**
* Tests the installation of event handlers from file
+5 -8
View File
@@ -14,6 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core;
use environment_results;
/**
* Moodle environment test.
*
@@ -22,14 +26,7 @@
* @copyright 2013 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Do standard environment.xml tests.
*/
class core_environment_testcase extends advanced_testcase {
class environment_test extends \advanced_testcase {
/**
* Test the environment check status.
@@ -14,20 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for the context locking events.
*
* @package core
* @category test
* @copyright 2019 University of Nottingham
* @author Neill Magill <neill.magill@nottingham.ac.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core\event\context_locked;
use core\event\context_unlocked;
defined('MOODLE_INTERNAL') || die();
namespace core\event;
/**
* Unit tests for the context_locked and context_unlocked events.
@@ -38,7 +25,7 @@ defined('MOODLE_INTERNAL') || die();
* @author Neill Magill <neill.magill@nottingham.ac.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_event_context_locked_testcase extends \advanced_testcase {
class context_locked_test extends \advanced_testcase {
/**
* Locks an unlocked context and checks that a core\event\context_locked event is created.
*
@@ -55,7 +42,7 @@ class core_event_context_locked_testcase extends \advanced_testcase {
$locksink->close();
self::assertCount(1, $lockevents);
self::assertContainsOnlyInstancesOf('core\event\context_locked', $lockevents);
self::assertContainsOnlyInstancesOf(context_locked::class, $lockevents);
self::assertEquals($context->id, $lockevents[0]->objectid);
$this->assertSame('context', $lockevents[0]->objecttable);
$this->assertEquals($context, $lockevents[0]->get_context());
@@ -101,7 +88,7 @@ class core_event_context_locked_testcase extends \advanced_testcase {
$unlocksink->close();
self::assertCount(1, $unlockevents);
self::assertContainsOnlyInstancesOf('core\event\context_unlocked', $unlockevents);
self::assertContainsOnlyInstancesOf(context_unlocked::class, $unlockevents);
self::assertEquals($context->id, $unlockevents[0]->objectid);
$this->assertSame('context', $unlockevents[0]->objecttable);
$this->assertEquals($context, $unlockevents[0]->get_context());
@@ -14,23 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\event;
/**
* Tests for deprecated events. Please add tests for deprecated events in this file.
* Tests for deprecated events.
*
* @package core
* @category phpunit
* @category test
* @copyright 2013 onwards Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Class core_event_instances_list_viewed_testcase
*
* Tests for deprecated events.
*/
class core_event_deprecated_testcase extends advanced_testcase {
class deprecated_test extends \advanced_testcase {
/**
* Test event properties and methods.
@@ -38,8 +32,7 @@ class core_event_deprecated_testcase extends advanced_testcase {
public function test_deprecated_course_module_instances_list_viewed_events() {
// Make sure the abstract class course_module_instances_list_viewed generates a debugging notice.
require_once(__DIR__.'/fixtures/event_mod_badfixtures.php');
require_once(__DIR__ . '/../fixtures/event_mod_badfixtures.php');
$this->assertDebuggingCalled(null, DEBUG_DEVELOPER);
}
}
@@ -14,16 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Tests the \core\event\grade_deleted event.
*
* @package core
* @category test
* @copyright 2014 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
namespace core\event;
/**
* Class core_event_grade_deleted_testcase
@@ -35,7 +26,7 @@ defined('MOODLE_INTERNAL') || die();
* @copyright 2014 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_event_grade_deleted_testcase extends advanced_testcase {
class grade_deleted_test extends \advanced_testcase {
/**
* Tests the event details.
@@ -57,12 +48,12 @@ class core_event_grade_deleted_testcase extends advanced_testcase {
grade_update('mod/quiz', $course->id, 'mod', 'quiz', $quiz->id, 0, $grade);
// Get the grade item and override it.
$gradeitem = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'quiz', 'iteminstance' => $quiz->id,
$gradeitem = \grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'quiz', 'iteminstance' => $quiz->id,
'courseid' => $course->id));
$gradeitem->update_final_grade($user->id, 10, 'gradebook');
// Get the grade_grade object.
$gradegrade = new grade_grade(array('userid' => $user->id, 'itemid' => $gradeitem->id), true);
$gradegrade = new \grade_grade(array('userid' => $user->id, 'itemid' => $gradeitem->id), true);
$gradegrade->grade_item = $gradeitem;
// Trigger the event.
@@ -76,7 +67,7 @@ class core_event_grade_deleted_testcase extends advanced_testcase {
$grade = $event->get_grade();
$this->assertInstanceOf('grade_grade', $grade);
$this->assertInstanceOf('\core\event\grade_deleted', $event);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$this->assertEquals(\context_course::instance($course->id), $event->get_context());
$this->assertSame($event->objecttable, 'grade_grades');
$this->assertEquals($event->objectid, $gradegrade->id);
$this->assertEquals($event->other['itemid'], $gradeitem->id);
@@ -14,14 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Tests the events related to the user profile fields and categories.
*
* @package core
* @category test
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\event;
use profile_define_base;
defined('MOODLE_INTERNAL') || die();
@@ -37,7 +32,7 @@ require_once($CFG->dirroot . '/user/profile/definelib.php');
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_event_profile_field_testcase extends advanced_testcase {
class profile_field_test extends \advanced_testcase {
/**
* Test set up.
@@ -134,7 +129,7 @@ class core_event_profile_field_testcase extends advanced_testcase {
$cat1 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']);
// Create a new profile field.
$data = new stdClass();
$data = new \stdClass();
$data->datatype = 'text';
$data->shortname = 'example';
$data->name = 'Example field';
@@ -14,6 +14,12 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\event;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/../fixtures/event_fixtures.php');
/**
* Tests for event manager, base event and observers.
*
@@ -22,15 +28,10 @@
* @copyright 2014 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/fixtures/event_fixtures.php');
class core_event_unknown_logged_testcase extends advanced_testcase {
class unknown_logged_test extends \advanced_testcase {
public function test_restore_event() {
$event1 = \core_tests\event\unittest_executed::create(array('context' => context_system::instance(), 'other' => array('sample' => 1, 'xx' => 10)));
$event1 = \core_tests\event\unittest_executed::create(array('context' => \context_system::instance(), 'other' => array('sample' => 1, 'xx' => 10)));
$data1 = $event1->get_data();
$data1['eventname'] = '\mod_xx\event\xx_yy';
@@ -50,4 +51,4 @@ class core_event_unknown_logged_testcase extends advanced_testcase {
$this->assertEquals($data1, $data2);
$this->assertEquals($extra1, $extra2);
}
}
}
@@ -14,14 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Tests the \core\event\user_graded event.
*
* @package core
* @category phpunit
* @copyright 2014 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\event;
defined('MOODLE_INTERNAL') || die();
@@ -30,8 +23,6 @@ global $CFG;
require_once($CFG->libdir . '/mathslib.php');
/**
* Class core_event_user_graded_testcase
*
* Tests for event \core\event\user_graded
*
* @package core
@@ -39,7 +30,7 @@ require_once($CFG->libdir . '/mathslib.php');
* @copyright 2014 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_event_user_graded_testcase extends advanced_testcase {
class user_graded_test extends \advanced_testcase {
/**
* Tests set up.
@@ -59,13 +50,13 @@ class core_event_user_graded_testcase extends advanced_testcase {
$user = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user->id, $course->id);
$grade_category = grade_category::fetch_course_category($course->id);
$grade_category = \grade_category::fetch_course_category($course->id);
$grade_category->load_grade_item();
$grade_item = $grade_category->grade_item;
$grade_item->update_final_grade($user->id, 10, 'gradebook');
$grade_grade = new grade_grade(array('userid' => $user->id, 'itemid' => $grade_item->id), true);
$grade_grade = new \grade_grade(array('userid' => $user->id, 'itemid' => $grade_item->id), true);
$grade_grade->grade_item = $grade_item;
$event = \core\event\user_graded::create_from_grade($grade_grade);
@@ -74,7 +65,7 @@ class core_event_user_graded_testcase extends advanced_testcase {
array($course->id, 'grade', 'update', '/report/grader/index.php?id=' . $course->id, $grade_item->itemname . ': ' . fullname($user)),
$event
);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$this->assertEquals(\context_course::instance($course->id), $event->get_context());
$this->assertSame($event->objecttable, 'grade_grades');
$this->assertEquals($event->objectid, $grade_grade->id);
$this->assertEquals($event->other['itemid'], $grade_item->id);
@@ -110,8 +101,8 @@ class core_event_user_graded_testcase extends advanced_testcase {
$quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));
$quizitemparams = array('itemtype' => 'mod', 'itemmodule' => 'quiz', 'iteminstance' => $quiz->id,
'courseid' => $course->id);
$gradeitem = grade_item::fetch($quizitemparams);
$courseitem = grade_item::fetch_course_item($course->id);
$gradeitem = \grade_item::fetch($quizitemparams);
$courseitem = \grade_item::fetch_course_item($course->id);
// Now mark the quiz using grade_update as this is the function that modules use.
$grade = array();
@@ -134,9 +125,9 @@ class core_event_user_graded_testcase extends advanced_testcase {
// will be set as needing an update when the grades are deleted.
$gradeitem->delete_all_grades();
grade_regrade_final_grades($course->id);
$gradeitem = grade_item::fetch($quizitemparams);
$gradeitem = \grade_item::fetch($quizitemparams);
// Now, create a grade using grade_item::update_final_grade().
// Now, create a grade using \grade_item::update_final_grade().
$sink = $this->redirectEvents();
$gradeitem->update_raw_grade($user->id, 10);
$events = $sink->get_events();
@@ -149,7 +140,7 @@ class core_event_user_graded_testcase extends advanced_testcase {
$this->assertInstanceOf('\core\event\user_graded', $events[1]);
$this->assertEquals($courseitem->id, $events[1]->other['itemid']);
// Now, update this grade using grade_item::update_raw_grade().
// Now, update this grade using \grade_item::update_raw_grade().
$sink = $this->redirectEvents();
$gradeitem->update_raw_grade($user->id, 20);
$events = $sink->get_events();
@@ -166,9 +157,9 @@ class core_event_user_graded_testcase extends advanced_testcase {
// will be set as needing an update when the grades are deleted.
$gradeitem->delete_all_grades();
grade_regrade_final_grades($course->id);
$gradeitem = grade_item::fetch($quizitemparams);
$gradeitem = \grade_item::fetch($quizitemparams);
// Now, create a grade using grade_item::update_final_grade().
// Now, create a grade using \grade_item::update_final_grade().
$sink = $this->redirectEvents();
$gradeitem->update_final_grade($user->id, 30);
$events = $sink->get_events();
@@ -181,7 +172,7 @@ class core_event_user_graded_testcase extends advanced_testcase {
$this->assertInstanceOf('\core\event\user_graded', $events[1]);
$this->assertEquals($courseitem->id, $events[1]->other['itemid']);
// Now, update this grade using grade_item::update_final_grade().
// Now, update this grade using \grade_item::update_final_grade().
$sink = $this->redirectEvents();
$gradeitem->update_final_grade($user->id, 40);
$events = $sink->get_events();
@@ -194,12 +185,12 @@ class core_event_user_graded_testcase extends advanced_testcase {
$this->assertInstanceOf('\core\event\user_graded', $events[1]);
$this->assertEquals($courseitem->id, $events[1]->other['itemid']);
// Remove the overridden flag from the grade, this was set by grade_item::update_final_grade().
$gradegrade = grade_grade::fetch(array('itemid' => $gradeitem->id, 'userid' => $user->id));
// Remove the overridden flag from the grade, this was set by \grade_item::update_final_grade().
$gradegrade = \grade_grade::fetch(array('itemid' => $gradeitem->id, 'userid' => $user->id));
$gradegrade->set_overridden(false, false);
// Let's change the calculation to anything that won't cause an error.
$calculation = calc_formula::unlocalize("=3");
$calculation = \calc_formula::unlocalize("=3");
$gradeitem->set_calculation($calculation);
// Now force the computation of the grade.
@@ -216,10 +207,10 @@ class core_event_user_graded_testcase extends advanced_testcase {
$this->assertEquals($courseitem->id, $events[1]->other['itemid']);
// Now, let's trick the gradebook, we manually update a grade, and flag the grade item as
// needing a regrading, so we can trigger the event in grade_item::regrade_final_grades().
$gradeitem = grade_item::fetch($quizitemparams);
// needing a regrading, so we can trigger the event in \grade_item::regrade_final_grades().
$gradeitem = \grade_item::fetch($quizitemparams);
$gradeitem->set_calculation('');
$gradegrade = grade_grade::fetch(array('itemid' => $gradeitem->id, 'userid' => $user->id));
$gradegrade = \grade_grade::fetch(array('itemid' => $gradeitem->id, 'userid' => $user->id));
$gradegrade->rawgrade = 50;
$gradegrade->update();
@@ -14,26 +14,17 @@
// 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 password changes event.
*
* @package core
* @category phpunit
* @copyright 2014 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
namespace core\event;
/**
* Tests for event \core\event\user_password_updated
*
* @package core
* @category phpunit
* @category test
* @copyright 2014 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_event_user_password_updated_testcase extends advanced_testcase {
class user_password_updated_test extends \advanced_testcase {
/**
* Test the event.
*/
@@ -41,9 +32,9 @@ class core_event_user_password_updated_testcase extends advanced_testcase {
$this->resetAfterTest();
$user1 = $this->getDataGenerator()->create_user();
$context1 = context_user::instance($user1->id);
$context1 = \context_user::instance($user1->id);
$user2 = $this->getDataGenerator()->create_user();
$context2 = context_user::instance($user2->id);
$context2 = \context_user::instance($user2->id);
$this->setUser($user1);
+19 -17
View File
@@ -15,13 +15,15 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Exporter class tests.
* Exporter testcase.
*
* @package core
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
defined('MOODLE_INTERNAL') || die();
global $CFG;
@@ -32,7 +34,7 @@ global $CFG;
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_exporter_testcase extends advanced_testcase {
class exporter_test extends \advanced_testcase {
protected $validrelated = null;
protected $invalidrelated = null;
@@ -40,7 +42,7 @@ class core_exporter_testcase extends advanced_testcase {
protected $invaliddata = null;
public function setUp(): void {
$s = new stdClass();
$s = new \stdClass();
$this->validrelated = array(
'simplestdClass' => $s,
'arrayofstdClass' => array($s, $s),
@@ -104,14 +106,14 @@ class core_exporter_testcase extends advanced_testcase {
$output = $PAGE->get_renderer('core');
// The exception message is a bit misleading, it actually indicates an expected property wasn't found.
$this->expectException(coding_exception::class);
$this->expectException(\coding_exception::class);
$this->expectExceptionMessage('Unexpected property stringAformat');
$result = $exporter->export($output);
}
public function test_invalid_related() {
$this->expectException(coding_exception::class);
$this->expectExceptionMessage('Exporter class is missing required related data: (core_testable_exporter) ' .
$this->expectException(\coding_exception::class);
$this->expectExceptionMessage('Exporter class is missing required related data: (core\core_testable_exporter) ' .
'simplestdClass => stdClass');
$exporter = new core_testable_exporter($this->validdata, $this->invalidrelated);
}
@@ -127,7 +129,7 @@ class core_exporter_testcase extends advanced_testcase {
$exporter = new core_testable_exporter($this->validdata, $data);
$output = $PAGE->get_renderer('core');
$result = $exporter->export($output);
} catch (coding_exception $e) {
} catch (\coding_exception $e) {
$this->assertNotFalse(strpos($e->getMessage(), $key));
}
}
@@ -147,14 +149,14 @@ class core_exporter_testcase extends advanced_testcase {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$syscontext = context_system::instance();
$coursecontext = context_course::instance($course->id);
$syscontext = \context_system::instance();
$coursecontext = \context_course::instance($course->id);
external_settings::get_instance()->set_filter(true);
\external_settings::get_instance()->set_filter(true);
filter_set_global_state('urltolink', TEXTFILTER_OFF);
filter_set_local_state('urltolink', $coursecontext->id, TEXTFILTER_ON);
set_config('formats', FORMAT_MARKDOWN, 'filter_urltolink');
filter_manager::reset_caches();
\filter_manager::reset_caches();
$data = [
'stringA' => '__Watch out:__ https://moodle.org @@PLUGINFILE@@/test.pdf',
@@ -168,7 +170,7 @@ class core_exporter_testcase extends advanced_testcase {
$result = $exporter->export($output);
$youtube = 'https://moodle.org';
$fileurl = (new moodle_url('/webservice/pluginfile.php/' . $syscontext->id . '/test/area/9/test.pdf'))->out(false);
$fileurl = (new \moodle_url('/webservice/pluginfile.php/' . $syscontext->id . '/test/area/9/test.pdf'))->out(false);
$expected = "<p><strong>Watch out:</strong> $youtube $fileurl</p>\n";
$this->assertEquals($expected, $result->stringA);
$this->assertEquals(FORMAT_HTML, $result->stringAformat);
@@ -177,7 +179,7 @@ class core_exporter_testcase extends advanced_testcase {
$exporter = new core_testable_exporter($data, ['context' => $coursecontext] + $this->validrelated);
$result = $exporter->export($output);
$youtube = '<a href="https://moodle.org" class="_blanktarget">https://moodle.org</a>';
$fileurl = (new moodle_url('/webservice/pluginfile.php/' . $coursecontext->id . '/test/area/9/test.pdf'))->out(false);
$fileurl = (new \moodle_url('/webservice/pluginfile.php/' . $coursecontext->id . '/test/area/9/test.pdf'))->out(false);
$expected = "<p><strong>Watch out:</strong> $youtube <a href=\"$fileurl\" class=\"_blanktarget\">$fileurl</a></p>\n";
$this->assertEquals($expected, $result->stringA);
$this->assertEquals(FORMAT_HTML, $result->stringAformat);
@@ -214,7 +216,7 @@ class core_testable_exporter extends \core\external\exporter {
'astring' => 'string', 'abool' => 'bool', 'aint' => 'int', 'ints' => 'int[]');
}
protected function get_other_values(renderer_base $output) {
protected function get_other_values(\renderer_base $output) {
return array(
'otherstring' => '>Another <strong>string</strong>',
'otherstrings' => array('String >a', 'String <strong>b</strong>')
@@ -259,7 +261,7 @@ class core_testable_exporter extends \core\external\exporter {
protected function get_format_parameters_for_stringA() {
return [
// For testing use the passed context if any.
'context' => isset($this->related['context']) ? $this->related['context'] : context_system::instance(),
'context' => isset($this->related['context']) ? $this->related['context'] : \context_system::instance(),
'component' => 'test',
'filearea' => 'area',
'itemid' => 9,
@@ -268,14 +270,14 @@ class core_testable_exporter extends \core\external\exporter {
protected function get_format_parameters_for_otherstring() {
return [
'context' => context_system::instance(),
'context' => \context_system::instance(),
'options' => ['escape' => false]
];
}
protected function get_format_parameters_for_otherstrings() {
return [
'context' => context_system::instance(),
'context' => \context_system::instance(),
];
}
}
+93 -80
View File
@@ -17,19 +17,33 @@
/**
* Unit tests for /lib/filelib.php.
*
* @package core_files
* @category phpunit
* @package core
* @category test
* @copyright 2009 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
use core_filetypes;
use curl;
use repository;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/filelib.php');
require_once($CFG->dirroot . '/repository/lib.php');
class core_filelib_testcase extends advanced_testcase {
/**
* Unit tests for /lib/filelib.php.
*
* @package core
* @category test
* @copyright 2009 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class filelib_test extends \advanced_testcase {
public function test_format_postdata_for_curlcall() {
// POST params with just simple types.
@@ -196,12 +210,12 @@ class core_filelib_testcase extends advanced_testcase {
// Test HTTP success.
$testhtml = $this->getExternalTestFileUrl('/test.html');
$curl = new curl();
$curl = new \curl();
$contents = $curl->get($testhtml);
$this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
$this->assertSame(0, $curl->get_errno());
$curl = new curl();
$curl = new \curl();
$tofile = "$CFG->tempdir/test.html";
@unlink($tofile);
$fp = fopen($tofile, 'w');
@@ -212,7 +226,7 @@ class core_filelib_testcase extends advanced_testcase {
$this->assertSame($contents, file_get_contents($tofile));
@unlink($tofile);
$curl = new curl();
$curl = new \curl();
$tofile = "$CFG->tempdir/test.html";
@unlink($tofile);
$result = $curl->download_one($testhtml, array(), array('filepath'=>$tofile));
@@ -222,7 +236,7 @@ class core_filelib_testcase extends advanced_testcase {
@unlink($tofile);
// Test 404 request.
$curl = new curl();
$curl = new \curl();
$contents = $curl->get($this->getExternalTestFileUrl('/i.do.not.exist'));
$response = $curl->getResponse();
$this->assertSame('404 Not Found', reset($response));
@@ -237,12 +251,12 @@ class core_filelib_testcase extends advanced_testcase {
// Test a request with a basic hostname filter applied.
$testhtml = $this->getExternalTestFileUrl('/test.html');
$url = new moodle_url($testhtml);
$url = new \moodle_url($testhtml);
$host = $url->get_host();
set_config('curlsecurityblockedhosts', $host); // Blocks $host.
// Create curl with the default security enabled. We expect this to be blocked.
$curl = new curl();
$curl = new \curl();
$contents = $curl->get($testhtml);
$expected = $curl->get_security()->get_blocked_url_string();
$this->assertSame($expected, $contents);
@@ -250,7 +264,7 @@ class core_filelib_testcase extends advanced_testcase {
// Now, create a curl using the 'ignoresecurity' override.
// We expect this request to pass, despite the admin setting having been set earlier.
$curl = new curl(['ignoresecurity' => true]);
$curl = new \curl(['ignoresecurity' => true]);
$contents = $curl->get($testhtml);
$this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
$this->assertSame(0, $curl->get_errno());
@@ -264,7 +278,7 @@ class core_filelib_testcase extends advanced_testcase {
// And make the mock security helper block all URLs. This helper instance doesn't care about config.
$mockhelper->expects($this->any())->method('url_is_blocked')->will($this->returnValue(true));
$curl = new curl(['securityhelper' => $mockhelper]);
$curl = new \curl(['securityhelper' => $mockhelper]);
$contents = $curl->get($testhtml);
$this->assertSame('You shall not pass', $curl->get_security()->get_blocked_url_string());
$this->assertSame($curl->get_security()->get_blocked_url_string(), $contents);
@@ -275,7 +289,7 @@ class core_filelib_testcase extends advanced_testcase {
$testurl = $this->getExternalTestFileUrl('/test_redir.php');
$curl = new curl();
$curl = new \curl();
$contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2));
$response = $curl->getResponse();
$this->assertSame('200 OK', reset($response));
@@ -284,7 +298,7 @@ class core_filelib_testcase extends advanced_testcase {
$this->assertSame('done', $contents);
// All redirects are emulated now. Enabling "emulateredirects" explicitly does not have effect.
$curl = new curl();
$curl = new \curl();
$curl->emulateredirects = true;
$contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2));
$response = $curl->getResponse();
@@ -294,7 +308,7 @@ class core_filelib_testcase extends advanced_testcase {
$this->assertSame('done', $contents);
// All redirects are emulated now. Attempting to disable "emulateredirects" explicitly causes warning.
$curl = new curl();
$curl = new \curl();
$curl->emulateredirects = false;
$contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS' => 2));
$response = $curl->getResponse();
@@ -314,7 +328,7 @@ class core_filelib_testcase extends advanced_testcase {
$responsecode302 = '302 Found';
}
$curl = new curl();
$curl = new \curl();
$contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0));
$response = $curl->getResponse();
$this->assertSame($responsecode302, reset($response));
@@ -322,12 +336,12 @@ class core_filelib_testcase extends advanced_testcase {
$this->assertSame(302, $curl->info['http_code']);
$this->assertSame('', $contents);
$curl = new curl();
$curl = new \curl();
$contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>1));
$this->assertSame(CURLE_TOO_MANY_REDIRECTS, $curl->get_errno());
$this->assertNotEmpty($contents);
$curl = new curl();
$curl = new \curl();
$tofile = "$CFG->tempdir/test.html";
@unlink($tofile);
$fp = fopen($tofile, 'w');
@@ -338,7 +352,7 @@ class core_filelib_testcase extends advanced_testcase {
$this->assertSame('done', file_get_contents($tofile));
@unlink($tofile);
$curl = new curl();
$curl = new \curl();
$tofile = "$CFG->tempdir/test.html";
@unlink($tofile);
$fp = fopen($tofile, 'w');
@@ -349,7 +363,7 @@ class core_filelib_testcase extends advanced_testcase {
$this->assertSame('done', file_get_contents($tofile));
@unlink($tofile);
$curl = new curl();
$curl = new \curl();
$tofile = "$CFG->tempdir/test.html";
@unlink($tofile);
$result = $curl->download_one("$testurl?redir=1", array(), array('filepath'=>$tofile));
@@ -358,7 +372,7 @@ class core_filelib_testcase extends advanced_testcase {
$this->assertSame('done', file_get_contents($tofile));
@unlink($tofile);
$curl = new curl();
$curl = new \curl();
$tofile = "$CFG->tempdir/test.html";
@unlink($tofile);
$result = $curl->download_one("$testurl?redir=1&verbose=1", array(), array('filepath' => $tofile));
@@ -381,21 +395,21 @@ class core_filelib_testcase extends advanced_testcase {
set_config('curlsecurityblockedhosts', 'moodle.com');
// Redirecting to a non-blocked host should resolve.
$curl = new curl();
$curl = new \curl();
$contents = $curl->get("{$testurl}?redir=2");
$response = $curl->getResponse();
$this->assertSame('200 OK', reset($response));
$this->assertSame(0, $curl->get_errno());
// Redirecting to the blocked host should fail.
$curl = new curl();
$curl = new \curl();
$blockedstring = $curl->get_security()->get_blocked_url_string();
$contents = $curl->get("{$testurl}?redir=1&extdest=1");
$this->assertSame($blockedstring, $contents);
$this->assertSame(0, $curl->get_errno());
// Redirecting to the blocked host after multiple successful redirects should also fail.
$curl = new curl();
$curl = new \curl();
$contents = $curl->get("{$testurl}?redir=3&extdest=1");
$this->assertSame($blockedstring, $contents);
$this->assertSame(0, $curl->get_errno());
@@ -405,7 +419,7 @@ class core_filelib_testcase extends advanced_testcase {
// Test relative location redirects.
$testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
$curl = new curl();
$curl = new \curl();
$contents = $curl->get($testurl);
$response = $curl->getResponse();
$this->assertSame('200 OK', reset($response));
@@ -416,7 +430,7 @@ class core_filelib_testcase extends advanced_testcase {
// Test different redirect types.
$testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
$curl = new curl();
$curl = new \curl();
$contents = $curl->get("$testurl?type=301");
$response = $curl->getResponse();
$this->assertSame('200 OK', reset($response));
@@ -424,7 +438,7 @@ class core_filelib_testcase extends advanced_testcase {
$this->assertSame(1, $curl->info['redirect_count']);
$this->assertSame('done', $contents);
$curl = new curl();
$curl = new \curl();
$contents = $curl->get("$testurl?type=302");
$response = $curl->getResponse();
$this->assertSame('200 OK', reset($response));
@@ -432,7 +446,7 @@ class core_filelib_testcase extends advanced_testcase {
$this->assertSame(1, $curl->info['redirect_count']);
$this->assertSame('done', $contents);
$curl = new curl();
$curl = new \curl();
$contents = $curl->get("$testurl?type=303");
$response = $curl->getResponse();
$this->assertSame('200 OK', reset($response));
@@ -440,7 +454,7 @@ class core_filelib_testcase extends advanced_testcase {
$this->assertSame(1, $curl->info['redirect_count']);
$this->assertSame('done', $contents);
$curl = new curl();
$curl = new \curl();
$contents = $curl->get("$testurl?type=307");
$response = $curl->getResponse();
$this->assertSame('200 OK', reset($response));
@@ -448,7 +462,7 @@ class core_filelib_testcase extends advanced_testcase {
$this->assertSame(1, $curl->info['redirect_count']);
$this->assertSame('done', $contents);
$curl = new curl();
$curl = new \curl();
$contents = $curl->get("$testurl?type=308");
$response = $curl->getResponse();
$this->assertSame('200 OK', reset($response));
@@ -467,7 +481,7 @@ class core_filelib_testcase extends advanced_testcase {
// Test without proxy bypass and inaccessible proxy.
$CFG->proxyhost = 'i.do.not.exist';
$CFG->proxybypass = '';
$curl = new curl();
$curl = new \curl();
$contents = $curl->get($testurl);
$this->assertNotEquals(0, $curl->get_errno());
$this->assertNotEquals('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
@@ -475,7 +489,7 @@ class core_filelib_testcase extends advanced_testcase {
// Test with proxy bypass.
$testurlhost = parse_url($testurl, PHP_URL_HOST);
$CFG->proxybypass = $testurlhost;
$curl = new curl();
$curl = new \curl();
$contents = $curl->get($testurl);
$this->assertSame(0, $curl->get_errno());
$this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
@@ -490,7 +504,7 @@ class core_filelib_testcase extends advanced_testcase {
public function test_duplicate_curl_header() {
$testurl = $this->getExternalTestFileUrl('/test_post.php');
$curl = new curl();
$curl = new \curl();
$headerdata = 'Accept: application/json';
$header = [$headerdata, $headerdata];
$this->assertCount(2, $header);
@@ -503,7 +517,7 @@ class core_filelib_testcase extends advanced_testcase {
$testurl = $this->getExternalTestFileUrl('/test_post.php');
// Test post request.
$curl = new curl();
$curl = new \curl();
$contents = $curl->post($testurl, 'data=moodletest');
$response = $curl->getResponse();
$this->assertSame('200 OK', reset($response));
@@ -511,7 +525,7 @@ class core_filelib_testcase extends advanced_testcase {
$this->assertSame('OK', $contents);
// Test 100 requests.
$curl = new curl();
$curl = new \curl();
$curl->setHeader('Expect: 100-continue');
$contents = $curl->post($testurl, 'data=moodletest');
$response = $curl->getResponse();
@@ -526,7 +540,7 @@ class core_filelib_testcase extends advanced_testcase {
$fs = get_file_storage();
$filerecord = array(
'contextid' => context_system::instance()->id,
'contextid' => \context_system::instance()->id,
'component' => 'test',
'filearea' => 'curl_post',
'itemid' => 0,
@@ -538,7 +552,7 @@ class core_filelib_testcase extends advanced_testcase {
// Test post with file.
$data = array('testfile' => $testfile);
$curl = new curl();
$curl = new \curl();
$contents = $curl->post($testurl, $data);
$this->assertSame('OK', $contents);
}
@@ -549,7 +563,7 @@ class core_filelib_testcase extends advanced_testcase {
$fs = get_file_storage();
$filerecord = array(
'contextid' => context_system::instance()->id,
'contextid' => \context_system::instance()->id,
'component' => 'test',
'filearea' => 'curl_post',
'itemid' => 0,
@@ -561,7 +575,7 @@ class core_filelib_testcase extends advanced_testcase {
// Test post with file.
$data = array('testfile' => $testfile);
$curl = new curl();
$curl = new \curl();
$contents = $curl->post($testurl, $data);
$this->assertSame('OK', $contents);
}
@@ -570,7 +584,7 @@ class core_filelib_testcase extends advanced_testcase {
// HTTP and HTTPS requests were verified in previous requests. Now check
// that we can selectively disable some protocols.
$curl = new curl();
$curl = new \curl();
// Other protocols than HTTP(S) are disabled by default.
$testurl = 'file:///';
@@ -618,7 +632,7 @@ class core_filelib_testcase extends advanced_testcase {
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$usercontext = context_user::instance($user->id);
$usercontext = \context_user::instance($user->id);
$USER = $DB->get_record('user', array('id'=>$user->id));
$repositorypluginname = 'user';
@@ -631,7 +645,7 @@ class core_filelib_testcase extends advanced_testcase {
$fs = get_file_storage();
$syscontext = context_system::instance();
$syscontext = \context_system::instance();
$component = 'core';
$filearea = 'unittest';
$itemid = 0;
@@ -654,7 +668,7 @@ class core_filelib_testcase extends advanced_testcase {
$this->assertInstanceOf('stored_file', $originalfile);
// Create a user private file.
$userfilerecord = new stdClass;
$userfilerecord = new \stdClass;
$userfilerecord->contextid = $usercontext->id;
$userfilerecord->component = 'user';
$userfilerecord->filearea = 'private';
@@ -729,7 +743,7 @@ class core_filelib_testcase extends advanced_testcase {
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$usercontext = context_user::instance($user->id);
$usercontext = \context_user::instance($user->id);
$USER = $DB->get_record('user', array('id'=>$user->id));
$repositorypluginname = 'user';
@@ -741,12 +755,12 @@ class core_filelib_testcase extends advanced_testcase {
$this->assertInstanceOf('repository', $userrepository);
$fs = get_file_storage();
$syscontext = context_system::instance();
$syscontext = \context_system::instance();
$filecontent = 'User file content';
// Create a user private file.
$userfilerecord = new stdClass;
$userfilerecord = new \stdClass;
$userfilerecord->contextid = $usercontext->id;
$userfilerecord->component = 'user';
$userfilerecord->filearea = 'private';
@@ -800,7 +814,7 @@ class core_filelib_testcase extends advanced_testcase {
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$usercontext = context_user::instance($user->id);
$usercontext = \context_user::instance($user->id);
$USER = $DB->get_record('user', array('id' => $user->id));
$repositorypluginname = 'user';
@@ -812,12 +826,12 @@ class core_filelib_testcase extends advanced_testcase {
$this->assertInstanceOf('repository', $userrepository);
$fs = get_file_storage();
$syscontext = context_system::instance();
$syscontext = \context_system::instance();
$filecontent = 'User file content';
// Create a user private file.
$userfilerecord = new stdClass;
$userfilerecord = new \stdClass;
$userfilerecord->contextid = $usercontext->id;
$userfilerecord->component = 'user';
$userfilerecord->filearea = 'private';
@@ -868,7 +882,7 @@ class core_filelib_testcase extends advanced_testcase {
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$usercontext = context_user::instance($user->id);
$usercontext = \context_user::instance($user->id);
$USER = $DB->get_record('user', ['id' => $user->id]);
$draftitemid = 0;
@@ -935,9 +949,9 @@ EOF;
$mdl30648expected = preg_replace("~(?!<\r)\n~", "\r\n", $mdl30648expected);
// Test stripping works OK.
$this->assertSame($mdl30648expected, curl::strip_double_headers($mdl30648example));
$this->assertSame($mdl30648expected, \curl::strip_double_headers($mdl30648example));
// Test it does nothing to the 'plain' data.
$this->assertSame($mdl30648expected, curl::strip_double_headers($mdl30648expected));
$this->assertSame($mdl30648expected, \curl::strip_double_headers($mdl30648expected));
// Example from OU proxy.
$httpsexample = <<<EOF
@@ -973,9 +987,9 @@ EOF;
$httpsexpected = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexpected);
// Test stripping works OK.
$this->assertSame($httpsexpected, curl::strip_double_headers($httpsexample));
$this->assertSame($httpsexpected, \curl::strip_double_headers($httpsexample));
// Test it does nothing to the 'plain' data.
$this->assertSame($httpsexpected, curl::strip_double_headers($httpsexpected));
$this->assertSame($httpsexpected, \curl::strip_double_headers($httpsexpected));
$httpsexample = <<<EOF
HTTP/1.0 200 Connection established
@@ -1010,9 +1024,9 @@ EOF;
$httpsexpected = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexpected);
// Test stripping works OK.
$this->assertSame($httpsexpected, curl::strip_double_headers($httpsexample));
$this->assertSame($httpsexpected, \curl::strip_double_headers($httpsexample));
// Test it does nothing to the 'plain' data.
$this->assertSame($httpsexpected, curl::strip_double_headers($httpsexpected));
$this->assertSame($httpsexpected, \curl::strip_double_headers($httpsexpected));
$httpsexample = <<<EOF
HTTP/1.0 200 Connection established
@@ -1047,9 +1061,9 @@ EOF;
$httpsexpected = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexpected);
// Test stripping works OK.
$this->assertSame($httpsexpected, curl::strip_double_headers($httpsexample));
$this->assertSame($httpsexpected, \curl::strip_double_headers($httpsexample));
// Test it does nothing to the 'plain' data.
$this->assertSame($httpsexpected, curl::strip_double_headers($httpsexpected));
$this->assertSame($httpsexpected, \curl::strip_double_headers($httpsexpected));
$httpsexample = <<<EOF
HTTP/1.1 200 Connection established
@@ -1084,9 +1098,9 @@ EOF;
$httpsexpected = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexpected);
// Test stripping works OK.
$this->assertSame($httpsexpected, curl::strip_double_headers($httpsexample));
$this->assertSame($httpsexpected, \curl::strip_double_headers($httpsexample));
// Test it does nothing to the 'plain' data.
$this->assertSame($httpsexpected, curl::strip_double_headers($httpsexpected));
$this->assertSame($httpsexpected, \curl::strip_double_headers($httpsexpected));
$httpsexample = <<<EOF
HTTP/2 200 Connection established
@@ -1121,10 +1135,9 @@ EOF;
$httpsexpected = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexpected);
// Test stripping works OK.
$this->assertSame($httpsexpected, curl::strip_double_headers($httpsexample));
$this->assertSame($httpsexpected, \curl::strip_double_headers($httpsexample));
// Test it does nothing to the 'plain' data.
$this->assertSame($httpsexpected, curl::strip_double_headers($httpsexpected));
$this->assertSame($httpsexpected, \curl::strip_double_headers($httpsexpected));
}
/**
@@ -1153,7 +1166,7 @@ EOF;
get_mimetype_description(array('filename' => 'test.frog')));
// Test custom description using multilang filter.
filter_manager::reset_caches();
\filter_manager::reset_caches();
filter_set_global_state('multilang', TEXTFILTER_ON);
filter_set_applies_to_strings('multilang', true);
core_filetypes::update_type('frog', 'frog', 'application/x-frog', 'document',
@@ -1243,7 +1256,7 @@ EOF;
// Finally, test it via exttests, to ensure the agent is sent properly.
// Matching.
$testurl = $this->getExternalTestFileUrl('/test_agent.php');
$extcurl = new curl();
$extcurl = new \curl();
$contents = $extcurl->get($testurl, array(), array('CURLOPT_USERAGENT' => 'AnotherUserAgent/1.2'));
$response = $extcurl->getResponse();
$this->assertSame('200 OK', reset($response));
@@ -1262,7 +1275,7 @@ EOF;
*/
public function test_file_rewrite_pluginfile_urls() {
$syscontext = context_system::instance();
$syscontext = \context_system::instance();
$originaltext = 'Fake test with an image <img src="@@PLUGINFILE@@/image.png">';
// Do the rewrite.
@@ -1287,7 +1300,7 @@ EOF;
$this->resetAfterTest();
$syscontext = context_system::instance();
$syscontext = \context_system::instance();
$originaltext = 'Fake test with an image <img src="@@PLUGINFILE@@/image.png">';
$options = ['includetoken' => true];
@@ -1336,7 +1349,7 @@ EOF;
$this->resetAfterTest();
$syscontext = context_system::instance();
$syscontext = \context_system::instance();
$originaltext = 'Fake test with an image <img src="@@PLUGINFILE@@/image.png">';
$options = ['includetoken' => true];
@@ -1386,7 +1399,7 @@ EOF;
if (isset($filedata['contextid'])) {
$filerecord['contextid'] = $filedata['contextid'];
} else {
$usercontext = context_user::instance($USER->id);
$usercontext = \context_user::instance($USER->id);
$filerecord['contextid'] = $usercontext->id;
}
$source = isset($filedata['source']) ? $filedata['source'] : serialize((object)array('source' => 'From string'));
@@ -1407,7 +1420,7 @@ EOF;
$this->resetAfterTest(true);
$this->setAdminUser();
$fs = get_file_storage();
$usercontext = context_user::instance($USER->id);
$usercontext = \context_user::instance($USER->id);
// Create a draft file.
$filename = 'data.txt';
@@ -1521,7 +1534,7 @@ EOF;
// Add new file.
file_merge_files_from_draft_area_into_filearea($file->get_itemid(), $file->get_contextid(), 'user', 'private', 0, $options);
$usercontext = context_user::instance($USER->id);
$usercontext = \context_user::instance($USER->id);
$files = $fs->get_area_files($usercontext->id, 'user', 'private', 0);
$this->assertCount(0, $files);
}
@@ -1546,7 +1559,7 @@ EOF;
// Add new file.
file_merge_files_from_draft_area_into_filearea($file->get_itemid(), $file->get_contextid(), 'user', 'private', 0, $options);
$usercontext = context_user::instance($USER->id);
$usercontext = \context_user::instance($USER->id);
// Check we only get the base directory, not a new file.
$files = $fs->get_area_files($usercontext->id, 'user', 'private', 0);
$this->assertCount(1, $files);
@@ -1572,7 +1585,7 @@ EOF;
// Add new file.
file_merge_files_from_draft_area_into_filearea($file->get_itemid(), $file->get_contextid(), 'user', 'private', 0, $options);
$usercontext = context_user::instance($USER->id);
$usercontext = \context_user::instance($USER->id);
// Check we only get the base directory, not a new file.
$files = $fs->get_area_files($usercontext->id, 'user', 'private', 0);
$this->assertCount(1, $files);
@@ -1605,7 +1618,7 @@ EOF;
$size += $file->get_filesize();
// Create directory.
$usercontext = context_user::instance($USER->id);
$usercontext = \context_user::instance($USER->id);
$dir = $fs->create_directory($usercontext->id, 'user', 'draft', $draftitemid, '/testsubdir/');
// Add file to directory.
$filerecord = array(
@@ -1662,7 +1675,7 @@ EOF;
$size += $file->get_filesize();
// Create directory.
$usercontext = context_user::instance($USER->id);
$usercontext = \context_user::instance($USER->id);
$dir = $fs->create_directory($usercontext->id, 'user', 'draft', $draftitemid, '/testsubdir/');
// Add file to directory.
$filerecord = array(
@@ -1714,7 +1727,7 @@ EOF;
// Confirm the user drafts area lists 3 files.
$fs = get_file_storage();
$usercontext = context_user::instance($USER->id);
$usercontext = \context_user::instance($USER->id);
$draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'itemid', 0);
$this->assertCount(3, $draftfiles);
@@ -1798,7 +1811,7 @@ EOF;
// Confirm one file in each draft area.
$fs = get_file_storage();
$usercontext = context_user::instance($USER->id);
$usercontext = \context_user::instance($USER->id);
$draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $file1->get_itemid(), 'itemid', 0);
$this->assertCount(1, $draftfiles);
$draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $file2->get_itemid(), 'itemid', 0);
@@ -1896,7 +1909,7 @@ class testable_curl extends curl {
*/
public function get_options() {
// Access to private property.
$rp = new ReflectionProperty('curl', 'options');
$rp = new \ReflectionProperty('curl', 'options');
$rp->setAccessible(true);
return $rp->getValue($this);
}
@@ -1908,7 +1921,7 @@ class testable_curl extends curl {
*/
public function set_options($options) {
// Access to private property.
$rp = new ReflectionProperty('curl', 'options');
$rp = new \ReflectionProperty('curl', 'options');
$rp->setAccessible(true);
$rp->setValue($this, $options);
}
@@ -1935,14 +1948,14 @@ class testable_curl extends curl {
}
/**
* Wrapper to access the private curl::apply_opt() method using reflection.
* Wrapper to access the private \curl::apply_opt() method using reflection.
*
* @param array $options
* @return resource The curl handle
*/
public function call_apply_opt($options = null) {
// Access to private method.
$rm = new ReflectionMethod('curl', 'apply_opt');
$rm = new \ReflectionMethod('curl', 'apply_opt');
$rm->setAccessible(true);
$ch = curl_init();
return $rm->invoke($this, $ch, $options);
+6 -10
View File
@@ -14,13 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for /lib/filestorage/zip_archive.php.
*
* @package core_files
* @copyright 2020 Université Rennes 2 {@link https://www.univ-rennes2.fr}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
use zip_archive;
defined('MOODLE_INTERNAL') || die();
@@ -31,11 +27,11 @@ require_once($CFG->libdir . '/filestorage/zip_archive.php');
/**
* Unit tests for /lib/filestorage/zip_archive.php.
*
* @package core_files
* @package core
* @copyright 2020 Université Rennes 2 {@link https://www.univ-rennes2.fr}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class filestorage_zip_archive_testcase extends advanced_testcase {
class filestorage_zip_archive_test extends \advanced_testcase {
/**
* Test mangle_pathname() method.
*
@@ -47,7 +43,7 @@ class filestorage_zip_archive_testcase extends advanced_testcase {
public function test_mangle_pathname($string, $expected) {
$ziparchive = new zip_archive();
$method = new ReflectionMethod('zip_archive', 'mangle_pathname');
$method = new \ReflectionMethod('zip_archive', 'mangle_pathname');
$method->setAccessible(true);
$result = $method->invoke($ziparchive, $string);
+14 -18
View File
@@ -14,13 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for /lib/classes/filetypes.php.
*
* @package core
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
use core_filetypes;
defined('MOODLE_INTERNAL') || die();
@@ -34,7 +30,7 @@ require_once($CFG->libdir . '/filelib.php');
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_filetypes_testcase extends advanced_testcase {
class filetypes_test extends \advanced_testcase {
public function test_add_type() {
$this->resetAfterTest();
@@ -63,7 +59,7 @@ class core_filetypes_testcase extends advanced_testcase {
try {
core_filetypes::add_type('frog', 'application/x-frog', 'document');
$this->fail();
} catch (coding_exception $e) {
} catch (\coding_exception $e) {
$this->assertStringContainsString('already exists', $e->getMessage());
$this->assertStringContainsString('frog', $e->getMessage());
}
@@ -72,14 +68,14 @@ class core_filetypes_testcase extends advanced_testcase {
try {
core_filetypes::add_type('.frog', 'application/x-frog', 'document');
$this->fail();
} catch (coding_exception $e) {
} catch (\coding_exception $e) {
$this->assertStringContainsString('Invalid extension', $e->getMessage());
$this->assertStringContainsString('..frog', $e->getMessage());
}
try {
core_filetypes::add_type('', 'application/x-frog', 'document');
$this->fail();
} catch (coding_exception $e) {
} catch (\coding_exception $e) {
$this->assertStringContainsString('Invalid extension', $e->getMessage());
}
@@ -89,7 +85,7 @@ class core_filetypes_testcase extends advanced_testcase {
core_filetypes::add_type('gecko', 'text/plain', 'document',
array(), '', '', true);
$this->fail();
} catch (coding_exception $e) {
} catch (\coding_exception $e) {
$this->assertStringContainsString('default icon set', $e->getMessage());
$this->assertStringContainsString('text/plain', $e->getMessage());
}
@@ -121,7 +117,7 @@ class core_filetypes_testcase extends advanced_testcase {
try {
core_filetypes::update_type('doc', 'doc', 'application/x-frog', 'document');
$this->fail();
} catch (coding_exception $e) {
} catch (\coding_exception $e) {
$this->assertStringContainsString('not found', $e->getMessage());
$this->assertStringContainsString('doc', $e->getMessage());
}
@@ -130,14 +126,14 @@ class core_filetypes_testcase extends advanced_testcase {
try {
core_filetypes::update_type('docccc', '.frog', 'application/x-frog', 'document');
$this->fail();
} catch (coding_exception $e) {
} catch (\coding_exception $e) {
$this->assertStringContainsString('Invalid extension', $e->getMessage());
$this->assertStringContainsString('.frog', $e->getMessage());
}
try {
core_filetypes::update_type('docccc', '', 'application/x-frog', 'document');
$this->fail();
} catch (coding_exception $e) {
} catch (\coding_exception $e) {
$this->assertStringContainsString('Invalid extension', $e->getMessage());
}
@@ -146,7 +142,7 @@ class core_filetypes_testcase extends advanced_testcase {
core_filetypes::update_type('docccc', 'docccc', 'text/plain', 'document',
array(), '', '', true);
$this->fail();
} catch (coding_exception $e) {
} catch (\coding_exception $e) {
$this->assertStringContainsString('default icon set', $e->getMessage());
$this->assertStringContainsString('text/plain', $e->getMessage());
}
@@ -168,7 +164,7 @@ class core_filetypes_testcase extends advanced_testcase {
try {
core_filetypes::delete_type('doc');
$this->fail();
} catch (coding_exception $e) {
} catch (\coding_exception $e) {
$this->assertStringContainsString('not found', $e->getMessage());
$this->assertStringContainsString('doc', $e->getMessage());
}
@@ -203,7 +199,7 @@ class core_filetypes_testcase extends advanced_testcase {
try {
core_filetypes::revert_type_to_default('frog');
$this->fail();
} catch (coding_exception $e) {
} catch (\coding_exception $e) {
$this->assertStringContainsString('not a default type', $e->getMessage());
$this->assertStringContainsString('frog', $e->getMessage());
}
+5 -11
View File
@@ -14,6 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core;
use filter_manager;
/**
* Unit tests for the {@link filter_manager} class.
*
@@ -22,17 +26,7 @@
* @copyright 2015 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
defined('MOODLE_INTERNAL') || die();
/**
* Unit tests for the {@link filter_manager} class.
*
* @copyright 2015 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
class core_filter_manager_testcase extends advanced_testcase {
class filter_manager_test extends \advanced_testcase {
/**
* Helper method to apply filters to some text and return the result.
+43 -41
View File
@@ -14,15 +14,9 @@
// 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 the parts of ../filterlib.php that involve loading the configuration
* from, and saving the configuration to, the database.
*
* @package core_filter
* @category phpunit
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
use filter_manager;
defined('MOODLE_INTERNAL') || die();
@@ -31,8 +25,16 @@ require_once($CFG->libdir . '/filterlib.php');
/**
* Test filters.
*
* Tests for the parts of ../filterlib.php that involve loading the configuration
* from, and saving the configuration to, the database.
*
* @package core
* @category test
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_filterlib_testcase extends advanced_testcase {
class filterlib_test extends \advanced_testcase {
private function assert_only_one_filter_globally($filter, $state) {
global $DB;
@@ -40,9 +42,9 @@ class core_filterlib_testcase extends advanced_testcase {
$this->assertCount(1, $recs);
$rec = reset($recs);
unset($rec->id);
$expectedrec = new stdClass();
$expectedrec = new \stdClass();
$expectedrec->filter = $filter;
$expectedrec->contextid = context_system::instance()->id;
$expectedrec->contextid = \context_system::instance()->id;
$expectedrec->active = $state;
$expectedrec->sortorder = 1;
$this->assertEquals($expectedrec, $rec);
@@ -52,7 +54,7 @@ class core_filterlib_testcase extends advanced_testcase {
global $DB;
$sortedfilters = $DB->get_records_menu('filter_active',
array('contextid' => context_system::instance()->id), 'sortorder', 'sortorder,filter');
array('contextid' => \context_system::instance()->id), 'sortorder', 'sortorder,filter');
$testarray = array();
$index = 1;
foreach ($filters as $filter) {
@@ -93,7 +95,7 @@ class core_filterlib_testcase extends advanced_testcase {
public function test_global_config_exception_on_invalid_state() {
$this->resetAfterTest();
$this->expectException(coding_exception::class);
$this->expectException(\coding_exception::class);
filter_set_global_state('name', 0);
}
@@ -227,7 +229,7 @@ class core_filterlib_testcase extends advanced_testcase {
$rec = reset($recs);
unset($rec->id);
unset($rec->sortorder);
$expectedrec = new stdClass();
$expectedrec = new \stdClass();
$expectedrec->filter = $filter;
$expectedrec->contextid = $contextid;
$expectedrec->active = $state;
@@ -269,15 +271,15 @@ class core_filterlib_testcase extends advanced_testcase {
public function test_local_invalid_state_throws_exception() {
$this->resetAfterTest();
// Exercise SUT.
$this->expectException(coding_exception::class);
$this->expectException(\coding_exception::class);
filter_set_local_state('name', 123, -9999);
}
public function test_throws_exception_when_setting_global() {
$this->resetAfterTest();
// Exercise SUT.
$this->expectException(coding_exception::class);
filter_set_local_state('name', context_system::instance()->id, TEXTFILTER_INHERIT);
$this->expectException(\coding_exception::class);
filter_set_local_state('name', \context_system::instance()->id, TEXTFILTER_INHERIT);
}
public function test_local_inherit_deletes_existing() {
@@ -297,7 +299,7 @@ class core_filterlib_testcase extends advanced_testcase {
$this->assertEquals(1, count($recs), 'More than one record returned %s.');
$rec = reset($recs);
unset($rec->id);
$expectedrec = new stdClass();
$expectedrec = new \stdClass();
$expectedrec->filter = $filter;
$expectedrec->contextid = $context;
$expectedrec->name = $name;
@@ -341,9 +343,9 @@ class core_filterlib_testcase extends advanced_testcase {
protected function setup_available_in_context_tests() {
$course = $this->getDataGenerator()->create_course(array('category' => 1));
$childcontext = context_coursecat::instance(1);
$childcontext2 = context_course::instance($course->id);
$syscontext = context_system::instance();
$childcontext = \context_coursecat::instance(1);
$childcontext2 = \context_course::instance($course->id);
$syscontext = \context_system::instance();
return [
'syscontext' => $syscontext,
@@ -504,7 +506,7 @@ class core_filterlib_testcase extends advanced_testcase {
$this->assertEquals(1, count($filters), 'More than one record returned %s.');
$rec = $filters[$filter];
unset($rec->id);
$expectedrec = new stdClass();
$expectedrec = new \stdClass();
$expectedrec->filter = $filter;
$expectedrec->localstate = $localstate;
$expectedrec->inheritedstate = $inheritedstate;
@@ -563,19 +565,19 @@ class core_filterlib_testcase extends advanced_testcase {
'syscontext' => $syscontext
] = $this->setup_available_in_context_tests();
// Exercise SUT.
$this->expectException(coding_exception::class);
$this->expectException(\coding_exception::class);
filter_get_available_in_context($syscontext);
}
protected function setup_preload_activities_test() {
$syscontext = context_system::instance();
$catcontext = context_coursecat::instance(1);
$syscontext = \context_system::instance();
$catcontext = \context_coursecat::instance(1);
$course = $this->getDataGenerator()->create_course(array('category' => 1));
$coursecontext = context_course::instance($course->id);
$coursecontext = \context_course::instance($course->id);
$page1 = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
$activity1context = context_module::instance($page1->cmid);
$activity1context = \context_module::instance($page1->cmid);
$page2 = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
$activity2context = context_module::instance($page2->cmid);
$activity2context = \context_module::instance($page2->cmid);
return [
'syscontext' => $syscontext,
'catcontext' => $catcontext,
@@ -590,7 +592,7 @@ class core_filterlib_testcase extends advanced_testcase {
global $FILTERLIB_PRIVATE, $DB;
// Use preload cache...
$FILTERLIB_PRIVATE = new stdClass();
$FILTERLIB_PRIVATE = new \stdClass();
filter_preload_activities($modinfo);
// Get data and check no queries are made.
@@ -601,7 +603,7 @@ class core_filterlib_testcase extends advanced_testcase {
$this->assertEquals($before, $after);
// Repeat without cache and check it makes queries now.
$FILTERLIB_PRIVATE = new stdClass;
$FILTERLIB_PRIVATE = new \stdClass;
$before = $DB->perf_get_reads();
$filters1 = filter_get_active_in_context($activity1context);
$filters2 = filter_get_active_in_context($activity2context);
@@ -623,7 +625,7 @@ class core_filterlib_testcase extends advanced_testcase {
'activity2context' => $activity2context
] = $this->setup_preload_activities_test();
// Get course and modinfo.
$modinfo = new course_modinfo($course, 2);
$modinfo = new \course_modinfo($course, 2);
// Note: All the tests in this function check that the result from the
// preloaded cache is the same as the result from calling the standard
@@ -681,8 +683,8 @@ class core_filterlib_testcase extends advanced_testcase {
// Setup fixture.
filter_set_global_state('name', TEXTFILTER_ON);
filter_set_global_state('other', TEXTFILTER_ON);
filter_set_local_config('name', context_system::instance()->id, 'settingname', 'A value');
filter_set_local_config('other', context_system::instance()->id, 'settingname', 'Other value');
filter_set_local_config('name', \context_system::instance()->id, 'settingname', 'A value');
filter_set_local_config('other', \context_system::instance()->id, 'settingname', 'Other value');
set_config('configname', 'A config value', 'filter_name');
set_config('configname', 'Other config value', 'filter_other');
// Exercise SUT.
@@ -692,10 +694,10 @@ class core_filterlib_testcase extends advanced_testcase {
$this->assertTrue($DB->record_exists('filter_active', array('filter' => 'other')));
$this->assertEquals(1, $DB->count_records('filter_config'));
$this->assertTrue($DB->record_exists('filter_config', array('filter' => 'other')));
$expectedconfig = new stdClass;
$expectedconfig = new \stdClass;
$expectedconfig->configname = 'Other config value';
$this->assertEquals($expectedconfig, get_config('filter_other'));
$this->assertEquals(get_config('filter_name'), new stdClass());
$this->assertEquals(get_config('filter_name'), new \stdClass());
}
public function test_filter_delete_all_for_context() {
@@ -713,7 +715,7 @@ class core_filterlib_testcase extends advanced_testcase {
filter_delete_all_for_context(123);
// Validate.
$this->assertEquals(1, $DB->count_records('filter_active'));
$this->assertTrue($DB->record_exists('filter_active', array('contextid' => context_system::instance()->id)));
$this->assertTrue($DB->record_exists('filter_active', array('contextid' => \context_system::instance()->id)));
$this->assertEquals(1, $DB->count_records('filter_config'));
$this->assertTrue($DB->record_exists('filter_config', array('filter' => 'other')));
}
@@ -802,7 +804,7 @@ class core_filterlib_testcase extends advanced_testcase {
$active = filter_get_active_state('glossary');
$this->assertEquals($active, TEXTFILTER_ON);
$systemcontext = context_system::instance();
$systemcontext = \context_system::instance();
// Passing $systemcontext object.
$active = filter_get_active_state('glossary', $systemcontext);
$this->assertEquals($active, TEXTFILTER_ON);
@@ -834,17 +836,17 @@ class core_filterlib_testcase extends advanced_testcase {
$this->resetAfterTest();
filter_set_global_state('glossary', TEXTFILTER_ON);
$systemcontextid = context_system::instance()->id;
$systemcontextid = \context_system::instance()->id;
$active = filter_get_active_state('glossary', $systemcontextid);
$this->assertEquals($active, TEXTFILTER_ON);
filter_set_global_state('glossary', TEXTFILTER_OFF);
$systemcontextid = context_system::instance()->id;
$systemcontextid = \context_system::instance()->id;
$active = filter_get_active_state('glossary', $systemcontextid);
$this->assertEquals($active, TEXTFILTER_OFF);
filter_set_global_state('glossary', TEXTFILTER_DISABLED);
$systemcontextid = context_system::instance()->id;
$systemcontextid = \context_system::instance()->id;
$active = filter_get_active_state('glossary', $systemcontextid);
$this->assertEquals($active, TEXTFILTER_DISABLED);
}
+20 -4
View File
@@ -17,12 +17,21 @@
/**
* Unit tests for /lib/formslib.php.
*
* @package core_form
* @category phpunit
* @package core
* @category test
* @copyright 2011 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
use HTML_QuickForm_Rule_Range;
use moodleform;
use MoodleQuickForm_radio;
use MoodleQuickForm_Rule_Required;
use MoodleQuickForm_select;
use MoodleQuickForm_text;
defined('MOODLE_INTERNAL') || die();
global $CFG;
@@ -31,8 +40,15 @@ require_once($CFG->libdir . '/form/radio.php');
require_once($CFG->libdir . '/form/select.php');
require_once($CFG->libdir . '/form/text.php');
class core_formslib_testcase extends advanced_testcase {
/**
* Unit tests for /lib/formslib.php.
*
* @package core
* @category test
* @copyright 2011 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class formslib_test extends \advanced_testcase {
public function test_require_rule() {
global $CFG;
+3 -13
View File
@@ -14,27 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Test gd functionality.
*
* @package core
* @category phpunit
* @copyright 2015 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
namespace core;
/**
* A set of tests for some of the gd functionality within Moodle.
*
* @package core
* @category phpunit
* @category test
* @copyright 2015 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_gdlib_testcase extends basic_testcase {
class gdlib_test extends \basic_testcase {
private $fixturepath = null;
+6 -1
View File
@@ -14,6 +14,11 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core;
use Google_Service_Exception;
use Google_Service_YouTube;
/**
* Tests for google library
*
@@ -21,7 +26,7 @@
* @copyright 2021 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_googlelib_testcase extends advanced_testcase {
class googlelib_test extends \advanced_testcase {
public function test_invalid_google_api_key() {
global $CFG;
+28 -27
View File
@@ -14,28 +14,29 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for /lib/gradelib.php.
*
* @package core_grades
* @category phpunit
* @copyright 2012 Andrew Davis
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/gradelib.php');
class core_gradelib_testcase extends advanced_testcase {
/**
* Unit tests for /lib/gradelib.php.
*
* @package core
* @category test
* @copyright 2012 Andrew Davis
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class gradelib_test extends \advanced_testcase {
public function test_grade_update_mod_grades() {
$this->resetAfterTest(true);
// Create a broken module instance.
$modinstance = new stdClass();
$modinstance = new \stdClass();
$modinstance->modname = 'doesntexist';
$this->assertFalse(grade_update_mod_grades($modinstance));
@@ -67,17 +68,17 @@ class core_gradelib_testcase extends advanced_testcase {
$course = $this->getDataGenerator()->create_course();
$context = context_course::instance($course->id);
$context = \context_course::instance($course->id);
// Add a grade letter to the course.
$letter = new stdClass();
$letter = new \stdClass();
$letter->letter = 'M';
$letter->lowerboundary = '100';
$letter->contextid = $context->id;
$DB->insert_record('grade_letters', $letter);
// Pre-warm the cache, ensure that that the letter is cached.
$cache = cache::make('core', 'grade_letters');
$cache = \cache::make('core', 'grade_letters');
// Check that the cache is empty beforehand.
$letters = $cache->get($context->id);
@@ -108,13 +109,13 @@ class core_gradelib_testcase extends advanced_testcase {
$this->resetAfterTest();
$category = core_course_category::create(array('name' => 'Cat1'));
$category = \core_course_category::create(array('name' => 'Cat1'));
// Add a grade letter to the category.
$letter = new stdClass();
$letter = new \stdClass();
$letter->letter = 'M';
$letter->lowerboundary = '100';
$letter->contextid = context_coursecat::instance($category->id)->id;
$letter->contextid = \context_coursecat::instance($category->id)->id;
$DB->insert_record('grade_letters', $letter);
grade_course_category_delete($category->id, '', false);
@@ -138,11 +139,11 @@ class core_gradelib_testcase extends advanced_testcase {
// We need two grade items.
$params = ['idnumber' => 'g1', 'courseid' => $course->id];
$g1 = new grade_item($this->getDataGenerator()->create_grade_item($params));
$g1 = new \grade_item($this->getDataGenerator()->create_grade_item($params));
unset($params['idnumber']);
$g2 = new grade_item($this->getDataGenerator()->create_grade_item($params));
$g2 = new \grade_item($this->getDataGenerator()->create_grade_item($params));
$category = new grade_category($this->getDataGenerator()->create_grade_category($params));
$category = new \grade_category($this->getDataGenerator()->create_grade_category($params));
$catitem = $category->get_grade_item();
// Now set a calculation.
@@ -174,11 +175,11 @@ class core_gradelib_testcase extends advanced_testcase {
* Tests for the grade_get_date_for_user_grade function.
*
* @dataProvider grade_get_date_for_user_grade_provider
* @param stdClass $grade
* @param stdClass $user
* @param \stdClass $grade
* @param \stdClass $user
* @param int $expected
*/
public function test_grade_get_date_for_user_grade(stdClass $grade, stdClass $user, ?int $expected): void {
public function test_grade_get_date_for_user_grade(\stdClass $grade, \stdClass $user, ?int $expected): void {
$this->assertEquals($expected, grade_get_date_for_user_grade($grade, $user));
}
@@ -256,9 +257,9 @@ class core_gradelib_testcase extends advanced_testcase {
// Setup some basics.
$course = $this->getDataGenerator()->create_course();
$context = context_course::instance($course->id);
$context = \context_course::instance($course->id);
$cache = cache::make('core', 'grade_letters');
$cache = \cache::make('core', 'grade_letters');
$letters = $cache->get($context->id);
// Make sure the cache is empty.
@@ -281,16 +282,16 @@ class core_gradelib_testcase extends advanced_testcase {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$context = context_course::instance($course->id);
$context = \context_course::instance($course->id);
$cache = cache::make('core', 'grade_letters');
$cache = \cache::make('core', 'grade_letters');
$letters = $cache->get($context->id);
// Make sure the cache is empty.
$this->assertFalse($letters);
// Add a grade letter to the course.
$letter = new stdClass();
$letter = new \stdClass();
$letter->letter = 'M';
$letter->lowerboundary = '100';
$letter->contextid = $context->id;
@@ -14,15 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for the grade API at /lib/classes/grades_external.php
*
* @package core_grades
* @category external
* @copyright 2012 Andrew Davis
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.7
*/
namespace core;
use core_grades_external;
defined('MOODLE_INTERNAL') || die();
@@ -33,12 +27,14 @@ require_once($CFG->dirroot . '/webservice/tests/helpers.php');
/**
* Grades functions unit tests
*
* @package core_grades
* @category external
* Unit tests for the grade API at /lib/classes/grades_external.php
*
* @package core
* @category test
* @copyright 2012 Andrew Davis
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_grades_external_testcase extends externallib_advanced_testcase {
class grades_external_test extends \externallib_advanced_testcase {
/**
* Load initial test information
@@ -53,7 +49,7 @@ class core_grades_external_testcase extends externallib_advanced_testcase {
// Adds a course, a teacher, 2 students, an assignment and grades for the students.
$course = $this->getDataGenerator()->create_course();
$coursecontext = context_course::instance($course->id);
$coursecontext = \context_course::instance($course->id);
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
@@ -69,7 +65,7 @@ class core_grades_external_testcase extends externallib_advanced_testcase {
$parent = $this->getDataGenerator()->create_user();
$this->setUser($parent);
$student1context = context_user::instance($student1->id);
$student1context = \context_user::instance($student1->id);
// Creates a new role, gives it the capability and gives $USER that role.
$parentroleid = $this->assignUserCapability('moodle/grade:viewall', $student1context->id);
// Enrol the user in the course using the new role.
@@ -85,7 +81,7 @@ class core_grades_external_testcase extends externallib_advanced_testcase {
assign_grade_item_update($assignment, $studentgrades);
// Insert a custom grade scale to be used by an outcome.
$gradescale = new grade_scale();
$gradescale = new \grade_scale();
$gradescale->name = 'unittestscale3';
$gradescale->courseid = $course->id;
$gradescale->userid = 0;
@@ -94,15 +90,15 @@ class core_grades_external_testcase extends externallib_advanced_testcase {
$gradescale->insert();
// Insert an outcome.
$data = new stdClass();
$data = new \stdClass();
$data->courseid = $course->id;
$data->fullname = 'Team work';
$data->shortname = 'Team work';
$data->scaleid = $gradescale->id;
$outcome = new grade_outcome($data, false);
$outcome = new \grade_outcome($data, false);
$outcome->insert();
$outcomegradeitem = new grade_item();
$outcomegradeitem = new \grade_item();
$outcomegradeitem->itemname = $outcome->shortname;
$outcomegradeitem->itemtype = 'mod';
$outcomegradeitem->itemmodule = 'assign';
@@ -119,7 +115,7 @@ class core_grades_external_testcase extends externallib_advanced_testcase {
$outcomegradeitem->hidden = '';
$outcomegradeitem->insert();
$assignmentgradeitem = grade_item::fetch(
$assignmentgradeitem = \grade_item::fetch(
array(
'itemtype' => 'mod',
'itemmodule' => 'assign',
@@ -162,7 +158,7 @@ class core_grades_external_testcase extends externallib_advanced_testcase {
array(),
array('grademax' => $changedmax)
);
$result = external_api::clean_returnvalue(core_grades_external::update_grades_returns(), $result);
$result = \external_api::clean_returnvalue(core_grades_external::update_grades_returns(), $result);
$this->assertTrue($result == GRADE_UPDATE_OK);
$grades = grade_get_grades($course->id, 'mod', 'assign', $assignment->id);
$this->assertTrue($grades->items[0]->grademax == $changedmax);
@@ -177,7 +173,7 @@ class core_grades_external_testcase extends externallib_advanced_testcase {
0,
array(array('studentid' => $student1->id, 'grade' => $student1grade))
);
$result = external_api::clean_returnvalue(core_grades_external::update_grades_returns(), $result);
$result = \external_api::clean_returnvalue(core_grades_external::update_grades_returns(), $result);
$this->assertTrue($result == GRADE_UPDATE_OK);
$grades = grade_get_grades($course->id, 'mod', 'assign', $assignment->id, array($student1->id));
$this->assertTrue($grades->items[0]->grades[$student1->id]->grade == $student1grade);
@@ -196,7 +192,7 @@ class core_grades_external_testcase extends externallib_advanced_testcase {
array('studentid' => $student2->id, 'grade' => $student2grade)
)
);
$result = external_api::clean_returnvalue(core_grades_external::update_grades_returns(), $result);
$result = \external_api::clean_returnvalue(core_grades_external::update_grades_returns(), $result);
$this->assertTrue($result == GRADE_UPDATE_OK);
$grades = grade_get_grades($course->id, 'mod', 'assign', $assignment->id, array($student1->id, $student2->id));
$this->assertTrue($grades->items[0]->grades[$student1->id]->grade == $student1grade);
@@ -215,7 +211,7 @@ class core_grades_external_testcase extends externallib_advanced_testcase {
array( array('studentid' => $student1->id, 'grade' => $student1grade))
);
$this->fail('moodle_exception expected');
} catch (moodle_exception $ex) {
} catch (\moodle_exception $ex) {
$this->assertTrue(true);
}
@@ -232,7 +228,7 @@ class core_grades_external_testcase extends externallib_advanced_testcase {
array( array('studentid' => $student1->id, 'grade' => $student1grade))
);
$this->fail('moodle_exception expected');
} catch (moodle_exception $ex) {
} catch (\moodle_exception $ex) {
$this->assertTrue(true);
}
@@ -249,13 +245,13 @@ class core_grades_external_testcase extends externallib_advanced_testcase {
array('hidden' => 1)
);
$this->fail('moodle_exception expected');
} catch (moodle_exception $ex) {
} catch (\moodle_exception $ex) {
$this->assertTrue(true);
}
// Give the student role 'moodle/grade:hide' and they should now be able to hide the grade item.
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$coursecontext = context_course::instance($course->id);
$coursecontext = \context_course::instance($course->id);
assign_capability('moodle/grade:hide', CAP_ALLOW, $studentrole->id, $coursecontext->id);
accesslib_clear_all_caches_for_unit_testing();
@@ -272,7 +268,7 @@ class core_grades_external_testcase extends externallib_advanced_testcase {
array(),
array('hidden' => 1)
);
$result = external_api::clean_returnvalue(core_grades_external::update_grades_returns(), $result);
$result = \external_api::clean_returnvalue(core_grades_external::update_grades_returns(), $result);
$this->assertTrue($result == GRADE_UPDATE_OK);
$grades = grade_get_grades($course->id, 'mod', 'assign', $assignment->id);
$this->assertTrue($grades->items[0]->hidden == 1);
@@ -292,7 +288,7 @@ class core_grades_external_testcase extends externallib_advanced_testcase {
// Test the most basic gradecategory creation.
$status1 = core_grades_external::create_gradecategory($course->id, 'Test Category 1', []);
$courseparentcat = new grade_category(['courseid' => $course->id, 'depth' => 1], true);
$courseparentcat = new \grade_category(['courseid' => $course->id, 'depth' => 1], true);
$record1 = $DB->get_record('grade_categories', ['id' => $status1['categoryid']]);
$this->assertEquals('Test Category 1', $record1->fullname);
// Confirm that the parent category for this category is the top level category for the course.
@@ -331,7 +327,7 @@ class core_grades_external_testcase extends externallib_advanced_testcase {
];
$status3 = core_grades_external::create_gradecategory($course->id, 'Test Category 3', $customopts);
$cat3 = new grade_category(['courseid' => $course->id, 'id' => $status3['categoryid']], true);
$cat3 = new \grade_category(['courseid' => $course->id, 'id' => $status3['categoryid']], true);
$cat3->load_grade_item();
// Lets check all of the data is in the right shape.
@@ -14,6 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core;
use core_grading_external;
defined('MOODLE_INTERNAL') || die();
global $CFG;
@@ -23,12 +27,12 @@ require_once($CFG->dirroot . '/webservice/tests/helpers.php');
/**
* Unit tests for the grading API defined in core_grading_external class.
*
* @package core_grading
* @category external
* @package core
* @category test
* @copyright 2013 Paul Charsley
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_grading_externallib_testcase extends externallib_advanced_testcase {
class grading_external_test extends \externallib_advanced_testcase {
/**
* Test get_definitions
@@ -56,9 +60,9 @@ class core_grading_externallib_testcase extends externallib_advanced_testcase {
$enrolid = $DB->insert_record('enrol', $manualenroldata);
// Create a teacher and give them capabilities.
$coursecontext = context_course::instance($course->id);
$coursecontext = \context_course::instance($course->id);
$roleid = $this->assignUserCapability('moodle/course:viewparticipants', $coursecontext->id, 3);
$modulecontext = context_module::instance($cm->cmid);
$modulecontext = \context_module::instance($cm->cmid);
$this->assignUserCapability('mod/assign:grade', $modulecontext->id, $roleid);
// Create the teacher's enrolment record.
@@ -141,7 +145,7 @@ class core_grading_externallib_testcase extends externallib_advanced_testcase {
$cmids = array ($cm->cmid);
$areaname = 'submissions';
$result = core_grading_external::get_definitions($cmids, $areaname);
$result = external_api::clean_returnvalue(core_grading_external::get_definitions_returns(), $result);
$result = \external_api::clean_returnvalue(core_grading_external::get_definitions_returns(), $result);
$this->assertEquals(1, count($result['areas']));
$this->assertEquals(1, count($result['areas'][0]['definitions']));
@@ -200,9 +204,9 @@ class core_grading_externallib_testcase extends externallib_advanced_testcase {
$enrolid = $DB->insert_record('enrol', $manualenroldata);
// Create a teacher and give them capabilities.
$coursecontext = context_course::instance($course->id);
$coursecontext = \context_course::instance($course->id);
$roleid = $this->assignUserCapability('moodle/course:viewparticipants', $coursecontext->id, 3);
$modulecontext = context_module::instance($assign->cmid);
$modulecontext = \context_module::instance($assign->cmid);
$this->assignUserCapability('mod/assign:grade', $modulecontext->id, $roleid);
// Create the teacher's enrolment record.
@@ -213,7 +217,7 @@ class core_grading_externallib_testcase extends externallib_advanced_testcase {
// Create a student with an assignment grade.
$student = self::getDataGenerator()->create_user();
$assigngrade = new stdClass();
$assigngrade = new \stdClass();
$assigngrade->assignment = $assign->id;
$assigngrade->userid = $student->id;
$assigngrade->timecreated = time();
@@ -286,7 +290,7 @@ class core_grading_externallib_testcase extends externallib_advanced_testcase {
// Call the external function.
$result = core_grading_external::get_gradingform_instances($definitionid, 0);
$result = external_api::clean_returnvalue(core_grading_external::get_gradingform_instances_returns(), $result);
$result = \external_api::clean_returnvalue(core_grading_external::get_gradingform_instances_returns(), $result);
$this->assertEquals(1, count($result['instances']));
$this->assertEquals($USER->id, $result['instances'][0]['raterid']);
@@ -315,8 +319,8 @@ class core_grading_externallib_testcase extends externallib_advanced_testcase {
$params['course'] = $course->id;
$instance = $generator->create_instance($params);
$cm = get_coursemodule_from_instance('assign', $instance->id);
$context = context_module::instance($cm->id);
$coursecontext = context_course::instance($course->id);
$context = \context_module::instance($cm->id);
$coursecontext = \context_course::instance($course->id);
// Create the teacher.
$teacher = self::getDataGenerator()->create_user();
@@ -554,8 +558,8 @@ class core_grading_externallib_testcase extends externallib_advanced_testcase {
$params['course'] = $course->id;
$instance = $generator->create_instance($params);
$cm = get_coursemodule_from_instance('assign', $instance->id);
$context = context_module::instance($cm->id);
$coursecontext = context_course::instance($course->id);
$context = \context_module::instance($cm->id);
$coursecontext = \context_course::instance($course->id);
// Create the teacher.
$teacher = self::getDataGenerator()->create_user();
+14 -20
View File
@@ -14,23 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core;
/**
* Tests groups subsystems.
* Unit tests for lib/grouplib.php
*
* @package core_group
* @category phpunit
* @package core
* @copyright 2007 onwards Martin Dougiamas (http://dougiamas.com)
* @author Andrew Nicols
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Unit tests for lib/grouplib.php
* @group core_group
*/
class core_grouplib_testcase extends advanced_testcase {
class grouplib_test extends \advanced_testcase {
public function test_groups_get_group_by_idnumber() {
$this->resetAfterTest(true);
@@ -184,7 +178,7 @@ class core_grouplib_testcase extends advanced_testcase {
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$coursecontext = context_course::instance($course->id);
$coursecontext = \context_course::instance($course->id);
$student1 = $generator->create_user();
$student2 = $generator->create_user();
$plugin = enrol_get_plugin('manual');
@@ -376,7 +370,7 @@ class core_grouplib_testcase extends advanced_testcase {
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$coursecontext = context_course::instance($course->id);
$coursecontext = \context_course::instance($course->id);
$student1 = $generator->create_user();
$student2 = $generator->create_user();
$plugin = enrol_get_plugin('manual');
@@ -411,7 +405,7 @@ class core_grouplib_testcase extends advanced_testcase {
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$coursecontext = context_course::instance($course->id);
$coursecontext = \context_course::instance($course->id);
$student1 = $generator->create_user();
$student2 = $generator->create_user();
$plugin = enrol_get_plugin('manual');
@@ -444,7 +438,7 @@ class core_grouplib_testcase extends advanced_testcase {
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$coursecontext = context_course::instance($course->id);
$coursecontext = \context_course::instance($course->id);
$student1 = $generator->create_user();
$student2 = $generator->create_user();
$plugin = enrol_get_plugin('manual');
@@ -465,7 +459,7 @@ class core_grouplib_testcase extends advanced_testcase {
groups_add_member($group->id, $student1->id);
// Test with members at any group and with an invalid $context.
$syscontext = context_system::instance();
$syscontext = \context_system::instance();
$this->expectException('coding_exception');
list($sql, $params) = groups_get_members_ids_sql(USERSWITHOUTGROUP, $syscontext);
}
@@ -759,7 +753,7 @@ class core_grouplib_testcase extends advanced_testcase {
// Create a course category, course and groups.
$cat = $generator->create_category(array('parent' => 0));
$course = $generator->create_course(array('category' => $cat->id));
$coursecontext = context_course::instance($course->id);
$coursecontext = \context_course::instance($course->id);
$group1 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 1'));
$group2 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 2'));
$group3 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 3'));
@@ -1011,7 +1005,7 @@ class core_grouplib_testcase extends advanced_testcase {
// Generate data.
$course = $this->getDataGenerator()->create_course();
$record = new stdClass();
$record = new \stdClass();
$record->courseid = $course->id;
$group1 = $this->getDataGenerator()->create_group($record);
$group2 = $this->getDataGenerator()->create_group($record);
@@ -1245,7 +1239,7 @@ class core_grouplib_testcase extends advanced_testcase {
protected function make_group_list($number) {
$testgroups = array();
for ($a = 0; $a < $number; $a++) {
$grp = new stdClass();
$grp = new \stdClass();
$grp->id = 100 + $a;
$grp->name = 'test group ' . $grp->id;
$testgroups[$grp->id] = $grp;
@@ -1319,7 +1313,7 @@ class core_grouplib_testcase extends advanced_testcase {
// Create a course category, course and groups.
$cat = $generator->create_category(array('parent' => 0));
$course = $generator->create_course(array('category' => $cat->id));
$coursecontext = context_course::instance($course->id);
$coursecontext = \context_course::instance($course->id);
$group1 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 1'));
$group2 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 2'));
$group3 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 3'));
+4 -7
View File
@@ -14,22 +14,19 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core;
/**
* Tests our html2text hacks
*
* Note: includes original tests from testweblib.php
*
* @package core
* @category phpunit
* @category test
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
class core_html2text_testcase extends basic_testcase {
class html2text_test extends \basic_testcase {
/**
* ALT as image replacements.
+3 -13
View File
@@ -14,27 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for the HTMLPurifier integration
*
* @package core
* @category phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
namespace core;
/**
* HTMLPurifier test case
*
* @package core
* @category phpunit
* @category test
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_htmlpurifier_testcase extends basic_testcase {
class htmlpurifier_test extends \basic_testcase {
/**
* Verify _blank target is allowed.
+2 -9
View File
@@ -14,15 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains the test class testing the \core\ip_utils static helper class functions.
*
* @package core
* @copyright 2016 Jake Dallimore <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
namespace core;
/**
* This tests the static helper functions contained in the class '\core\ip_utils'.
@@ -31,7 +24,7 @@ defined('MOODLE_INTERNAL') || die();
* @copyright 2016 Jake Dallimore <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_ip_utils_testcase extends basic_testcase {
class ip_utils_test extends \basic_testcase {
/**
* Test for \core\ip_utils::is_domain_name().
*
+4 -12
View File
@@ -14,19 +14,11 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core;
/**
* Test jQuery integration.
*
* @package core
* @category phpunit
* @copyright 2014 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* This is not a complete jquery test, it just validates
* Moodle integration is set up properly.
*
@@ -34,11 +26,11 @@ defined('MOODLE_INTERNAL') || die();
* verify it actually works in browser.
*
* @package core
* @category phpunit
* @category test
* @copyright 2014 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_jquery_testcase extends basic_testcase {
class jquery_test extends \basic_testcase {
public function test_plugins_file() {
global $CFG;
+10 -9
View File
@@ -14,21 +14,22 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* ldap tests.
*
* @package core
* @category phpunit
* @copyright Damyon Wiese, Iñaki Arenaza 2014
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
namespace core;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/ldaplib.php');
class core_ldaplib_testcase extends advanced_testcase {
/**
* ldap tests.
*
* @package core
* @category test
* @copyright Damyon Wiese, Iñaki Arenaza 2014
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
class ldaplib_test extends \advanced_testcase {
public function test_ldap_addslashes() {
// See http://tools.ietf.org/html/rfc4514#section-5.2 if you want
+5 -15
View File
@@ -14,27 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* lock unit tests
*
* @package core
* @category lock
* @copyright 2013 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
namespace core;
/**
* Unit tests for our locking configuration.
*
* @package core
* @category lock
* @category test
* @copyright 2013 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class lock_config_testcase extends advanced_testcase {
class lock_config_test extends \advanced_testcase {
/**
* Tests the static parse charset method
@@ -62,7 +52,7 @@ class lock_config_testcase extends advanced_testcase {
try {
$factory = \core\lock\lock_config::get_lock_factory('cache');
$this->fail('Exception expected');
} catch (moodle_exception $ex) {
} catch (\moodle_exception $ex) {
$this->assertInstanceOf('coding_exception', $ex);
}
@@ -77,7 +67,7 @@ class lock_config_testcase extends advanced_testcase {
try {
$factory = \core\lock\lock_config::get_lock_factory('cache');
$this->fail('Exception expected');
} catch (moodle_exception $ex) {
} catch (\moodle_exception $ex) {
$this->assertInstanceOf('coding_exception', $ex);
}
+2 -12
View File
@@ -14,17 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* lock unit tests
*
* @package core
* @category test
* @copyright 2013 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
namespace core;
/**
* Unit tests for our locking implementations.
@@ -34,7 +24,7 @@ defined('MOODLE_INTERNAL') || die();
* @copyright 2013 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class lock_testcase extends advanced_testcase {
class lock_test extends \advanced_testcase {
/**
* Some lock types will store data in the database.
+4 -12
View File
@@ -14,19 +14,11 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core;
/**
* Test markdown text format.
*
* @package core
* @category phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* This is not a complete markdown test, it just validates
* Moodle integration works.
*
@@ -34,11 +26,11 @@ defined('MOODLE_INTERNAL') || die();
* for more format information.
*
* @package core
* @category phpunit
* @category test
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_markdown_testcase extends basic_testcase {
class markdown_test extends \basic_testcase {
public function test_paragraphs() {
$text = "one\n\ntwo";
+10 -8
View File
@@ -14,6 +14,15 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core;
use calc_formula;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/mathslib.php');
/**
* Unit tests of mathslib wrapper and underlying EvalMath library.
*
@@ -22,14 +31,7 @@
* @copyright 2007 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/mathslib.php');
class core_mathslib_testcase extends basic_testcase {
class mathslib_test extends \basic_testcase {
/**
* Tests the basic formula evaluation.
+38 -37
View File
@@ -14,6 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core;
use core_media_manager;
use media_test_plugin;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/fixtures/testable_core_media_player.php');
/**
* Test classes for handling embedded media (audio/video).
*
@@ -22,14 +30,7 @@
* @copyright 2012 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/fixtures/testable_core_media_player.php');
/**
* Test script for media embedding.
*/
class core_medialib_testcase extends advanced_testcase {
class medialib_test extends \advanced_testcase {
/**
* Pre-test setup. Preserves $CFG.
@@ -55,7 +56,7 @@ class core_medialib_testcase extends advanced_testcase {
*/
private function pretend_to_be_safari() {
// Pretend to be using Safari browser (must support mp4 for tests to work).
core_useragent::instance(true, 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; de-at) ' .
\core_useragent::instance(true, 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; de-at) ' .
'AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1');
}
@@ -64,7 +65,7 @@ class core_medialib_testcase extends advanced_testcase {
*/
private function pretend_to_be_firefox() {
// Pretend to be using Firefox browser (must support ogg for tests to work).
core_useragent::instance(true, 'Mozilla/5.0 (X11; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0 ');
\core_useragent::instance(true, 'Mozilla/5.0 (X11; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0 ');
}
/**
@@ -73,11 +74,11 @@ class core_medialib_testcase extends advanced_testcase {
public function test_get_filename() {
$manager = core_media_manager::instance();
$this->assertSame('frog.mp4', $manager->get_filename(new moodle_url(
$this->assertSame('frog.mp4', $manager->get_filename(new \moodle_url(
'/pluginfile.php/312/mod_page/content/7/frog.mp4')));
// This should work even though slasharguments is true, because we want
// it to support 'legacy' links if somebody toggles the option later.
$this->assertSame('frog.mp4', $manager->get_filename(new moodle_url(
$this->assertSame('frog.mp4', $manager->get_filename(new \moodle_url(
'/pluginfile.php?file=/312/mod_page/content/7/frog.mp4')));
}
@@ -87,13 +88,13 @@ class core_medialib_testcase extends advanced_testcase {
public function test_get_extension() {
$manager = core_media_manager::instance();
$this->assertSame('mp4', $manager->get_extension(new moodle_url(
$this->assertSame('mp4', $manager->get_extension(new \moodle_url(
'/pluginfile.php/312/mod_page/content/7/frog.mp4')));
$this->assertSame('', $manager->get_extension(new moodle_url(
$this->assertSame('', $manager->get_extension(new \moodle_url(
'/pluginfile.php/312/mod_page/content/7/frog')));
$this->assertSame('mp4', $manager->get_extension(new moodle_url(
$this->assertSame('mp4', $manager->get_extension(new \moodle_url(
'/pluginfile.php?file=/312/mod_page/content/7/frog.mp4')));
$this->assertSame('', $manager->get_extension(new moodle_url(
$this->assertSame('', $manager->get_extension(new \moodle_url(
'/pluginfile.php?file=/312/mod_page/content/7/frog')));
}
@@ -104,9 +105,9 @@ class core_medialib_testcase extends advanced_testcase {
$test = new media_test_plugin(1, 13, ['tst', 'test']);
// Some example URLs.
$supported1 = new moodle_url('http://example.org/1.test');
$supported2 = new moodle_url('http://example.org/2.TST');
$unsupported = new moodle_url('http://example.org/2.jpg');
$supported1 = new \moodle_url('http://example.org/1.test');
$supported2 = new \moodle_url('http://example.org/2.TST');
$unsupported = new \moodle_url('http://example.org/2.jpg');
// No URLs => none.
$result = $test->list_supported_urls(array());
@@ -163,7 +164,7 @@ class core_medialib_testcase extends advanced_testcase {
*/
public function test_can_embed_url() {
// All players are initially disabled, so mp4 cannot be rendered.
$url = new moodle_url('http://example.org/test.mp4');
$url = new \moodle_url('http://example.org/test.mp4');
$manager = core_media_manager::instance();
$this->assertFalse($manager->can_embed_url($url));
@@ -195,7 +196,7 @@ class core_medialib_testcase extends advanced_testcase {
$link = 'mediafallbacklink';
$test = 'mediaplugin_test';
$url = new moodle_url('http://example.org/test.mp4');
$url = new \moodle_url('http://example.org/test.mp4');
// All plugins disabled, NOLINK option.
\core\plugininfo\media::set_enabled_plugins('');
@@ -219,7 +220,7 @@ class core_medialib_testcase extends advanced_testcase {
$mediaformats = array('mp3', 'mp4');
foreach ($mediaformats as $format) {
$url = new moodle_url('http://example.org/test.' . $format);
$url = new \moodle_url('http://example.org/test.' . $format);
$textwithlink = $manager->embed_url($url);
$textwithoutlink = $manager->embed_url($url, 0, 0, '', array(core_media_manager::OPTION_NO_LINK => true));
@@ -262,12 +263,12 @@ class core_medialib_testcase extends advanced_testcase {
$manager = core_media_manager::instance();
// Without any options...
$url = new moodle_url('http://example.org/test.swf');
$url = new \moodle_url('http://example.org/test.swf');
$t = $manager->embed_url($url);
$this->assertStringNotContainsString('</object>', $t);
// ...and with the 'no it's safe, I checked it' option.
$url = new moodle_url('http://example.org/test.swf');
$url = new \moodle_url('http://example.org/test.swf');
$t = $manager->embed_url($url, '', 0, 0, array(core_media_manager::OPTION_TRUSTED => true));
$this->assertStringNotContainsString('</object>', $t);
}
@@ -286,7 +287,7 @@ class core_medialib_testcase extends advanced_testcase {
$manager = core_media_manager::instance();
// Format: mp3.
$url = new moodle_url('http://example.org/pluginfile.php?file=x/y/z/test.mp3');
$url = new \moodle_url('http://example.org/pluginfile.php?file=x/y/z/test.mp3');
$t = $manager->embed_url($url);
$this->assertStringContainsString('</audio>', $t);
}
@@ -303,13 +304,13 @@ class core_medialib_testcase extends advanced_testcase {
$options = array(core_media_manager::OPTION_FALLBACK_TO_BLANK => true);
// Embed that does match something should still include the link too.
$url = new moodle_url('http://example.org/test.ogg');
$url = new \moodle_url('http://example.org/test.ogg');
$t = $manager->embed_url($url, '', 0, 0, $options);
$this->assertStringContainsString('</audio>', $t);
$this->assertStringContainsString('mediafallbacklink', $t);
// Embed that doesn't match something should be totally blank.
$url = new moodle_url('http://example.org/test.mp4');
$url = new \moodle_url('http://example.org/test.mp4');
$t = $manager->embed_url($url, '', 0, 0, $options);
$this->assertSame('', $t);
}
@@ -328,7 +329,7 @@ class core_medialib_testcase extends advanced_testcase {
// through.
\core\plugininfo\media::set_enabled_plugins('html5video');
$manager = core_media_manager::instance();
$url = new moodle_url('http://example.org/test.mp4');
$url = new \moodle_url('http://example.org/test.mp4');
// HTML5 default size - specifies core width and does not specify height.
$t = $manager->embed_url($url);
@@ -341,7 +342,7 @@ class core_medialib_testcase extends advanced_testcase {
$this->assertStringContainsString('height="101"', $t);
// HTML5 size specified in url, overrides call.
$url = new moodle_url('http://example.org/test.mp4?d=123x456');
$url = new \moodle_url('http://example.org/test.mp4?d=123x456');
$t = $manager->embed_url($url, '', '666', '101');
$this->assertStringContainsString('width="123"', $t);
$this->assertStringContainsString('height="456"', $t);
@@ -357,7 +358,7 @@ class core_medialib_testcase extends advanced_testcase {
// html5video.
\core\plugininfo\media::set_enabled_plugins('html5video');
$manager = core_media_manager::instance();
$url = new moodle_url('http://example.org/test.mp4');
$url = new \moodle_url('http://example.org/test.mp4');
// HTML5 default name - use filename.
$t = $manager->embed_url($url);
@@ -414,10 +415,10 @@ class core_medialib_testcase extends advanced_testcase {
// MP4, OGV, WebM and FLV.
$urls = array(
new moodle_url('http://example.org/test.mp4'),
new moodle_url('http://example.org/test.ogv'),
new moodle_url('http://example.org/test.webm'),
new moodle_url('http://example.org/test.flv'),
new \moodle_url('http://example.org/test.mp4'),
new \moodle_url('http://example.org/test.ogv'),
new \moodle_url('http://example.org/test.webm'),
new \moodle_url('http://example.org/test.flv'),
);
// Enable html5 and "test" ("test" first).
@@ -452,14 +453,14 @@ class core_medialib_testcase extends advanced_testcase {
* Make sure the instance() method returns singleton for the same page and different object for another page
*/
public function test_initialise() {
$moodlepage1 = new moodle_page();
$moodlepage1 = new \moodle_page();
$mediamanager1 = core_media_manager::instance($moodlepage1);
$mediamanager2 = core_media_manager::instance($moodlepage1);
$this->assertSame($mediamanager1, $mediamanager2);
$moodlepage3 = new moodle_page();
$moodlepage3 = new \moodle_page();
$mediamanager3 = core_media_manager::instance($moodlepage3);
$this->assertNotSame($mediamanager1, $mediamanager3);
@@ -473,7 +474,7 @@ class core_medialib_testcase extends advanced_testcase {
* @return string Comma-separated list of players
*/
public function get_players_test($manager) {
$method = new ReflectionMethod("core_media_manager", "get_players");
$method = new \ReflectionMethod("core_media_manager", "get_players");
$method->setAccessible(true);
$players = $method->invoke($manager);
$out = '';
+35 -44
View File
@@ -14,28 +14,19 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Test classes for \core\message\message.
*
* @package core_message
* @category test
* @copyright 2015 onwards Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
namespace core;
/**
* Test script for message class.
*
* @package core_message
* Test classes for \core\message\message.
*
* @package core
* @category test
* @copyright 2015 onwards Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_message_testcase extends advanced_testcase {
class message_test extends \advanced_testcase {
/**
* Test the method get_eventobject_for_processor().
@@ -68,8 +59,8 @@ class core_message_testcase extends advanced_testcase {
$message->set_additional_content('test', $content);
// Create a file instance.
$usercontext = context_user::instance($user->id);
$file = new stdClass;
$usercontext = \context_user::instance($user->id);
$file = new \stdClass;
$file->contextid = $usercontext->id;
$file->component = 'user';
$file->filearea = 'private';
@@ -82,49 +73,49 @@ class core_message_testcase extends advanced_testcase {
$file = $fs->create_file_from_string($file, 'file1 content');
$message->attachment = $file;
$stdclass = $message->get_eventobject_for_processor('test');
$stdClass = $message->get_eventobject_for_processor('test');
$this->assertSame($message->courseid, $stdclass->courseid);
$this->assertSame($message->component, $stdclass->component);
$this->assertSame($message->name, $stdclass->name);
$this->assertSame($message->userfrom, $stdclass->userfrom);
$this->assertSame($message->userto, $stdclass->userto);
$this->assertSame($message->subject, $stdclass->subject);
$this->assertSame(' test ' . $message->fullmessage . ' test ', $stdclass->fullmessage);
$this->assertSame(' test ' . $message->fullmessagehtml . ' test ', $stdclass->fullmessagehtml);
$this->assertSame(' test ' . $message->smallmessage . ' test ', $stdclass->smallmessage);
$this->assertSame($message->notification, $stdclass->notification);
$this->assertSame($message->contexturl, $stdclass->contexturl);
$this->assertSame($message->contexturlname, $stdclass->contexturlname);
$this->assertSame($message->replyto, $stdclass->replyto);
$this->assertSame($message->replytoname, $stdclass->replytoname);
$this->assertSame($message->attachname, $stdclass->attachname);
$this->assertSame($message->courseid, $stdClass->courseid);
$this->assertSame($message->component, $stdClass->component);
$this->assertSame($message->name, $stdClass->name);
$this->assertSame($message->userfrom, $stdClass->userfrom);
$this->assertSame($message->userto, $stdClass->userto);
$this->assertSame($message->subject, $stdClass->subject);
$this->assertSame(' test ' . $message->fullmessage . ' test ', $stdClass->fullmessage);
$this->assertSame(' test ' . $message->fullmessagehtml . ' test ', $stdClass->fullmessagehtml);
$this->assertSame(' test ' . $message->smallmessage . ' test ', $stdClass->smallmessage);
$this->assertSame($message->notification, $stdClass->notification);
$this->assertSame($message->contexturl, $stdClass->contexturl);
$this->assertSame($message->contexturlname, $stdClass->contexturlname);
$this->assertSame($message->replyto, $stdClass->replyto);
$this->assertSame($message->replytoname, $stdClass->replytoname);
$this->assertSame($message->attachname, $stdClass->attachname);
// Extra content for fullmessage only.
$content = array('fullmessage' => array('header' => ' test ', 'footer' => ' test '));
$message->set_additional_content('test', $content);
$stdclass = $message->get_eventobject_for_processor('test');
$this->assertSame(' test ' . $message->fullmessage . ' test ', $stdclass->fullmessage);
$this->assertSame($message->fullmessagehtml, $stdclass->fullmessagehtml);
$this->assertSame($message->smallmessage, $stdclass->smallmessage);
$stdClass = $message->get_eventobject_for_processor('test');
$this->assertSame(' test ' . $message->fullmessage . ' test ', $stdClass->fullmessage);
$this->assertSame($message->fullmessagehtml, $stdClass->fullmessagehtml);
$this->assertSame($message->smallmessage, $stdClass->smallmessage);
// Extra content for fullmessagehtml and smallmessage only.
$content = array('fullmessagehtml' => array('header' => ' test ', 'footer' => ' test '),
'smallmessage' => array('header' => ' testsmall ', 'footer' => ' testsmall '));
$message->set_additional_content('test', $content);
$stdclass = $message->get_eventobject_for_processor('test');
$this->assertSame($message->fullmessage, $stdclass->fullmessage);
$this->assertSame(' test ' . $message->fullmessagehtml . ' test ', $stdclass->fullmessagehtml);
$this->assertSame(' testsmall ' . $message->smallmessage . ' testsmall ', $stdclass->smallmessage);
$stdClass = $message->get_eventobject_for_processor('test');
$this->assertSame($message->fullmessage, $stdClass->fullmessage);
$this->assertSame(' test ' . $message->fullmessagehtml . ' test ', $stdClass->fullmessagehtml);
$this->assertSame(' testsmall ' . $message->smallmessage . ' testsmall ', $stdClass->smallmessage);
// Extra content for * and smallmessage.
$content = array('*' => array('header' => ' test ', 'footer' => ' test '),
'smallmessage' => array('header' => ' testsmall ', 'footer' => ' testsmall '));
$message->set_additional_content('test', $content);
$stdclass = $message->get_eventobject_for_processor('test');
$this->assertSame(' test ' . $message->fullmessage . ' test ', $stdclass->fullmessage);
$this->assertSame(' test ' . $message->fullmessagehtml . ' test ', $stdclass->fullmessagehtml);
$this->assertSame(' testsmall ' . ' test ' . $message->smallmessage . ' test ' . ' testsmall ', $stdclass->smallmessage);
$stdClass = $message->get_eventobject_for_processor('test');
$this->assertSame(' test ' . $message->fullmessage . ' test ', $stdClass->fullmessage);
$this->assertSame(' test ' . $message->fullmessagehtml . ' test ', $stdClass->fullmessagehtml);
$this->assertSame(' testsmall ' . ' test ' . $message->smallmessage . ' test ' . ' testsmall ', $stdClass->smallmessage);
}
/**
+15 -11
View File
@@ -15,27 +15,31 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Test script for message class.
*
* Test classes for \core\message\inbound.
*
* @package core_message
* @package core
* @category test
* @copyright 2015 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
namespace core;
global $CFG;
defined('MOODLE_INTERNAL') || die();
/**
* Test script for message class.
*
* @package core_message
* Test classes for \core\message\inbound.
*
* @package core
* @category test
* @copyright 2015 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_messageinbound_testcase extends advanced_testcase {
class messageinbound_test extends \advanced_testcase {
/**
* @dataProvider message_inbound_handler_trim_testprovider
@@ -43,9 +47,9 @@ class core_messageinbound_testcase extends advanced_testcase {
public function test_messageinbound_handler_trim($file, $source, $expectedplain, $expectedhtml) {
$this->resetAfterTest();
$mime = Horde_Mime_Part::parseMessage($source);
$mime = \Horde_Mime_Part::parseMessage($source);
if ($plainpartid = $mime->findBody('plain')) {
$messagedata = new stdClass();
$messagedata = new \stdClass();
$messagedata->plain = $mime->getPart($plainpartid)->getContents();
$messagedata->html = '';
@@ -58,7 +62,7 @@ class core_messageinbound_testcase extends advanced_testcase {
}
if ($htmlpartid = $mime->findBody('html')) {
$messagedata = new stdClass();
$messagedata = new \stdClass();
$messagedata->plain = '';
$messagedata->html = $mime->getPart($htmlpartid)->getContents();
@@ -128,7 +132,7 @@ class core_messageinbound_testcase extends advanced_testcase {
}
if (null === $section) {
if (!isset($sections[$token])) {
throw new coding_exception(sprintf(
throw new \coding_exception(sprintf(
'The test file "%s" should not contain a section named "%s".',
basename($file),
$token
@@ -143,7 +147,7 @@ class core_messageinbound_testcase extends advanced_testcase {
}
foreach ($sections as $section => $required) {
if ($required && !isset($data[$section])) {
throw new coding_exception(sprintf(
throw new \coding_exception(sprintf(
'The test file "%s" must have a section named "%s".',
str_replace($fixturesdir.'/', '', $file),
$section
@@ -167,5 +171,5 @@ class test_handler extends \core\message\inbound\handler {
public function get_description() {}
public function process_message(stdClass $record, stdClass $messagedata) {}
public function process_message(\stdClass $record, \stdClass $messagedata) {}
}
+10 -11
View File
@@ -14,22 +14,21 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* core_minify related tests.
*
* @package core
* @category phpunit
* @copyright 2013 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
namespace core;
use core_minify;
/**
* Class core_minify_testcase.
*
* core_minify related tests.
*
* @package core
* @category test
* @copyright 2013 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_minify_testcase extends advanced_testcase {
class minify_test extends \advanced_testcase {
public function test_css() {
$css = "
body {
+58 -47
View File
@@ -18,20 +18,31 @@
* Tests for the moodle_page class.
*
* @package core
* @category phpunit
* @category test
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \moodle_page
*/
namespace core;
use moodle_page;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/pagelib.php');
require_once($CFG->libdir . '/blocklib.php');
class core_moodle_page_testcase extends advanced_testcase {
/**
* Tests for the moodle_page class.
*
* @package core
* @category test
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \moodle_page
*/
class moodle_page_test extends \advanced_testcase {
/**
* @var testable_moodle_page
@@ -53,7 +64,7 @@ class core_moodle_page_testcase extends advanced_testcase {
public function test_setting_course_works() {
// Setup fixture.
$course = $this->getDataGenerator()->create_course();
$this->testpage->set_context(context_system::instance()); // Avoid trying to set the context.
$this->testpage->set_context(\context_system::instance()); // Avoid trying to set the context.
// Exercise SUT.
$this->testpage->set_course($course);
// Validated.
@@ -64,7 +75,7 @@ class core_moodle_page_testcase extends advanced_testcase {
global $COURSE, $PAGE;
// Setup fixture.
$course = $this->getDataGenerator()->create_course();
$this->testpage->set_context(context_system::instance()); // Avoid trying to set the context.
$this->testpage->set_context(\context_system::instance()); // Avoid trying to set the context.
$PAGE = $this->testpage;
// Exercise SUT.
$this->testpage->set_course($course);
@@ -77,7 +88,7 @@ class core_moodle_page_testcase extends advanced_testcase {
$originalcourse = $COURSE;
// Setup fixture.
$course = $this->getDataGenerator()->create_course();
$this->testpage->set_context(context_system::instance()); // Avoid trying to set the context.
$this->testpage->set_context(\context_system::instance()); // Avoid trying to set the context.
// Exercise SUT.
$this->testpage->set_course($course);
// Validated.
@@ -86,38 +97,38 @@ class core_moodle_page_testcase extends advanced_testcase {
public function test_cannot_set_course_once_theme_set() {
// Setup fixture.
$this->testpage->force_theme(theme_config::DEFAULT_THEME);
$this->testpage->force_theme(\theme_config::DEFAULT_THEME);
$course = $this->getDataGenerator()->create_course();
// Exercise SUT.
$this->expectException(coding_exception::class);
$this->expectException(\coding_exception::class);
$this->testpage->set_course($course);
}
public function test_cannot_set_category_once_theme_set() {
// Setup fixture.
$this->testpage->force_theme(theme_config::DEFAULT_THEME);
$this->testpage->force_theme(\theme_config::DEFAULT_THEME);
// Exercise SUT.
$this->expectException(coding_exception::class);
$this->expectException(\coding_exception::class);
$this->testpage->set_category_by_id(123);
}
public function test_cannot_set_category_once_course_set() {
// Setup fixture.
$course = $this->getDataGenerator()->create_course();
$this->testpage->set_context(context_system::instance()); // Avoid trying to set the context.
$this->testpage->set_context(\context_system::instance()); // Avoid trying to set the context.
$this->testpage->set_course($course);
// Exercise SUT.
$this->expectException(coding_exception::class);
$this->expectException(\coding_exception::class);
$this->testpage->set_category_by_id(123);
}
public function test_categories_array_empty_for_front_page() {
global $SITE;
// Setup fixture.
$this->testpage->set_context(context_system::instance()); // Avoid trying to set the context.
$this->testpage->set_context(\context_system::instance()); // Avoid trying to set the context.
$this->testpage->set_course($SITE);
// Exercise SUT and validate.
$this->assertEquals(array(), $this->testpage->categories);
@@ -125,25 +136,25 @@ class core_moodle_page_testcase extends advanced_testcase {
public function test_set_state_normal_path() {
$course = $this->getDataGenerator()->create_course();
$this->testpage->set_context(context_system::instance());
$this->testpage->set_context(\context_system::instance());
$this->testpage->set_course($course);
$this->assertEquals(moodle_page::STATE_BEFORE_HEADER, $this->testpage->state);
$this->assertEquals(\moodle_page::STATE_BEFORE_HEADER, $this->testpage->state);
$this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER);
$this->assertEquals(moodle_page::STATE_PRINTING_HEADER, $this->testpage->state);
$this->testpage->set_state(\moodle_page::STATE_PRINTING_HEADER);
$this->assertEquals(\moodle_page::STATE_PRINTING_HEADER, $this->testpage->state);
$this->testpage->set_state(moodle_page::STATE_IN_BODY);
$this->assertEquals(moodle_page::STATE_IN_BODY, $this->testpage->state);
$this->testpage->set_state(\moodle_page::STATE_IN_BODY);
$this->assertEquals(\moodle_page::STATE_IN_BODY, $this->testpage->state);
$this->testpage->set_state(moodle_page::STATE_DONE);
$this->assertEquals(moodle_page::STATE_DONE, $this->testpage->state);
$this->testpage->set_state(\moodle_page::STATE_DONE);
$this->assertEquals(\moodle_page::STATE_DONE, $this->testpage->state);
}
public function test_set_state_cannot_skip_one() {
// Exercise SUT.
$this->expectException(coding_exception::class);
$this->testpage->set_state(moodle_page::STATE_IN_BODY);
$this->expectException(\coding_exception::class);
$this->testpage->set_state(\moodle_page::STATE_IN_BODY);
}
public function test_header_printed_false_initially() {
@@ -153,12 +164,12 @@ class core_moodle_page_testcase extends advanced_testcase {
public function test_header_printed_becomes_true() {
$course = $this->getDataGenerator()->create_course();
$this->testpage->set_context(context_system::instance());
$this->testpage->set_context(\context_system::instance());
$this->testpage->set_course($course);
// Exercise SUT.
$this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER);
$this->testpage->set_state(moodle_page::STATE_IN_BODY);
$this->testpage->set_state(\moodle_page::STATE_PRINTING_HEADER);
$this->testpage->set_state(\moodle_page::STATE_IN_BODY);
// Validated.
$this->assertTrue($this->testpage->headerprinted);
}
@@ -166,7 +177,7 @@ class core_moodle_page_testcase extends advanced_testcase {
public function test_set_context() {
// Setup fixture.
$course = $this->getDataGenerator()->create_course();
$context = context_course::instance($course->id);
$context = \context_course::instance($course->id);
// Exercise SUT.
$this->testpage->set_context($context);
// Validated.
@@ -269,7 +280,7 @@ class core_moodle_page_testcase extends advanced_testcase {
public function test_set_url_using_moodle_url() {
global $CFG;
// Fixture setup.
$url = new moodle_url('/mod/workshop/allocation.php', array('cmid' => 29, 'method' => 'manual'));
$url = new \moodle_url('/mod/workshop/allocation.php', array('cmid' => 29, 'method' => 'manual'));
// Exercise SUT.
$this->testpage->set_url($url);
// Validated.
@@ -336,7 +347,7 @@ class core_moodle_page_testcase extends advanced_testcase {
public function test_setting_course_sets_context() {
// Setup fixture.
$course = $this->getDataGenerator()->create_course();
$context = context_course::instance($course->id);
$context = \context_course::instance($course->id);
// Exercise SUT.
$this->testpage->set_course($course);
@@ -354,7 +365,7 @@ class core_moodle_page_testcase extends advanced_testcase {
$this->testpage->set_category_by_id($cat->id);
// Validated.
$this->assertEquals($catdbrecord, $this->testpage->category);
$this->assertSame(context_coursecat::instance($cat->id), $this->testpage->context);
$this->assertSame(\context_coursecat::instance($cat->id), $this->testpage->context);
}
public function test_set_nested_categories() {
@@ -395,7 +406,7 @@ class core_moodle_page_testcase extends advanced_testcase {
$forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
$cm = get_coursemodule_from_id('forum', $forum->cmid);
// Exercise SUT.
$this->expectException(coding_exception::class);
$this->expectException(\coding_exception::class);
$this->testpage->set_activity_record($forum);
}
@@ -407,7 +418,7 @@ class core_moodle_page_testcase extends advanced_testcase {
// Exercise SUT.
$this->testpage->set_cm($cm);
// Validated.
$this->assertSame(context_module::instance($cm->id), $this->testpage->context);
$this->assertSame(\context_module::instance($cm->id), $this->testpage->context);
}
public function test_activity_record_loaded_if_not_set() {
@@ -443,7 +454,7 @@ class core_moodle_page_testcase extends advanced_testcase {
$this->testpage->set_cm($cm);
// Exercise SUT.
$forum->course = 13;
$this->expectException(coding_exception::class);
$this->expectException(\coding_exception::class);
$this->testpage->set_activity_record($forum);
}
@@ -455,7 +466,7 @@ class core_moodle_page_testcase extends advanced_testcase {
$this->testpage->set_cm($cm);
// Exercise SUT.
$forum->id = 13;
$this->expectException(coding_exception::class);
$this->expectException(\coding_exception::class);
$this->testpage->set_activity_record($forum);
}
@@ -492,7 +503,7 @@ class core_moodle_page_testcase extends advanced_testcase {
$cm = get_coursemodule_from_id('forum', $forum->cmid);
// Exercise SUT.
$cm->course = 13;
$this->expectException(coding_exception::class);
$this->expectException(\coding_exception::class);
$this->testpage->set_cm($cm, $course);
}
@@ -514,7 +525,7 @@ class core_moodle_page_testcase extends advanced_testcase {
// Setup fixture.
global $USER;
$this->testpage->set_context(context_system::instance());
$this->testpage->set_context(\context_system::instance());
$this->setAdminUser();
$USER->editing = true;
@@ -529,7 +540,7 @@ class core_moodle_page_testcase extends advanced_testcase {
// Setup fixture.
global $USER;
$this->testpage->set_context(context_system::instance());
$this->testpage->set_context(\context_system::instance());
$this->setAdminUser();
$USER->editing = false;
@@ -538,7 +549,7 @@ class core_moodle_page_testcase extends advanced_testcase {
}
public function test_default_editing_capabilities() {
$this->testpage->set_context(context_system::instance());
$this->testpage->set_context(\context_system::instance());
$this->setAdminUser();
// Validated.
@@ -546,7 +557,7 @@ class core_moodle_page_testcase extends advanced_testcase {
}
public function test_other_block_editing_cap() {
$this->testpage->set_context(context_system::instance());
$this->testpage->set_context(\context_system::instance());
$this->setAdminUser();
// Exercise SUT.
@@ -556,7 +567,7 @@ class core_moodle_page_testcase extends advanced_testcase {
}
public function test_other_editing_cap() {
$this->testpage->set_context(context_system::instance());
$this->testpage->set_context(\context_system::instance());
$this->setAdminUser();
// Exercise SUT.
@@ -568,7 +579,7 @@ class core_moodle_page_testcase extends advanced_testcase {
}
public function test_other_editing_caps() {
$this->testpage->set_context(context_system::instance());
$this->testpage->set_context(\context_system::instance());
$this->setAdminUser();
// Exercise SUT.
@@ -607,7 +618,7 @@ class core_moodle_page_testcase extends advanced_testcase {
try {
$this->testpage->get_renderer('core', 'monkeys');
$this->fail('Request for renderer with invalid component didn\'t throw expected exception.');
} catch (coding_exception $exception) {
} catch (\coding_exception $exception) {
$this->assertEquals('monkeys', $exception->debuginfo);
}
@@ -644,7 +655,7 @@ class core_moodle_page_testcase extends advanced_testcase {
try {
$this->testpage->get_renderer('core', 'monkeys');
$this->fail('Request for renderer with invalid component didn\'t throw expected exception.');
} catch (coding_exception $exception) {
} catch (\coding_exception $exception) {
$this->assertEquals('monkeys', $exception->debuginfo);
}
@@ -673,7 +684,7 @@ class core_moodle_page_testcase extends advanced_testcase {
set_config('allowuserthemes', 1);
set_config('allowcohortthemes', 1);
$systemctx = context_system::instance();
$systemctx = \context_system::instance();
set_config('theme', $sitetheme);
// Create user.
@@ -772,7 +783,7 @@ class core_moodle_page_testcase extends advanced_testcase {
public function test_user_can_edit_blocks() {
global $DB;
$systemcontext = context_system::instance();
$systemcontext = \context_system::instance();
$this->testpage->set_context($systemcontext);
$user = $this->getDataGenerator()->create_user();
@@ -795,7 +806,7 @@ class core_moodle_page_testcase extends advanced_testcase {
* @covers ::force_lock_all_blocks()
*/
public function test_force_lock_all_blocks() {
$this->testpage->set_context(context_system::instance());
$this->testpage->set_context(\context_system::instance());
$this->setAdminUser();
// Confirm admin user has access to edit blocks.
+49 -56
View File
@@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core;
/**
* Tests for moodle_url.
*
@@ -21,67 +23,58 @@
* @copyright 2018 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Tests for moodle_url.
*
* @copyright 2018 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_moodle_url_testcase extends advanced_testcase {
class moodle_url_test extends \advanced_testcase {
/**
* Test basic moodle_url construction.
*/
public function test_moodle_url_constructor() {
global $CFG;
$url = new moodle_url('/index.php');
$url = new \moodle_url('/index.php');
$this->assertSame($CFG->wwwroot.'/index.php', $url->out());
$url = new moodle_url('/index.php', array());
$url = new \moodle_url('/index.php', array());
$this->assertSame($CFG->wwwroot.'/index.php', $url->out());
$url = new moodle_url('/index.php', array('id' => 2));
$url = new \moodle_url('/index.php', array('id' => 2));
$this->assertSame($CFG->wwwroot.'/index.php?id=2', $url->out());
$url = new moodle_url('/index.php', array('id' => 'two'));
$url = new \moodle_url('/index.php', array('id' => 'two'));
$this->assertSame($CFG->wwwroot.'/index.php?id=two', $url->out());
$url = new moodle_url('/index.php', array('id' => 1, 'cid' => '2'));
$url = new \moodle_url('/index.php', array('id' => 1, 'cid' => '2'));
$this->assertSame($CFG->wwwroot.'/index.php?id=1&amp;cid=2', $url->out());
$this->assertSame($CFG->wwwroot.'/index.php?id=1&cid=2', $url->out(false));
$url = new moodle_url('/index.php', null, 'test');
$url = new \moodle_url('/index.php', null, 'test');
$this->assertSame($CFG->wwwroot.'/index.php#test', $url->out());
$url = new moodle_url('/index.php', array('id' => 2), 'test');
$url = new \moodle_url('/index.php', array('id' => 2), 'test');
$this->assertSame($CFG->wwwroot.'/index.php?id=2#test', $url->out());
}
/**
* Tests moodle_url::get_path().
* Tests \moodle_url::get_path().
*/
public function test_moodle_url_get_path() {
$url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
$url = new \moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
$this->assertSame('/my/file/is/here.txt', $url->get_path());
$url = new moodle_url('http://www.example.org/');
$url = new \moodle_url('http://www.example.org/');
$this->assertSame('/', $url->get_path());
$url = new moodle_url('http://www.example.org/pluginfile.php/slash/arguments');
$url = new \moodle_url('http://www.example.org/pluginfile.php/slash/arguments');
$this->assertSame('/pluginfile.php/slash/arguments', $url->get_path());
$this->assertSame('/pluginfile.php', $url->get_path(false));
}
public function test_moodle_url_round_trip() {
$strurl = 'http://moodle.org/course/view.php?id=5';
$url = new moodle_url($strurl);
$url = new \moodle_url($strurl);
$this->assertSame($strurl, $url->out(false));
$strurl = 'http://moodle.org/user/index.php?contextid=53&sifirst=M&silast=D';
$url = new moodle_url($strurl);
$url = new \moodle_url($strurl);
$this->assertSame($strurl, $url->out(false));
}
@@ -90,23 +83,23 @@ class core_moodle_url_testcase extends advanced_testcase {
*/
public function test_moodle_url_empty_param_values() {
$strurl = 'http://moodle.org/course/view.php?id=0';
$url = new moodle_url($strurl, array('id' => 0));
$url = new \moodle_url($strurl, array('id' => 0));
$this->assertSame($strurl, $url->out(false));
$strurl = 'http://moodle.org/course/view.php?id';
$url = new moodle_url($strurl, array('id' => false));
$url = new \moodle_url($strurl, array('id' => false));
$this->assertSame($strurl, $url->out(false));
$strurl = 'http://moodle.org/course/view.php?id';
$url = new moodle_url($strurl, array('id' => null));
$url = new \moodle_url($strurl, array('id' => null));
$this->assertSame($strurl, $url->out(false));
$strurl = 'http://moodle.org/course/view.php?id';
$url = new moodle_url($strurl, array('id' => ''));
$url = new \moodle_url($strurl, array('id' => ''));
$this->assertSame($strurl, $url->out(false));
$strurl = 'http://moodle.org/course/view.php?id';
$url = new moodle_url($strurl);
$url = new \moodle_url($strurl);
$this->assertSame($strurl, $url->out(false));
}
@@ -114,7 +107,7 @@ class core_moodle_url_testcase extends advanced_testcase {
* Test set good scheme on Moodle URL objects.
*/
public function test_moodle_url_set_good_scheme() {
$url = new moodle_url('http://moodle.org/foo/bar');
$url = new \moodle_url('http://moodle.org/foo/bar');
$url->set_scheme('myscheme');
$this->assertSame('myscheme://moodle.org/foo/bar', $url->out());
}
@@ -123,46 +116,46 @@ class core_moodle_url_testcase extends advanced_testcase {
* Test set bad scheme on Moodle URL objects.
*/
public function test_moodle_url_set_bad_scheme() {
$url = new moodle_url('http://moodle.org/foo/bar');
$this->expectException(coding_exception::class);
$url = new \moodle_url('http://moodle.org/foo/bar');
$this->expectException(\coding_exception::class);
$url->set_scheme('not a valid $ scheme');
}
public function test_moodle_url_round_trip_array_params() {
$strurl = 'http://example.com/?a%5B1%5D=1&a%5B2%5D=2';
$url = new moodle_url($strurl);
$url = new \moodle_url($strurl);
$this->assertSame($strurl, $url->out(false));
$url = new moodle_url('http://example.com/?a[1]=1&a[2]=2');
$url = new \moodle_url('http://example.com/?a[1]=1&a[2]=2');
$this->assertSame($strurl, $url->out(false));
// For un-keyed array params, we expect 0..n keys to be returned.
$strurl = 'http://example.com/?a%5B0%5D=0&a%5B1%5D=1';
$url = new moodle_url('http://example.com/?a[]=0&a[]=1');
$url = new \moodle_url('http://example.com/?a[]=0&a[]=1');
$this->assertSame($strurl, $url->out(false));
}
public function test_compare_url() {
$url1 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2));
$url2 = new moodle_url('index2.php', array('var1' => 1, 'var2' => 2, 'var3' => 3));
$url1 = new \moodle_url('index.php', array('var1' => 1, 'var2' => 2));
$url2 = new \moodle_url('index2.php', array('var1' => 1, 'var2' => 2, 'var3' => 3));
$this->assertFalse($url1->compare($url2, URL_MATCH_BASE));
$this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS));
$this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
$url2 = new moodle_url('index.php', array('var1' => 1, 'var3' => 3));
$url2 = new \moodle_url('index.php', array('var1' => 1, 'var3' => 3));
$this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
$this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS));
$this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
$url2 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2, 'var3' => 3));
$url2 = new \moodle_url('index.php', array('var1' => 1, 'var2' => 2, 'var3' => 3));
$this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
$this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
$this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
$url2 = new moodle_url('index.php', array('var2' => 2, 'var1' => 1));
$url2 = new \moodle_url('index.php', array('var2' => 2, 'var1' => 1));
$this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
$this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
@@ -182,26 +175,26 @@ class core_moodle_url_testcase extends advanced_testcase {
public function test_out_as_local_url() {
global $CFG;
// Test http url.
$url1 = new moodle_url('/lib/tests/weblib_test.php');
$url1 = new \moodle_url('/lib/tests/weblib_test.php');
$this->assertSame('/lib/tests/weblib_test.php', $url1->out_as_local_url());
// Test https url.
$httpswwwroot = str_replace("http://", "https://", $CFG->wwwroot);
$url2 = new moodle_url($httpswwwroot.'/login/profile.php');
$url2 = new \moodle_url($httpswwwroot.'/login/profile.php');
$this->assertSame('/login/profile.php', $url2->out_as_local_url());
// Test http url matching wwwroot.
$url3 = new moodle_url($CFG->wwwroot);
$url3 = new \moodle_url($CFG->wwwroot);
$this->assertSame('', $url3->out_as_local_url());
// Test http url matching wwwroot ending with slash (/).
$url3 = new moodle_url($CFG->wwwroot.'/');
$url3 = new \moodle_url($CFG->wwwroot.'/');
$this->assertSame('/', $url3->out_as_local_url());
}
public function test_out_as_local_url_error() {
$url2 = new moodle_url('http://www.google.com/lib/tests/weblib_test.php');
$this->expectException(coding_exception::class);
$url2 = new \moodle_url('http://www.google.com/lib/tests/weblib_test.php');
$this->expectException(\coding_exception::class);
$url2->out_as_local_url();
}
@@ -212,8 +205,8 @@ class core_moodle_url_testcase extends advanced_testcase {
global $CFG;
$modifiedurl = $CFG->wwwroot.'1';
$url3 = new moodle_url($modifiedurl.'/login/profile.php');
$this->expectException(coding_exception::class);
$url3 = new \moodle_url($modifiedurl.'/login/profile.php');
$this->expectException(\coding_exception::class);
$url3->out_as_local_url();
}
@@ -221,38 +214,38 @@ class core_moodle_url_testcase extends advanced_testcase {
* Try get local url from external https url and you should get error
*/
public function test_https_out_as_local_url_error() {
$url4 = new moodle_url('https://www.google.com/lib/tests/weblib_test.php');
$this->expectException(coding_exception::class);
$url4 = new \moodle_url('https://www.google.com/lib/tests/weblib_test.php');
$this->expectException(\coding_exception::class);
$url4->out_as_local_url();
}
public function test_moodle_url_get_scheme() {
// Should return the scheme only.
$url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
$url = new \moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
$this->assertSame('http', $url->get_scheme());
// Should work for secure URLs.
$url = new moodle_url('https://www.example.org:447/my/file/is/here.txt?really=1');
$url = new \moodle_url('https://www.example.org:447/my/file/is/here.txt?really=1');
$this->assertSame('https', $url->get_scheme());
// Should return an empty string if no scheme is specified.
$url = new moodle_url('www.example.org:447/my/file/is/here.txt?really=1');
$url = new \moodle_url('www.example.org:447/my/file/is/here.txt?really=1');
$this->assertSame('', $url->get_scheme());
}
public function test_moodle_url_get_host() {
// Should return the host part only.
$url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
$url = new \moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
$this->assertSame('www.example.org', $url->get_host());
}
public function test_moodle_url_get_port() {
// Should return the port if one provided.
$url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
$url = new \moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
$this->assertSame(447, $url->get_port());
// Should return an empty string if port not specified.
$url = new moodle_url('http://www.example.org/some/path/here.php');
$url = new \moodle_url('http://www.example.org/some/path/here.php');
$this->assertSame('', $url->get_port());
}
@@ -270,7 +263,7 @@ class core_moodle_url_testcase extends advanced_testcase {
$this->resetAfterTest();
$CFG->slasharguments = $slashargs;
$url = call_user_func_array('moodle_url::make_pluginfile_url', $args);
$url = call_user_func_array('\moodle_url::make_pluginfile_url', $args);
$this->assertMatchesRegularExpression($expected, $url->out(true));
}
+12 -18
View File
@@ -14,13 +14,7 @@
// 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 myprofilelib apis.
*
* @package core
* @copyright 2015 onwards Ankit agarwal <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
namespace core;
defined('MOODLE_INTERNAL') || die();
@@ -34,7 +28,7 @@ require_once($CFG->dirroot . '/lib/myprofilelib.php');
* @copyright 2015 onwards Ankit agarwal <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
class core_myprofilelib_testcase extends advanced_testcase {
class myprofilelib_test extends \advanced_testcase {
/**
* @var stdClass The user.
@@ -72,7 +66,7 @@ class core_myprofilelib_testcase extends advanced_testcase {
// Test tree as admin user.
core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
$reflector = new ReflectionObject($this->tree);
$reflector = new \ReflectionObject($this->tree);
$categories = $reflector->getProperty('categories');
$categories->setAccessible(true);
$cats = $categories->getValue($this->tree);
@@ -97,7 +91,7 @@ class core_myprofilelib_testcase extends advanced_testcase {
$iscurrentuser = false;
core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
$reflector = new ReflectionObject($this->tree);
$reflector = new \ReflectionObject($this->tree);
$nodes = $reflector->getProperty('nodes');
$nodes->setAccessible(true);
$this->assertArrayNotHasKey('fullprofile', $nodes->getValue($this->tree));
@@ -111,7 +105,7 @@ class core_myprofilelib_testcase extends advanced_testcase {
$iscurrentuser = true;
core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
$reflector = new ReflectionObject($this->tree);
$reflector = new \ReflectionObject($this->tree);
$nodes = $reflector->getProperty('nodes');
$nodes->setAccessible(true);
$this->assertArrayHasKey('editprofile', $nodes->getValue($this->tree));
@@ -125,7 +119,7 @@ class core_myprofilelib_testcase extends advanced_testcase {
$iscurrentuser = false;
core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
$reflector = new ReflectionObject($this->tree);
$reflector = new \ReflectionObject($this->tree);
$nodes = $reflector->getProperty('nodes');
$nodes->setAccessible(true);
$this->assertArrayHasKey('editprofile', $nodes->getValue($this->tree));
@@ -139,7 +133,7 @@ class core_myprofilelib_testcase extends advanced_testcase {
$iscurrentuser = false;
core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
$reflector = new ReflectionObject($this->tree);
$reflector = new \ReflectionObject($this->tree);
$nodes = $reflector->getProperty('nodes');
$nodes->setAccessible(true);
$this->assertArrayHasKey('preferences', $nodes->getValue($this->tree));
@@ -156,7 +150,7 @@ class core_myprofilelib_testcase extends advanced_testcase {
$iscurrentuser = false;
core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
$reflector = new ReflectionObject($this->tree);
$reflector = new \ReflectionObject($this->tree);
$nodes = $reflector->getProperty('nodes');
$nodes->setAccessible(true);
$this->assertArrayNotHasKey('loginas', $nodes->getValue($this->tree));
@@ -194,7 +188,7 @@ class core_myprofilelib_testcase extends advanced_testcase {
// User with proper permissions.
core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, null);
$reflector = new ReflectionObject($this->tree);
$reflector = new \ReflectionObject($this->tree);
$nodes = $reflector->getProperty('nodes');
$nodes->setAccessible(true);
foreach ($hiddenfields as $field) {
@@ -219,7 +213,7 @@ class core_myprofilelib_testcase extends advanced_testcase {
// User without permission.
$this->setUser($this->user2);
core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, null);
$reflector = new ReflectionObject($this->tree);
$reflector = new \ReflectionObject($this->tree);
$nodes = $reflector->getProperty('nodes');
$nodes->setAccessible(true);
foreach ($hiddenfields as $field) {
@@ -240,7 +234,7 @@ class core_myprofilelib_testcase extends advanced_testcase {
$iscurrentuser = false;
core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, null);
$reflector = new ReflectionObject($this->tree);
$reflector = new \ReflectionObject($this->tree);
$nodes = $reflector->getProperty('nodes');
$nodes->setAccessible(true);
$this->assertArrayHasKey('firstaccess', $nodes->getValue($this->tree));
@@ -259,7 +253,7 @@ class core_myprofilelib_testcase extends advanced_testcase {
$iscurrentuser = false;
core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, null);
$reflector = new ReflectionObject($this->tree);
$reflector = new \ReflectionObject($this->tree);
$nodes = $reflector->getProperty('nodes');
$nodes->setAccessible(true);
$this->assertArrayNotHasKey('firstaccess', $nodes->getValue($this->tree));
+41 -22
View File
@@ -18,18 +18,37 @@
* Unit tests for lib/navigationlib.php
*
* @package core
* @category phpunit
* @category test
* @copyright 2009 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5)
*/
namespace core;
use action_link;
use global_navigation;
use navbar;
use navigation_cache;
use navigation_node;
use navigation_node_collection;
use pix_icon;
use popup_action;
use settings_navigation;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/navigationlib.php');
class core_navigationlib_testcase extends advanced_testcase {
/**
* Unit tests for lib/navigationlib.php
*
* @package core
* @category test
* @copyright 2009 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5)
*/
class navigationlib_test extends \advanced_testcase {
/**
* @var navigation_node
*/
@@ -42,7 +61,7 @@ class core_navigationlib_testcase extends advanced_testcase {
$PAGE->set_course($SITE);
$activeurl = $PAGE->url;
$inactiveurl = new moodle_url('http://www.moodle.com/');
$inactiveurl = new \moodle_url('http://www.moodle.com/');
navigation_node::override_active_url($PAGE->url);
@@ -71,7 +90,7 @@ class core_navigationlib_testcase extends advanced_testcase {
'shorttext' => 'A very silly extra long short text string, more than 25 characters',
'key' => 'key',
'type' => 'navigation_node::TYPE_COURSE',
'action' => new moodle_url('http://www.moodle.org/'));
'action' => new \moodle_url('http://www.moodle.org/'));
$node = new navigation_node($fakeproperties);
$this->assertSame($fakeproperties['text'], $node->text);
@@ -252,7 +271,7 @@ class core_navigationlib_testcase extends advanced_testcase {
$this->setup_node();
$node1 = $this->node->add('active node 1', null, navigation_node::TYPE_CUSTOM, null, 'anode1');
$node2 = $this->node->add('active node 2', new moodle_url($CFG->wwwroot), navigation_node::TYPE_COURSE, null, 'anode2');
$node2 = $this->node->add('active node 2', new \moodle_url($CFG->wwwroot), navigation_node::TYPE_COURSE, null, 'anode2');
$node1->make_active();
$this->node->get('anode2')->make_active();
$this->assertTrue($node1->isactive);
@@ -345,9 +364,9 @@ class core_navigationlib_testcase extends advanced_testcase {
$cat2 = $generator->create_category(array('parent' => $cat1->id));
$course = $generator->create_course(array('category' => $cat2->id));
$page = new moodle_page();
$page = new \moodle_page();
$page->set_course($course);
$page->set_url(new moodle_url('/course/view.php', array('id' => $course->id)));
$page->set_url(new \moodle_url('/course/view.php', array('id' => $course->id)));
$page->navbar->prepend('test 1');
$page->navbar->prepend('test 2');
$page->navbar->add('test 3');
@@ -376,7 +395,7 @@ class core_navigationlib_testcase extends advanced_testcase {
* @depends test_navbar_prepend_and_add
* @param $node
*/
public function test_navbar_has_items(moodle_page $page) {
public function test_navbar_has_items(\moodle_page $page) {
$this->resetAfterTest();
$this->assertTrue($page->navbar->has_items());
@@ -619,14 +638,14 @@ class core_navigationlib_testcase extends advanced_testcase {
return [
'The navigation node has an action link.' =>
[
navigation_node::create('Node', new action_link(new moodle_url('/'), '',
new popup_action('click', new moodle_url('/'))), navigation_node::TYPE_SETTING),
navigation_node::create('Node', new action_link(new \moodle_url('/'), '',
new popup_action('click', new \moodle_url('/'))), navigation_node::TYPE_SETTING),
true
],
'The navigation node does not have an action link.' =>
[
navigation_node::create('Node', new moodle_url('/'), navigation_node::TYPE_SETTING),
navigation_node::create('Node', new \moodle_url('/'), navigation_node::TYPE_SETTING),
false
],
];
@@ -672,17 +691,17 @@ class core_navigationlib_testcase extends advanced_testcase {
return [
'The navigation node has an action link with an action attached.' =>
[
navigation_node::create('Node', new action_link(new moodle_url('/'), '',
new popup_action('click', new moodle_url('/'))), navigation_node::TYPE_SETTING),
navigation_node::create('Node', new action_link(new \moodle_url('/'), '',
new popup_action('click', new \moodle_url('/'))), navigation_node::TYPE_SETTING),
],
'The navigation node has an action link without an action.' =>
[
navigation_node::create('Node', new action_link(new moodle_url('/'), '', null),
navigation_node::create('Node', new action_link(new \moodle_url('/'), '', null),
navigation_node::TYPE_SETTING),
],
'The navigation node does not have an action link.' =>
[
navigation_node::create('Node', new moodle_url('/'), navigation_node::TYPE_SETTING),
navigation_node::create('Node', new \moodle_url('/'), navigation_node::TYPE_SETTING),
],
];
}
@@ -695,7 +714,7 @@ class core_navigationlib_testcase extends advanced_testcase {
*/
class exposed_global_navigation extends global_navigation {
protected $exposedkey = 'exposed_';
public function __construct(moodle_page $page=null) {
public function __construct(\moodle_page $page=null) {
global $PAGE;
if ($page === null) {
$page = $PAGE;
@@ -710,7 +729,7 @@ class exposed_global_navigation extends global_navigation {
if (method_exists($this, $method)) {
return call_user_func_array(array($this, $method), $arguments);
}
throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
throw new \coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
}
public function set_initialised() {
$this->initialised = true;
@@ -753,7 +772,7 @@ class mock_initialise_global_navigation extends global_navigation {
*/
class exposed_navbar extends navbar {
protected $exposedkey = 'exposed_';
public function __construct(moodle_page $page) {
public function __construct(\moodle_page $page) {
parent::__construct($page);
$this->cache = new navigation_cache('unittest_nav');
}
@@ -764,11 +783,11 @@ class exposed_navbar extends navbar {
if (method_exists($this, $method)) {
return call_user_func_array(array($this, $method), $arguments);
}
throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
throw new \coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
}
}
class navigation_exposed_moodle_page extends moodle_page {
class navigation_exposed_moodle_page extends \moodle_page {
public function set_navigation(navigation_node $node) {
$this->_navigation = $node;
}
@@ -792,6 +811,6 @@ class exposed_settings_navigation extends settings_navigation {
if (method_exists($this, $method)) {
return call_user_func_array(array($this, $method), $arguments);
}
throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
throw new \coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
}
}
+6 -16
View File
@@ -14,39 +14,29 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for core\notification.
*
* @package core
* @category phpunit
* @copyright 2016 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
namespace core;
/**
* Unit tests for core\notification.
*
* @package core
* @category phpunit
* @category phpunit
* @category test
* @copyright 2016 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_notification_testcase extends advanced_testcase {
class notification_test extends \advanced_testcase {
/**
* Setup required for all notification tests.
*
* This includes emptying the list of notifications on the session, resetting any session which exists, and setting
* up a new moodle_page object.
* up a new \moodle_page object.
*/
public function setUp(): void {
global $PAGE, $SESSION;
parent::setUp();
$PAGE = new moodle_page();
$PAGE = new \moodle_page();
\core\session\manager::init_empty_session();
$SESSION->notifications = [];
}
@@ -55,7 +45,7 @@ class core_notification_testcase extends advanced_testcase {
* Tear down required for all notification tests.
*
* This includes emptying the list of notifications on the session, resetting any session which exists, and setting
* up a new moodle_page object.
* up a new \moodle_page object.
*/
public function tearDown(): void {
global $PAGE, $SESSION;
@@ -14,21 +14,16 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for lib/classes/output/mustache_helper_collection
*
* @copyright 2019 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
use core\output\mustache_helper_collection;
namespace core\output;
/**
* Unit tests for the mustache_helper_collection class.
*
* @package core
* @copyright 2019 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_output_mustache_helper_collection_testcase extends advanced_testcase {
class mustache_helper_collection_test extends \advanced_testcase {
/**
* Test cases to confirm that disallowed helpers are stripped from the source
* text by the helper before being passed to other another helper. This prevents
@@ -14,24 +14,20 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\output;
/**
* Unit tests for lib/classes/output/mustache_template_finder.php
*
* Unit tests for the Mustache template finder class (contains logic about
* resolving mustache template locations.
*
* @package core
* @category phpunit
* @category test
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
use core\output\mustache_template_finder;
/**
* Unit tests for the Mustache template finder class (contains logic about
* resolving mustache template locations.
*/
class core_output_mustache_template_finder_testcase extends advanced_testcase {
class mustache_template_finder_test extends \advanced_testcase {
/**
* Data provider which reutrns a set of valid template directories to be used when testing
@@ -105,7 +101,7 @@ class core_output_mustache_template_finder_testcase extends advanced_testcase {
*/
public function test_invalid_component_get_template_directories_for_component() {
// Test something invalid.
$this->expectException(coding_exception::class);
$this->expectException(\coding_exception::class);
mustache_template_finder::get_template_directories_for_component('octopus', 'classic');
}
@@ -192,7 +188,7 @@ class core_output_mustache_template_finder_testcase extends advanced_testcase {
* Tests for get_template_filepath when dealing with an invalid component.
*/
public function test_invalid_component_get_template_filepath() {
$this->expectException(moodle_exception::class);
$this->expectException(\moodle_exception::class);
mustache_template_finder::get_template_filepath('core/octopus', 'classic');
}
}
@@ -14,26 +14,18 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\output;
/**
* Unit tests for the Mustache source loader class.
*
* Unit tests for lib/classes/output/mustache_template_source_loader.php
*
* @package core
* @copyright 2018 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
use core\output\mustache_template_source_loader;
/**
* Unit tests for the Mustache source loader class.
*
* @package core
* @copyright 2018 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_output_mustache_template_source_loader_testcase extends advanced_testcase {
class mustache_template_source_loader_test extends \advanced_testcase {
/**
* Ensure that stripping comments from templates does not mutilate the template body.
*/
@@ -130,7 +122,7 @@ TBD;
TBC;
$loader = new mustache_template_source_loader();
$actual = phpunit_util::call_internal_method(
$actual = \phpunit_util::call_internal_method(
$loader,
'strip_template_comments',
[$templatewithcomment],
@@ -502,7 +494,7 @@ TEMPLATE;
* @param string $expected The expected output
*/
public function test_scan_template_source_for_dependencies($loader, $source, $expected) {
$actual = phpunit_util::call_internal_method(
$actual = \phpunit_util::call_internal_method(
$loader,
'scan_template_source_for_dependencies',
[$source],
+44 -36
View File
@@ -14,14 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for lib/outputcomponents.php.
*
* @package core
* @category phpunit
* @copyright 2011 David Mudrak <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
use block_contents;
use custom_menu;
use custom_menu_item;
use paging_bar;
use renderer_base;
use single_select;
use theme_config;
use url_select;
use user_picture;
defined('MOODLE_INTERNAL') || die();
@@ -29,9 +32,14 @@ global $CFG;
require_once($CFG->libdir . '/outputcomponents.php');
/**
* Unit tests for the user_picture class.
* Unit tests for lib/outputcomponents.php.
*
* @package core
* @category test
* @copyright 2011 David Mudrak <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_outputcomponents_testcase extends advanced_testcase {
class outputcomponents_test extends \advanced_testcase {
/**
* Tests user_picture::fields.
@@ -77,7 +85,7 @@ class core_outputcomponents_testcase extends advanced_testcase {
$fields = implode(',', \core_user\fields::get_picture_fields());
$fields = array_map('trim', explode(',', $fields));
$fakerecord = new stdClass();
$fakerecord = new \stdClass();
$fakerecord->aliasedid = 42;
foreach ($fields as $field) {
if ($field !== 'id') {
@@ -104,7 +112,7 @@ class core_outputcomponents_testcase extends advanced_testcase {
$fields = implode(',', \core_user\fields::get_picture_fields());
$fields = array_map('trim', explode(',', $fields));
$fakerecord = new stdClass();
$fakerecord = new \stdClass();
$fakerecord->aliasedid = 42;
foreach ($fields as $field) {
if ($field !== 'id') {
@@ -144,25 +152,25 @@ class core_outputcomponents_testcase extends advanced_testcase {
$this->assertSame('mm', $CFG->gravatardefaulturl);
// Create some users.
$page = new moodle_page();
$page = new \moodle_page();
$page->set_url('/user/profile.php');
$page->set_context(context_system::instance());
$page->set_context(\context_system::instance());
$renderer = $page->get_renderer('core');
$user1 = $this->getDataGenerator()->create_user(array('picture'=>11, 'email'=>'[email protected]'));
$context1 = context_user::instance($user1->id);
$context1 = \context_user::instance($user1->id);
$user2 = $this->getDataGenerator()->create_user(array('picture'=>0, 'email'=>'[email protected]'));
$context2 = context_user::instance($user2->id);
$context2 = \context_user::instance($user2->id);
// User 3 is deleted.
$user3 = $this->getDataGenerator()->create_user(array('picture'=>1, 'deleted'=>1, 'email'=>'[email protected]'));
$this->assertNotEmpty(context_user::instance($user3->id));
$this->assertNotEmpty(\context_user::instance($user3->id));
$this->assertEquals(0, $user3->picture);
$this->assertNotEquals('[email protected]', $user3->email);
// User 4 is incorrectly deleted with its context deleted as well (testing legacy code).
$user4 = $this->getDataGenerator()->create_user(['picture' => 1, 'deleted' => 1, 'email' => '[email protected]']);
context_helper::delete_instance(CONTEXT_USER, $user4->id);
\context_helper::delete_instance(CONTEXT_USER, $user4->id);
$this->assertEquals(0, $user4->picture);
$this->assertNotEquals('[email protected]', $user4->email);
@@ -173,7 +181,7 @@ class core_outputcomponents_testcase extends advanced_testcase {
$user1->picture = 11;
// Try valid user with picture when user context is not cached - 1 query expected.
context_helper::reset_caches();
\context_helper::reset_caches();
$reads = $DB->perf_get_reads();
$up1 = new user_picture($user1);
$this->assertEquals($reads, $DB->perf_get_reads());
@@ -182,7 +190,7 @@ class core_outputcomponents_testcase extends advanced_testcase {
// Try valid user with contextid hint - no queries expected.
$user1->contextid = $context1->id;
context_helper::reset_caches();
\context_helper::reset_caches();
$reads = $DB->perf_get_reads();
$up1 = new user_picture($user1);
$this->assertEquals($reads, $DB->perf_get_reads());
@@ -190,7 +198,7 @@ class core_outputcomponents_testcase extends advanced_testcase {
$this->assertEquals($reads, $DB->perf_get_reads());
// Try valid user without image - no queries expected.
context_helper::reset_caches();
\context_helper::reset_caches();
$reads = $DB->perf_get_reads();
$up2 = new user_picture($user2);
$this->assertEquals($reads, $DB->perf_get_reads());
@@ -199,7 +207,7 @@ class core_outputcomponents_testcase extends advanced_testcase {
// Try guessing of deleted users - no queries expected.
unset($user3->deleted);
context_helper::reset_caches();
\context_helper::reset_caches();
$reads = $DB->perf_get_reads();
$up3 = new user_picture($user3);
$this->assertEquals($reads, $DB->perf_get_reads());
@@ -275,9 +283,9 @@ class core_outputcomponents_testcase extends advanced_testcase {
// Now test gravatar with one theme having own images (afterburner).
// $this->assertFileExists("$CFG->dirroot/theme/afterburner/config.php");
// set_config('theme', 'afterburner');
// $page = new moodle_page();
// $page = new \moodle_page();
// $page->set_url('/user/profile.php');
// $page->set_context(context_system::instance());
// $page->set_context(\context_system::instance());
// $renderer = $page->get_renderer('core');
// $up2 = new user_picture($user2);
@@ -291,9 +299,9 @@ class core_outputcomponents_testcase extends advanced_testcase {
// set_config('enablegravatar', 0);
// $this->assertFileExists("$CFG->dirroot/theme/formal_white/config.php"); // Use any other theme.
// set_config('theme', 'formal_white');
// $page = new moodle_page();
// $page = new \moodle_page();
// $page->set_url('/user/profile.php');
// $page->set_context(context_system::instance());
// $page->set_context(\context_system::instance());
// $renderer = $page->get_renderer('core');
// $up1 = new user_picture($user1);
@@ -306,9 +314,9 @@ class core_outputcomponents_testcase extends advanced_testcase {
set_config('theme', 'classic');
$CFG->wwwroot = str_replace('https:', 'http:', $CFG->wwwroot);
$CFG->slasharguments = 0;
$page = new moodle_page();
$page = new \moodle_page();
$page->set_url('/user/profile.php');
$page->set_context(context_system::instance());
$page->set_context(\context_system::instance());
$renderer = $page->get_renderer('core');
$up3 = new user_picture($user3);
@@ -317,7 +325,7 @@ class core_outputcomponents_testcase extends advanced_testcase {
public function test_empty_menu() {
$emptymenu = new custom_menu();
$this->assertInstanceOf('custom_menu', $emptymenu);
$this->assertInstanceOf(custom_menu::class, $emptymenu);
$this->assertFalse($emptymenu->has_children());
}
@@ -347,7 +355,7 @@ EOF;
$this->assertCount(3, $item->get_children());
$this->assertEquals('Moodle community', $item->get_text());
$itemurl = $item->get_url();
$this->assertTrue($itemurl instanceof moodle_url);
$this->assertTrue($itemurl instanceof \moodle_url);
$this->assertEquals('http://moodle.org', $itemurl->out());
$this->assertNull($item->get_title()); // Implicit title.
@@ -460,7 +468,7 @@ EOF;
'<a href="page?page=7">8</a>',
);
$mpage = new moodle_page();
$mpage = new \moodle_page();
$rbase = new renderer_base($mpage, "/");
$pbara = new paging_bar(40, 0, 5, 'index.php');
$pbara->prepare($rbase, $mpage, "/");
@@ -475,7 +483,7 @@ EOF;
public function test_pix_icon() {
$this->resetAfterTest();
$page = new moodle_page();
$page = new \moodle_page();
set_config('theme', 'boost');
// Need to reset after changing theme.
@@ -543,7 +551,7 @@ EOF;
$options = [ "Option A", "Option B", "Option C" ];
$nothing = ['' => 'choosedots'];
$url = new moodle_url('/');
$url = new \moodle_url('/');
$singleselect = new single_select($url, $realname, $options, null, $nothing, 'someformid');
$singleselect->class = $realclass;
@@ -619,9 +627,9 @@ EOF;
'style' => $labelstyle
];
$url1 = new moodle_url("/#a");
$url2 = new moodle_url("/#b");
$url3 = new moodle_url("/#c");
$url1 = new \moodle_url("/#a");
$url2 = new \moodle_url("/#b");
$url3 = new \moodle_url("/#c");
$urls = [
$url1->out() => 'A',
+10 -10
View File
@@ -14,14 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for lib/outputfactories.php.
*
* @package core
* @category phpunit
* @copyright 2014 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
use test_output_factory;
defined('MOODLE_INTERNAL') || die();
@@ -30,9 +25,14 @@ require_once($CFG->libdir . '/outputfactories.php');
require_once($CFG->libdir . '/tests/fixtures/test_renderer_factory.php');
/**
* Unit tests for the user_picture class.
* Unit tests for lib/outputfactories.php.
*
* @package core
* @category test
* @copyright 2014 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_outputfactories_testcase extends advanced_testcase {
class outputfactories_test extends \advanced_testcase {
public function test_nonautoloaded_classnames() {
global $PAGE;
+18 -17
View File
@@ -14,14 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for lib/outputrequirementslibphp.
*
* @package core
* @category phpunit
* @copyright 2012 Petr Škoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
defined('MOODLE_INTERNAL') || die();
@@ -29,11 +22,19 @@ global $CFG;
require_once($CFG->libdir . '/outputrequirementslib.php');
class core_outputrequirementslib_testcase extends advanced_testcase {
/**
* Unit tests for lib/outputrequirementslibphp.
*
* @package core
* @category test
* @copyright 2012 Petr Škoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class outputrequirementslib_test extends \advanced_testcase {
public function test_string_for_js() {
$this->resetAfterTest();
$page = new moodle_page();
$page = new \moodle_page();
$page->requires->string_for_js('course', 'moodle', 1);
$page->requires->string_for_js('course', 'moodle', 1);
$this->expectException('coding_exception');
@@ -44,21 +45,21 @@ class core_outputrequirementslib_testcase extends advanced_testcase {
}
public function test_one_time_output_normal_case() {
$page = new moodle_page();
$page = new \moodle_page();
$this->assertTrue($page->requires->should_create_one_time_item_now('test_item'));
$this->assertFalse($page->requires->should_create_one_time_item_now('test_item'));
}
public function test_one_time_output_repeat_output_throws() {
$page = new moodle_page();
$page = new \moodle_page();
$page->requires->set_one_time_item_created('test_item');
$this->expectException('coding_exception');
$page->requires->set_one_time_item_created('test_item');
}
public function test_one_time_output_different_pages_independent() {
$firstpage = new moodle_page();
$secondpage = new moodle_page();
$firstpage = new \moodle_page();
$secondpage = new \moodle_page();
$this->assertTrue($firstpage->requires->should_create_one_time_item_now('test_item'));
$this->assertTrue($secondpage->requires->should_create_one_time_item_now('test_item'));
}
@@ -76,7 +77,7 @@ class core_outputrequirementslib_testcase extends advanced_testcase {
// With slasharguments on.
$CFG->slasharguments = 1;
$page = new moodle_page();
$page = new \moodle_page();
$requirements = $page->requires;
// Assert successful method call.
$this->assertTrue($requirements->jquery_plugin('jquery'));
@@ -91,7 +92,7 @@ class core_outputrequirementslib_testcase extends advanced_testcase {
// With slasharguments off.
$CFG->slasharguments = 0;
$page = new moodle_page();
$page = new \moodle_page();
$requirements = $page->requires;
// Assert successful method call.
$this->assertTrue($requirements->jquery_plugin('jquery'));
@@ -108,7 +109,7 @@ class core_outputrequirementslib_testcase extends advanced_testcase {
*/
public function test_js_call_amd() {
$page = new moodle_page();
$page = new \moodle_page();
// Load an AMD module without a function call.
$page->requires->js_call_amd('theme_foobar/lightbox');

Some files were not shown because too many files have changed in this diff Show More