Merge branch 'MDL-67353-38' of https://github.com/junpataleta/moodle into MOODLE_38_STABLE

This commit is contained in:
Jun Pataleta
2020-01-30 12:41:22 +08:00
11 changed files with 395 additions and 264 deletions
@@ -68,6 +68,17 @@ class course_handler extends \core_customfield\handler {
return self::$singleton;
}
/**
* Run reset code after unit tests to reset the singleton usage.
*/
public static function reset_caches(): void {
if (!PHPUNIT_TEST) {
throw new \coding_exception('This feature is only intended for use in unit tests');
}
static::$singleton = null;
}
/**
* The current user can configure custom fields on this component.
*
+14 -18
View File
@@ -38,25 +38,11 @@ use \core_customfield\category_controller;
class core_customfield_api_testcase extends advanced_testcase {
/**
* This method is called after the last test of this test class is run.
*/
public static function tearDownAfterClass() {
$handler = core_course\customfield\course_handler::create();
$handler->delete_all();
}
/**
* Tests set up.
*/
public function setUp() {
$this->resetAfterTest();
}
/**
* Get generator
* Get generator.
*
* @return core_customfield_generator
*/
protected function get_generator() : core_customfield_generator {
protected function get_generator(): core_customfield_generator {
return $this->getDataGenerator()->get_plugin_generator('core_customfield');
}
@@ -65,7 +51,7 @@ class core_customfield_api_testcase extends advanced_testcase {
*
* @param array $expected
* @param array $array array of objects with "get($property)" method
* @param sring $propertyname
* @param string $propertyname
*/
protected function assert_property_in_array($expected, $array, $propertyname) {
$this->assertEquals($expected, array_values(array_map(function($a) use ($propertyname) {
@@ -80,6 +66,8 @@ class core_customfield_api_testcase extends advanced_testcase {
* in the interface using drag-drop.
*/
public function test_move_category() {
$this->resetAfterTest();
// Create the categories.
$params = ['component' => 'core_course', 'area' => 'course', 'itemid' => 0];
$id0 = $this->get_generator()->create_category($params)->get('id');
@@ -129,6 +117,8 @@ class core_customfield_api_testcase extends advanced_testcase {
* Tests for \core_customfield\api::get_categories_with_fields() behaviour.
*/
public function test_get_categories_with_fields() {
$this->resetAfterTest();
// Create the categories.
$options = [
'component' => 'core_course',
@@ -163,6 +153,8 @@ class core_customfield_api_testcase extends advanced_testcase {
* Test for functions api::save_category() and rename_category)
*/
public function test_save_category() {
$this->resetAfterTest();
$params = ['component' => 'core_course', 'area' => 'course', 'itemid' => 0, 'name' => 'Cat1',
'contextid' => context_system::instance()->id];
$c1 = category_controller::create(0, (object)$params);
@@ -196,6 +188,8 @@ class core_customfield_api_testcase extends advanced_testcase {
* Test for function handler::create_category
*/
public function test_create_category() {
$this->resetAfterTest();
$handler = \core_course\customfield\course_handler::create();
$c1id = $handler->create_category();
$c1 = $handler->get_categories_with_fields()[$c1id];
@@ -218,6 +212,8 @@ class core_customfield_api_testcase extends advanced_testcase {
* Tests for \core_customfield\api::delete_category() behaviour.
*/
public function test_delete_category_with_fields() {
$this->resetAfterTest();
global $DB;
// Create two categories with fields and data.
$options = [
+15 -17
View File
@@ -37,29 +37,20 @@ use \core_customfield\field_controller;
class core_customfield_category_controller_testcase extends advanced_testcase {
/**
* This method is called after the last test of this test class is run.
*/
public static function tearDownAfterClass() {
$handler = core_course\customfield\course_handler::create();
$handler->delete_all();
}
/**
* Tests set up.
*/
public function setUp() {
$this->resetAfterTest();
}
/**
* Get generator
* Get generator.
*
* @return core_customfield_generator
*/
protected function get_generator() : core_customfield_generator {
protected function get_generator(): core_customfield_generator {
return $this->getDataGenerator()->get_plugin_generator('core_customfield');
}
/**
* Test for the field_controller::__construct function.
*/
public function test_constructor() {
$this->resetAfterTest();
$c = category_controller::create(0, (object)['component' => 'core_course', 'area' => 'course', 'itemid' => 0]);
$handler = $c->get_handler();
$this->assertTrue($c instanceof category_controller);
@@ -83,6 +74,8 @@ class core_customfield_category_controller_testcase extends advanced_testcase {
*/
public function test_constructor_errors() {
global $DB;
$this->resetAfterTest();
$cat = $this->get_generator()->create_category();
$catrecord = $cat->to_record();
@@ -181,6 +174,7 @@ class core_customfield_category_controller_testcase extends advanced_testcase {
* \core_customfield\category_controller::get()
*/
public function test_create_category() {
$this->resetAfterTest();
// Create the category.
$lpg = $this->get_generator();
@@ -209,6 +203,8 @@ class core_customfield_category_controller_testcase extends advanced_testcase {
* Tests for \core_customfield\category_controller::set() behaviour.
*/
public function test_rename_category() {
$this->resetAfterTest();
// Create the category.
$params = ['component' => 'core_course', 'area' => 'course', 'itemid' => 0, 'name' => 'Cat1',
'contextid' => context_system::instance()->id];
@@ -232,6 +228,8 @@ class core_customfield_category_controller_testcase extends advanced_testcase {
* Tests for \core_customfield\category_controller::delete() behaviour.
*/
public function test_delete_category() {
$this->resetAfterTest();
// Create the category.
$lpg = $this->get_generator();
$category0 = $lpg->create_category();
+8 -18
View File
@@ -35,25 +35,11 @@ use core_customfield\data_controller;
class core_customfield_data_controller_testcase extends advanced_testcase {
/**
* This method is called after the last test of this test class is run.
*/
public static function tearDownAfterClass() {
$handler = core_course\customfield\course_handler::create();
$handler->delete_all();
}
/**
* Tests set up.
*/
public function setUp() {
$this->resetAfterTest();
}
/**
* Get generator
* Get generator.
*
* @return core_customfield_generator
*/
protected function get_generator() : core_customfield_generator {
protected function get_generator(): core_customfield_generator {
return $this->getDataGenerator()->get_plugin_generator('core_customfield');
}
@@ -62,6 +48,8 @@ class core_customfield_data_controller_testcase extends advanced_testcase {
*/
public function test_constructor() {
global $DB;
$this->resetAfterTest();
// Create a course, fields category and fields.
$course = $this->getDataGenerator()->create_course();
$category0 = $this->get_generator()->create_category(['name' => 'aaaa']);
@@ -130,6 +118,8 @@ class core_customfield_data_controller_testcase extends advanced_testcase {
*/
public function test_constructor_errors() {
global $DB;
$this->resetAfterTest();
// Create a category, field and data.
$category = $this->get_generator()->create_category();
$field = $this->get_generator()->create_field(['categoryid' => $category->get('id')]);
@@ -188,4 +178,4 @@ class core_customfield_data_controller_testcase extends advanced_testcase {
$this->assertEquals(moodle_exception::class, get_class($e));
}
}
}
}
+14 -18
View File
@@ -39,25 +39,11 @@ use \core_customfield\field_controller;
class core_customfield_field_controller_testcase extends advanced_testcase {
/**
* This method is called after the last test of this test class is run.
*/
public static function tearDownAfterClass() {
$handler = core_course\customfield\course_handler::create();
$handler->delete_all();
}
/**
* Tests set up.
*/
public function setUp() {
$this->resetAfterTest();
}
/**
* Get generator
* Get generator.
*
* @return core_customfield_generator
*/
protected function get_generator() : core_customfield_generator {
protected function get_generator(): core_customfield_generator {
return $this->getDataGenerator()->get_plugin_generator('core_customfield');
}
@@ -66,6 +52,8 @@ class core_customfield_field_controller_testcase extends advanced_testcase {
*/
public function test_constructor() {
global $DB;
$this->resetAfterTest();
// Create the category.
$category0 = $this->get_generator()->create_category();
@@ -111,6 +99,8 @@ class core_customfield_field_controller_testcase extends advanced_testcase {
*/
public function test_constructor_errors() {
global $DB;
$this->resetAfterTest();
// Create a category and a field.
$category = $this->get_generator()->create_category();
$field = $this->get_generator()->create_field(['categoryid' => $category->get('id')]);
@@ -183,6 +173,8 @@ class core_customfield_field_controller_testcase extends advanced_testcase {
*/
public function test_create_field() {
global $DB;
$this->resetAfterTest();
$lpg = $this->get_generator();
$category = $lpg->create_category();
$fields = $DB->get_records(\core_customfield\field::TABLE, ['categoryid' => $category->get('id')]);
@@ -211,6 +203,8 @@ class core_customfield_field_controller_testcase extends advanced_testcase {
*/
public function test_delete_field() {
global $DB;
$this->resetAfterTest();
$lpg = $this->get_generator();
$category = $lpg->create_category();
$fields = $DB->get_records(\core_customfield\field::TABLE, ['categoryid' => $category->get('id')]);
@@ -237,6 +231,8 @@ class core_customfield_field_controller_testcase extends advanced_testcase {
* Tests for \core_customfield\field_controller::get_configdata_property() behaviour.
*/
public function test_get_configdata_property() {
$this->resetAfterTest();
$lpg = $this->get_generator();
$category = $lpg->create_category();
$configdata = ['a' => 'b', 'c' => ['d', 'e']];
@@ -251,4 +247,4 @@ class core_customfield_field_controller_testcase extends advanced_testcase {
$this->assertEquals(['d', 'e'], $field->get_configdata_property('c'));
$this->assertEquals(null, $field->get_configdata_property('x'));
}
}
}
+1 -9
View File
@@ -35,19 +35,11 @@ defined('MOODLE_INTERNAL') || die();
*/
class core_customfield_generator_testcase extends advanced_testcase {
/**
* This method is called after the last test of this test class is run.
*/
public static function tearDownAfterClass() {
$handler = core_course\customfield\course_handler::create();
$handler->delete_all();
}
/**
* Get generator
* @return core_customfield_generator
*/
protected function get_generator() : core_customfield_generator {
protected function get_generator(): core_customfield_generator {
return $this->getDataGenerator()->get_plugin_generator('core_customfield');
}
+100 -69
View File
@@ -38,71 +38,59 @@ use core_customfield\privacy\provider;
*/
class core_customfield_privacy_testcase extends provider_testcase {
/** @var stdClass[] */
private $courses = [];
/** @var \core_customfield\category_controller[] */
private $cfcats = [];
/** @var \core_customfield\field_controller[] */
private $cffields = [];
/**
* This method is called after the last test of this test class is run.
* Generate data.
*
* @return array
*/
public static function tearDownAfterClass() {
$handler = core_course\customfield\course_handler::create();
$handler->delete_all();
}
/**
* Set up
*/
public function setUp() {
protected function generate_test_data(): array {
$this->resetAfterTest();
$this->cfcats[1] = $this->get_generator()->create_category();
$this->cfcats[2] = $this->get_generator()->create_category();
$this->cffields[11] = $this->get_generator()->create_field(
['categoryid' => $this->cfcats[1]->get('id'), 'type' => 'checkbox']);
$this->cffields[12] = $this->get_generator()->create_field(
['categoryid' => $this->cfcats[1]->get('id'), 'type' => 'date']);
$this->cffields[13] = $this->get_generator()->create_field(
['categoryid' => $this->cfcats[1]->get('id'),
$generator = $this->getDataGenerator()->get_plugin_generator('core_customfield');
$cfcats[1] = $generator->create_category();
$cfcats[2] = $generator->create_category();
$cffields[11] = $generator->create_field(
['categoryid' => $cfcats[1]->get('id'), 'type' => 'checkbox']);
$cffields[12] = $generator->create_field(
['categoryid' => $cfcats[1]->get('id'), 'type' => 'date']);
$cffields[13] = $generator->create_field(
['categoryid' => $cfcats[1]->get('id'),
'type' => 'select', 'configdata' => ['options' => "a\nb\nc"]]);
$this->cffields[14] = $this->get_generator()->create_field(
['categoryid' => $this->cfcats[1]->get('id'), 'type' => 'text']);
$this->cffields[15] = $this->get_generator()->create_field(
['categoryid' => $this->cfcats[1]->get('id'), 'type' => 'textarea']);
$this->cffields[21] = $this->get_generator()->create_field(
['categoryid' => $this->cfcats[2]->get('id')]);
$this->cffields[22] = $this->get_generator()->create_field(
['categoryid' => $this->cfcats[2]->get('id')]);
$cffields[14] = $generator->create_field(
['categoryid' => $cfcats[1]->get('id'), 'type' => 'text']);
$cffields[15] = $generator->create_field(
['categoryid' => $cfcats[1]->get('id'), 'type' => 'textarea']);
$cffields[21] = $generator->create_field(
['categoryid' => $cfcats[2]->get('id')]);
$cffields[22] = $generator->create_field(
['categoryid' => $cfcats[2]->get('id')]);
$this->courses[1] = $this->getDataGenerator()->create_course();
$this->courses[2] = $this->getDataGenerator()->create_course();
$this->courses[3] = $this->getDataGenerator()->create_course();
$courses[1] = $this->getDataGenerator()->create_course();
$courses[2] = $this->getDataGenerator()->create_course();
$courses[3] = $this->getDataGenerator()->create_course();
$this->get_generator()->add_instance_data($this->cffields[11], $this->courses[1]->id, 1);
$this->get_generator()->add_instance_data($this->cffields[12], $this->courses[1]->id, 1546300800);
$this->get_generator()->add_instance_data($this->cffields[13], $this->courses[1]->id, 2);
$this->get_generator()->add_instance_data($this->cffields[14], $this->courses[1]->id, 'Hello1');
$this->get_generator()->add_instance_data($this->cffields[15], $this->courses[1]->id,
$generator->add_instance_data($cffields[11], $courses[1]->id, 1);
$generator->add_instance_data($cffields[12], $courses[1]->id, 1546300800);
$generator->add_instance_data($cffields[13], $courses[1]->id, 2);
$generator->add_instance_data($cffields[14], $courses[1]->id, 'Hello1');
$generator->add_instance_data($cffields[15], $courses[1]->id,
['text' => '<p>Hi there</p>', 'format' => FORMAT_HTML]);
$this->get_generator()->add_instance_data($this->cffields[21], $this->courses[1]->id, 'hihi1');
$generator->add_instance_data($cffields[21], $courses[1]->id, 'hihi1');
$this->get_generator()->add_instance_data($this->cffields[14], $this->courses[2]->id, 'Hello2');
$generator->add_instance_data($cffields[14], $courses[2]->id, 'Hello2');
$this->get_generator()->add_instance_data($this->cffields[21], $this->courses[2]->id, 'hihi2');
$generator->add_instance_data($cffields[21], $courses[2]->id, 'hihi2');
$this->setUser($this->getDataGenerator()->create_user());
}
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
/**
* Get generator
* @return core_customfield_generator
*/
protected function get_generator() : core_customfield_generator {
return $this->getDataGenerator()->get_plugin_generator('core_customfield');
return [
'user' => $user,
'cfcats' => $cfcats,
'cffields' => $cffields,
'courses' => $courses,
];
}
/**
@@ -119,11 +107,17 @@ class core_customfield_privacy_testcase extends provider_testcase {
*/
public function test_get_customfields_data_contexts() {
global $DB;
list($sql, $params) = $DB->get_in_or_equal([$this->courses[1]->id, $this->courses[2]->id], SQL_PARAMS_NAMED);
[
'cffields' => $cffields,
'cfcats' => $cfcats,
'courses' => $courses,
] = $this->generate_test_data();
list($sql, $params) = $DB->get_in_or_equal([$courses[1]->id, $courses[2]->id], SQL_PARAMS_NAMED);
$r = provider::get_customfields_data_contexts('core_course', 'course', '=0',
$sql, $params);
$this->assertEquals([context_course::instance($this->courses[1]->id)->id,
context_course::instance($this->courses[2]->id)->id],
$this->assertEquals([context_course::instance($courses[1]->id)->id,
context_course::instance($courses[2]->id)->id],
$r->get_contextids(), '', 0, 10, true);
}
@@ -131,6 +125,8 @@ class core_customfield_privacy_testcase extends provider_testcase {
* Test for provider::get_customfields_configuration_contexts()
*/
public function test_get_customfields_configuration_contexts() {
$this->generate_test_data();
$r = provider::get_customfields_configuration_contexts('core_course', 'course');
$this->assertEquals([context_system::instance()->id], $r->get_contextids());
}
@@ -140,13 +136,20 @@ class core_customfield_privacy_testcase extends provider_testcase {
*/
public function test_export_customfields_data() {
global $USER, $DB;
$this->resetAfterTest();
[
'cffields' => $cffields,
'cfcats' => $cfcats,
'courses' => $courses,
] = $this->generate_test_data();
// Hack one of the fields so it has an invalid field type.
$invalidfieldid = $this->cffields[21]->get('id');
$invalidfieldid = $cffields[21]->get('id');
$DB->update_record('customfield_field', ['id' => $invalidfieldid, 'type' => 'invalid']);
$context = context_course::instance($this->courses[1]->id);
$context = context_course::instance($courses[1]->id);
$contextlist = new approved_contextlist($USER, 'core_customfield', [$context->id]);
provider::export_customfields_data($contextlist, 'core_course', 'course', '=0', '=:i', ['i' => $this->courses[1]->id]);
provider::export_customfields_data($contextlist, 'core_course', 'course', '=0', '=:i', ['i' => $courses[1]->id]);
/** @var core_privacy\tests\request\content_writer $writer */
$writer = writer::with_context($context);
@@ -155,7 +158,7 @@ class core_customfield_privacy_testcase extends provider_testcase {
$invaldfieldischecked = false;
foreach ($DB->get_records('customfield_data', []) as $dbrecord) {
$data = $writer->get_data(['Custom fields data', $dbrecord->id]);
if ($dbrecord->instanceid == $this->courses[1]->id) {
if ($dbrecord->instanceid == $courses[1]->id) {
$this->assertEquals($dbrecord->fieldid, $data->fieldid);
$this->assertNotEmpty($data->fieldtype);
$this->assertNotEmpty($data->fieldshortname);
@@ -175,10 +178,17 @@ class core_customfield_privacy_testcase extends provider_testcase {
*/
public function test_delete_customfields_data() {
global $USER, $DB;
$approvedcontexts = new approved_contextlist($USER, 'core_course', [context_course::instance($this->courses[1]->id)->id]);
$this->resetAfterTest();
[
'cffields' => $cffields,
'cfcats' => $cfcats,
'courses' => $courses,
] = $this->generate_test_data();
$approvedcontexts = new approved_contextlist($USER, 'core_course', [context_course::instance($courses[1]->id)->id]);
provider::delete_customfields_data($approvedcontexts, 'core_course', 'course');
$this->assertEmpty($DB->get_records('customfield_data', ['instanceid' => $this->courses[1]->id]));
$this->assertNotEmpty($DB->get_records('customfield_data', ['instanceid' => $this->courses[2]->id]));
$this->assertEmpty($DB->get_records('customfield_data', ['instanceid' => $courses[1]->id]));
$this->assertNotEmpty($DB->get_records('customfield_data', ['instanceid' => $courses[2]->id]));
}
/**
@@ -186,9 +196,16 @@ class core_customfield_privacy_testcase extends provider_testcase {
*/
public function test_delete_customfields_configuration() {
global $USER, $DB;
$this->resetAfterTest();
[
'cffields' => $cffields,
'cfcats' => $cfcats,
'courses' => $courses,
] = $this->generate_test_data();
// Remember the list of fields in the category 2 before we delete it.
$catid1 = $this->cfcats[1]->get('id');
$catid2 = $this->cfcats[2]->get('id');
$catid1 = $cfcats[1]->get('id');
$catid2 = $cfcats[2]->get('id');
$fids2 = $DB->get_fieldset_select('customfield_field', 'id', 'categoryid=?', [$catid2]);
$this->assertNotEmpty($fids2);
list($fsql, $fparams) = $DB->get_in_or_equal($fids2, SQL_PARAMS_NAMED);
@@ -216,9 +233,16 @@ class core_customfield_privacy_testcase extends provider_testcase {
*/
public function test_delete_customfields_configuration_for_context() {
global $USER, $DB;
$this->resetAfterTest();
[
'cffields' => $cffields,
'cfcats' => $cfcats,
'courses' => $courses,
] = $this->generate_test_data();
// Remember the list of fields in the category 2 before we delete it.
$catid1 = $this->cfcats[1]->get('id');
$catid2 = $this->cfcats[2]->get('id');
$catid1 = $cfcats[1]->get('id');
$catid2 = $cfcats[2]->get('id');
$fids2 = $DB->get_fieldset_select('customfield_field', 'id', 'categoryid=?', [$catid2]);
$this->assertNotEmpty($fids2);
list($fsql, $fparams) = $DB->get_in_or_equal($fids2, SQL_PARAMS_NAMED);
@@ -246,12 +270,19 @@ class core_customfield_privacy_testcase extends provider_testcase {
*/
public function test_delete_customfields_data_for_context() {
global $DB;
$this->resetAfterTest();
[
'cffields' => $cffields,
'cfcats' => $cfcats,
'courses' => $courses,
] = $this->generate_test_data();
provider::delete_customfields_data_for_context('core_course', 'course',
context_course::instance($this->courses[1]->id));
context_course::instance($courses[1]->id));
$fids2 = $DB->get_fieldset_select('customfield_field', 'id', '1=1', []);
list($fsql, $fparams) = $DB->get_in_or_equal($fids2, SQL_PARAMS_NAMED);
$fparams['course1'] = $this->courses[1]->id;
$fparams['course2'] = $this->courses[2]->id;
$fparams['course1'] = $courses[1]->id;
$fparams['course2'] = $courses[2]->id;
$this->assertEmpty($DB->get_records_select('customfield_data', 'instanceid = :course1 AND fieldid ' . $fsql, $fparams));
$this->assertNotEmpty($DB->get_records_select('customfield_data', 'instanceid = :course2 AND fieldid ' . $fsql, $fparams));
}
@@ -88,7 +88,7 @@ class component_favourite_service_testcase extends advanced_testcase {
// Check the mockstore for all objects with properties matching the key => val pairs in $criteria.
foreach ($mockstore as $index => $mockrow) {
$mockrowarr = (array)$mockrow;
if (array_diff($criteria, $mockrowarr) == []) {
if (array_diff_assoc($criteria, $mockrowarr) == []) {
$returns[$index] = $mockrow;
}
}
@@ -107,7 +107,7 @@ class component_favourite_service_testcase extends advanced_testcase {
$crit = ['userid' => $userid, 'component' => $comp, 'itemtype' => $type, 'itemid' => $id, 'contextid' => $ctxid];
foreach ($mockstore as $fakerow) {
$fakerowarr = (array)$fakerow;
if (array_diff($crit, $fakerowarr) == []) {
if (array_diff_assoc($crit, $fakerowarr) == []) {
return $fakerow;
}
}
@@ -133,7 +133,7 @@ class component_favourite_service_testcase extends advanced_testcase {
// Check the mockstore for all objects with properties matching the key => val pairs in $criteria.
foreach ($mockstore as $index => $mockrow) {
$mockrowarr = (array)$mockrow;
if (array_diff($criteria, $mockrowarr) == []) {
if (array_diff_assoc($criteria, $mockrowarr) == []) {
$count++;
}
}
@@ -156,7 +156,7 @@ class component_favourite_service_testcase extends advanced_testcase {
// Check the mockstore for all objects with properties matching the key => val pairs in $criteria.
foreach ($mockstore as $index => $mockrow) {
$mockrowarr = (array)$mockrow;
if (array_diff($criteria, $mockrowarr) == []) {
if (array_diff_assoc($criteria, $mockrowarr) == []) {
unset($mockstore[$index]);
}
}
@@ -169,7 +169,7 @@ class component_favourite_service_testcase extends advanced_testcase {
foreach ($mockstore as $index => $mockrow) {
$mockrowarr = (array)$mockrow;
echo "Here";
if (array_diff($criteria, $mockrowarr) == []) {
if (array_diff_assoc($criteria, $mockrowarr) == []) {
return true;
}
}
@@ -88,7 +88,7 @@ class user_favourite_service_testcase extends advanced_testcase {
// Check the mockstore for all objects with properties matching the key => val pairs in $criteria.
foreach ($mockstore as $index => $mockrow) {
$mockrowarr = (array)$mockrow;
if (array_diff($criteria, $mockrowarr) == []) {
if (array_diff_assoc($criteria, $mockrowarr) == []) {
$returns[$index] = $mockrow;
}
}
@@ -107,7 +107,7 @@ class user_favourite_service_testcase extends advanced_testcase {
$crit = ['userid' => $userid, 'component' => $comp, 'itemtype' => $type, 'itemid' => $id, 'contextid' => $ctxid];
foreach ($mockstore as $fakerow) {
$fakerowarr = (array)$fakerow;
if (array_diff($crit, $fakerowarr) == []) {
if (array_diff_assoc($crit, $fakerowarr) == []) {
return $fakerow;
}
}
@@ -133,7 +133,7 @@ class user_favourite_service_testcase extends advanced_testcase {
// Check the mockstore for all objects with properties matching the key => val pairs in $criteria.
foreach ($mockstore as $index => $mockrow) {
$mockrowarr = (array)$mockrow;
if (array_diff($criteria, $mockrowarr) == []) {
if (array_diff_assoc($criteria, $mockrowarr) == []) {
$count++;
}
}
@@ -156,7 +156,7 @@ class user_favourite_service_testcase extends advanced_testcase {
// Check the mockstore for all objects with properties matching the key => val pairs in $criteria.
foreach ($mockstore as $index => $mockrow) {
$mockrowarr = (array)$mockrow;
if (array_diff($criteria, $mockrowarr) == []) {
if (array_diff_assoc($criteria, $mockrowarr) == []) {
return true;
}
}
+3
View File
@@ -252,6 +252,9 @@ class phpunit_util extends testing_util {
if (class_exists('\core\update\checker')) {
\core\update\checker::reset_caches(true);
}
if (class_exists('\core_course\customfield\course_handler')) {
\core_course\customfield\course_handler::reset_caches();
}
// Clear static cache within restore.
if (class_exists('restore_section_structure_step')) {
+220 -106
View File
@@ -33,23 +33,6 @@ require_once($CFG->libdir . '/filterlib.php');
* Test filters.
*/
class core_filterlib_testcase extends advanced_testcase {
private $syscontext;
private $childcontext;
private $childcontext2;
private $catcontext;
private $coursecontext;
private $course;
private $activity1context;
private $activity2context;
protected function setUp() {
global $DB;
parent::setUp();
$this->resetAfterTest();
$DB->delete_records('filter_active', array());
$DB->delete_records('filter_config', array());
}
private function assert_only_one_filter_globally($filter, $state) {
global $DB;
@@ -79,6 +62,8 @@ class core_filterlib_testcase extends advanced_testcase {
}
public function test_set_filter_globally_on() {
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
// Setup fixture.
// Exercise SUT.
filter_set_global_state('name', TEXTFILTER_ON);
@@ -87,6 +72,8 @@ class core_filterlib_testcase extends advanced_testcase {
}
public function test_set_filter_globally_off() {
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
// Setup fixture.
// Exercise SUT.
filter_set_global_state('name', TEXTFILTER_OFF);
@@ -95,6 +82,8 @@ class core_filterlib_testcase extends advanced_testcase {
}
public function test_set_filter_globally_disabled() {
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
// Setup fixture.
// Exercise SUT.
filter_set_global_state('name', TEXTFILTER_DISABLED);
@@ -106,10 +95,13 @@ class core_filterlib_testcase extends advanced_testcase {
* @expectedException coding_exception
*/
public function test_global_config_exception_on_invalid_state() {
$this->resetAfterTest();
filter_set_global_state('name', 0);
}
public function test_auto_sort_order() {
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
// Setup fixture.
// Exercise SUT.
filter_set_global_state('one', TEXTFILTER_DISABLED);
@@ -119,6 +111,8 @@ class core_filterlib_testcase extends advanced_testcase {
}
public function test_auto_sort_order_enabled() {
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
// Setup fixture.
// Exercise SUT.
filter_set_global_state('one', TEXTFILTER_ON);
@@ -128,6 +122,8 @@ class core_filterlib_testcase extends advanced_testcase {
}
public function test_update_existing_dont_duplicate() {
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
// Setup fixture.
// Exercise SUT.
filter_set_global_state('name', TEXTFILTER_ON);
@@ -137,6 +133,8 @@ class core_filterlib_testcase extends advanced_testcase {
}
public function test_update_reorder_down() {
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
// Setup fixture.
filter_set_global_state('one', TEXTFILTER_ON);
filter_set_global_state('two', TEXTFILTER_ON);
@@ -148,6 +146,8 @@ class core_filterlib_testcase extends advanced_testcase {
}
public function test_update_reorder_up() {
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
// Setup fixture.
filter_set_global_state('one', TEXTFILTER_ON);
filter_set_global_state('two', TEXTFILTER_ON);
@@ -160,6 +160,8 @@ class core_filterlib_testcase extends advanced_testcase {
}
public function test_auto_sort_order_change_to_enabled() {
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
// Setup fixture.
filter_set_global_state('one', TEXTFILTER_ON);
filter_set_global_state('two', TEXTFILTER_DISABLED);
@@ -171,6 +173,8 @@ class core_filterlib_testcase extends advanced_testcase {
}
public function test_auto_sort_order_change_to_disabled() {
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
// Setup fixture.
filter_set_global_state('one', TEXTFILTER_ON);
filter_set_global_state('two', TEXTFILTER_ON);
@@ -182,6 +186,8 @@ class core_filterlib_testcase extends advanced_testcase {
}
public function test_filter_get_global_states() {
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
// Setup fixture.
filter_set_global_state('one', TEXTFILTER_ON);
filter_set_global_state('two', TEXTFILTER_OFF);
@@ -216,6 +222,8 @@ class core_filterlib_testcase extends advanced_testcase {
}
public function test_local_on() {
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
// Exercise SUT.
filter_set_local_state('name', 123, TEXTFILTER_ON);
// Validate.
@@ -223,6 +231,8 @@ class core_filterlib_testcase extends advanced_testcase {
}
public function test_local_off() {
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
// Exercise SUT.
filter_set_local_state('name', 123, TEXTFILTER_OFF);
// Validate.
@@ -230,6 +240,8 @@ class core_filterlib_testcase extends advanced_testcase {
}
public function test_local_inherit() {
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
// Exercise SUT.
filter_set_local_state('name', 123, TEXTFILTER_INHERIT);
// Validate.
@@ -240,6 +252,7 @@ class core_filterlib_testcase extends advanced_testcase {
* @expectedException coding_exception
*/
public function test_local_invalid_state_throws_exception() {
$this->resetAfterTest();
// Exercise SUT.
filter_set_local_state('name', 123, -9999);
}
@@ -248,11 +261,14 @@ class core_filterlib_testcase extends advanced_testcase {
* @expectedException coding_exception
*/
public function test_throws_exception_when_setting_global() {
$this->resetAfterTest();
// Exercise SUT.
filter_set_local_state('name', context_system::instance()->id, TEXTFILTER_INHERIT);
}
public function test_local_inherit_deletes_existing() {
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
// Setup fixture.
filter_set_local_state('name', 123, TEXTFILTER_INHERIT);
// Exercise SUT.
@@ -276,6 +292,8 @@ class core_filterlib_testcase extends advanced_testcase {
}
public function test_set_new_config() {
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
// Exercise SUT.
filter_set_local_config('name', 123, 'settingname', 'An arbitrary value');
// Validate.
@@ -283,6 +301,8 @@ class core_filterlib_testcase extends advanced_testcase {
}
public function test_update_existing_config() {
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
// Setup fixture.
filter_set_local_config('name', 123, 'settingname', 'An arbitrary value');
// Exercise SUT.
@@ -292,6 +312,7 @@ class core_filterlib_testcase extends advanced_testcase {
}
public function test_filter_get_local_config() {
$this->resetAfterTest();
// Setup fixture.
filter_set_local_config('name', 123, 'setting1', 'An arbitrary value');
filter_set_local_config('name', 123, 'setting2', 'Another arbitrary value');
@@ -306,22 +327,37 @@ class core_filterlib_testcase extends advanced_testcase {
protected function setup_available_in_context_tests() {
$course = $this->getDataGenerator()->create_course(array('category'=>1));
$this->childcontext = context_coursecat::instance(1);
$this->childcontext2 = context_course::instance($course->id);
$this->syscontext = context_system::instance();
$childcontext = context_coursecat::instance(1);
$childcontext2 = context_course::instance($course->id);
$syscontext = context_system::instance();
return [
'syscontext' => $syscontext,
'childcontext' => $childcontext,
'childcontext2' => $childcontext2
];
}
protected function remove_all_filters_from_config() {
global $DB;
$DB->delete_records('filter_active', array());
$DB->delete_records('filter_config', array());
}
private function assert_filter_list($expectedfilters, $filters) {
$this->setup_available_in_context_tests();
$this->assertEquals($expectedfilters, array_keys($filters), '', 0, 10, true);
}
public function test_globally_on_is_returned() {
$this->setup_available_in_context_tests();
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
[
'syscontext' => $syscontext
] = $this->setup_available_in_context_tests();
// Setup fixture.
filter_set_global_state('name', TEXTFILTER_ON);
// Exercise SUT.
$filters = filter_get_active_in_context($this->syscontext);
$filters = filter_get_active_in_context($syscontext);
// Validate.
$this->assert_filter_list(array('name'), $filters);
// Check no config returned correctly.
@@ -329,97 +365,128 @@ class core_filterlib_testcase extends advanced_testcase {
}
public function test_globally_off_not_returned() {
$this->setup_available_in_context_tests();
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
[
'childcontext2' => $childcontext2
] = $this->setup_available_in_context_tests();
// Setup fixture.
filter_set_global_state('name', TEXTFILTER_OFF);
// Exercise SUT.
$filters = filter_get_active_in_context($this->childcontext2);
$filters = filter_get_active_in_context($childcontext2);
// Validate.
$this->assert_filter_list(array(), $filters);
}
public function test_globally_off_overridden() {
$this->setup_available_in_context_tests();
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
[
'childcontext' => $childcontext,
'childcontext2' => $childcontext2
] = $this->setup_available_in_context_tests();
// Setup fixture.
filter_set_global_state('name', TEXTFILTER_OFF);
filter_set_local_state('name', $this->childcontext->id, TEXTFILTER_ON);
filter_set_local_state('name', $childcontext->id, TEXTFILTER_ON);
// Exercise SUT.
$filters = filter_get_active_in_context($this->childcontext2);
$filters = filter_get_active_in_context($childcontext2);
// Validate.
$this->assert_filter_list(array('name'), $filters);
}
public function test_globally_on_overridden() {
$this->setup_available_in_context_tests();
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
[
'childcontext' => $childcontext,
'childcontext2' => $childcontext2
] = $this->setup_available_in_context_tests();
// Setup fixture.
filter_set_global_state('name', TEXTFILTER_ON);
filter_set_local_state('name', $this->childcontext->id, TEXTFILTER_OFF);
filter_set_local_state('name', $childcontext->id, TEXTFILTER_OFF);
// Exercise SUT.
$filters = filter_get_active_in_context($this->childcontext2);
$filters = filter_get_active_in_context($childcontext2);
// Validate.
$this->assert_filter_list(array(), $filters);
}
public function test_globally_disabled_not_overridden() {
$this->setup_available_in_context_tests();
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
[
'syscontext' => $syscontext,
'childcontext' => $childcontext
] = $this->setup_available_in_context_tests();
// Setup fixture.
filter_set_global_state('name', TEXTFILTER_DISABLED);
filter_set_local_state('name', $this->childcontext->id, TEXTFILTER_ON);
filter_set_local_state('name', $childcontext->id, TEXTFILTER_ON);
// Exercise SUT.
$filters = filter_get_active_in_context($this->syscontext);
$filters = filter_get_active_in_context($syscontext);
// Validate.
$this->assert_filter_list(array(), $filters);
}
public function test_single_config_returned() {
$this->setup_available_in_context_tests();
$this->resetAfterTest();
[
'childcontext' => $childcontext
] = $this->setup_available_in_context_tests();
// Setup fixture.
filter_set_global_state('name', TEXTFILTER_ON);
filter_set_local_config('name', $this->childcontext->id, 'settingname', 'A value');
filter_set_local_config('name', $childcontext->id, 'settingname', 'A value');
// Exercise SUT.
$filters = filter_get_active_in_context($this->childcontext);
$filters = filter_get_active_in_context($childcontext);
// Validate.
$this->assertEquals(array('settingname' => 'A value'), $filters['name']);
}
public function test_multi_config_returned() {
$this->setup_available_in_context_tests();
$this->resetAfterTest();
[
'childcontext' => $childcontext
] = $this->setup_available_in_context_tests();
// Setup fixture.
filter_set_global_state('name', TEXTFILTER_ON);
filter_set_local_config('name', $this->childcontext->id, 'settingname', 'A value');
filter_set_local_config('name', $this->childcontext->id, 'anothersettingname', 'Another value');
filter_set_local_config('name', $childcontext->id, 'settingname', 'A value');
filter_set_local_config('name', $childcontext->id, 'anothersettingname', 'Another value');
// Exercise SUT.
$filters = filter_get_active_in_context($this->childcontext);
$filters = filter_get_active_in_context($childcontext);
// Validate.
$this->assertEquals(array('settingname' => 'A value', 'anothersettingname' => 'Another value'), $filters['name']);
}
public function test_config_from_other_context_not_returned() {
$this->setup_available_in_context_tests();
$this->resetAfterTest();
[
'childcontext' => $childcontext,
'childcontext2' => $childcontext2
] = $this->setup_available_in_context_tests();
// Setup fixture.
filter_set_global_state('name', TEXTFILTER_ON);
filter_set_local_config('name', $this->childcontext->id, 'settingname', 'A value');
filter_set_local_config('name', $this->childcontext2->id, 'anothersettingname', 'Another value');
filter_set_local_config('name', $childcontext->id, 'settingname', 'A value');
filter_set_local_config('name', $childcontext2->id, 'anothersettingname', 'Another value');
// Exercise SUT.
$filters = filter_get_active_in_context($this->childcontext2);
$filters = filter_get_active_in_context($childcontext2);
// Validate.
$this->assertEquals(array('anothersettingname' => 'Another value'), $filters['name']);
}
public function test_config_from_other_filter_not_returned() {
$this->setup_available_in_context_tests();
$this->resetAfterTest();
[
'childcontext' => $childcontext
] = $this->setup_available_in_context_tests();
// Setup fixture.
filter_set_global_state('name', TEXTFILTER_ON);
filter_set_local_config('name', $this->childcontext->id, 'settingname', 'A value');
filter_set_local_config('other', $this->childcontext->id, 'anothersettingname', 'Another value');
filter_set_local_config('name', $childcontext->id, 'settingname', 'A value');
filter_set_local_config('other', $childcontext->id, 'anothersettingname', 'Another value');
// Exercise SUT.
$filters = filter_get_active_in_context($this->childcontext);
$filters = filter_get_active_in_context($childcontext);
// Validate.
$this->assertEquals(array('settingname' => 'A value'), $filters['name']);
}
protected function assert_one_available_filter($filter, $localstate, $inheritedstate, $filters) {
$this->setup_available_in_context_tests();
$this->assertEquals(1, count($filters), 'More than one record returned %s.');
$rec = $filters[$filter];
unset($rec->id);
@@ -431,34 +498,47 @@ class core_filterlib_testcase extends advanced_testcase {
}
public function test_available_in_context_localoverride() {
$this->setup_available_in_context_tests();
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
[
'childcontext' => $childcontext
] = $this->setup_available_in_context_tests();
// Setup fixture.
filter_set_global_state('name', TEXTFILTER_ON);
filter_set_local_state('name', $this->childcontext->id, TEXTFILTER_OFF);
filter_set_local_state('name', $childcontext->id, TEXTFILTER_OFF);
// Exercise SUT.
$filters = filter_get_available_in_context($this->childcontext);
$filters = filter_get_available_in_context($childcontext);
// Validate.
$this->assert_one_available_filter('name', TEXTFILTER_OFF, TEXTFILTER_ON, $filters);
}
public function test_available_in_context_nolocaloverride() {
$this->setup_available_in_context_tests();
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
[
'childcontext' => $childcontext,
'childcontext2' => $childcontext2
] = $this->setup_available_in_context_tests();
// Setup fixture.
filter_set_global_state('name', TEXTFILTER_ON);
filter_set_local_state('name', $this->childcontext->id, TEXTFILTER_OFF);
filter_set_local_state('name', $childcontext->id, TEXTFILTER_OFF);
// Exercise SUT.
$filters = filter_get_available_in_context($this->childcontext2);
$filters = filter_get_available_in_context($childcontext2);
// Validate.
$this->assert_one_available_filter('name', TEXTFILTER_INHERIT, TEXTFILTER_OFF, $filters);
}
public function test_available_in_context_disabled_not_returned() {
$this->setup_available_in_context_tests();
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
[
'childcontext' => $childcontext
] = $this->setup_available_in_context_tests();
// Setup fixture.
filter_set_global_state('name', TEXTFILTER_DISABLED);
filter_set_local_state('name', $this->childcontext->id, TEXTFILTER_ON);
filter_set_local_state('name', $childcontext->id, TEXTFILTER_ON);
// Exercise SUT.
$filters = filter_get_available_in_context($this->childcontext);
$filters = filter_get_available_in_context($childcontext);
// Validate.
$this->assertEquals(array(), $filters);
}
@@ -467,23 +547,34 @@ class core_filterlib_testcase extends advanced_testcase {
* @expectedException coding_exception
*/
public function test_available_in_context_exception_with_syscontext() {
$this->setup_available_in_context_tests();
$this->resetAfterTest();
[
'syscontext' => $syscontext
] = $this->setup_available_in_context_tests();
// Exercise SUT.
filter_get_available_in_context($this->syscontext);
filter_get_available_in_context($syscontext);
}
protected function setup_preload_activities_test() {
$this->syscontext = context_system::instance();
$this->catcontext = context_coursecat::instance(1);
$this->course = $this->getDataGenerator()->create_course(array('category'=>1));
$this->coursecontext = context_course::instance($this->course->id);
$page1 = $this->getDataGenerator()->create_module('page', array('course'=>$this->course->id));
$this->activity1context = context_module::instance($page1->cmid);
$page2 = $this->getDataGenerator()->create_module('page', array('course'=>$this->course->id));
$this->activity2context = context_module::instance($page2->cmid);
$syscontext = context_system::instance();
$catcontext = context_coursecat::instance(1);
$course = $this->getDataGenerator()->create_course(array('category' => 1));
$coursecontext = context_course::instance($course->id);
$page1 = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
$activity1context = context_module::instance($page1->cmid);
$page2 = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
$activity2context = context_module::instance($page2->cmid);
return [
'syscontext' => $syscontext,
'catcontext' => $catcontext,
'course' => $course,
'coursecontext' => $coursecontext,
'activity1context' => $activity1context,
'activity2context' => $activity2context
];
}
private function assert_matches($modinfo) {
private function assert_matches($modinfo, $activity1context, $activity2context) {
global $FILTERLIB_PRIVATE, $DB;
// Use preload cache...
@@ -492,16 +583,16 @@ class core_filterlib_testcase extends advanced_testcase {
// Get data and check no queries are made.
$before = $DB->perf_get_reads();
$plfilters1 = filter_get_active_in_context($this->activity1context);
$plfilters2 = filter_get_active_in_context($this->activity2context);
$plfilters1 = filter_get_active_in_context($activity1context);
$plfilters2 = filter_get_active_in_context($activity2context);
$after = $DB->perf_get_reads();
$this->assertEquals($before, $after);
// Repeat without cache and check it makes queries now.
$FILTERLIB_PRIVATE = new stdClass;
$before = $DB->perf_get_reads();
$filters1 = filter_get_active_in_context($this->activity1context);
$filters2 = filter_get_active_in_context($this->activity2context);
$filters1 = filter_get_active_in_context($activity1context);
$filters2 = filter_get_active_in_context($activity2context);
$after = $DB->perf_get_reads();
$this->assertTrue($after > $before);
@@ -511,60 +602,69 @@ class core_filterlib_testcase extends advanced_testcase {
}
public function test_preload() {
$this->setup_preload_activities_test();
$this->resetAfterTest();
[
'catcontext' => $catcontext,
'course' => $course,
'coursecontext' => $coursecontext,
'activity1context' => $activity1context,
'activity2context' => $activity2context
] = $this->setup_preload_activities_test();
// Get course and modinfo.
$modinfo = new course_modinfo($this->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
// function without preloading.
// Initially, check with no filters enabled.
$this->assert_matches($modinfo);
$this->assert_matches($modinfo, $activity1context, $activity2context);
// Enable filter globally, check.
filter_set_global_state('name', TEXTFILTER_ON);
$this->assert_matches($modinfo);
$this->assert_matches($modinfo, $activity1context, $activity2context);
// Disable for activity 2.
filter_set_local_state('name', $this->activity2context->id, TEXTFILTER_OFF);
$this->assert_matches($modinfo);
filter_set_local_state('name', $activity2context->id, TEXTFILTER_OFF);
$this->assert_matches($modinfo, $activity1context, $activity2context);
// Disable at category.
filter_set_local_state('name', $this->catcontext->id, TEXTFILTER_OFF);
$this->assert_matches($modinfo);
filter_set_local_state('name', $catcontext->id, TEXTFILTER_OFF);
$this->assert_matches($modinfo, $activity1context, $activity2context);
// Enable for activity 1.
filter_set_local_state('name', $this->activity1context->id, TEXTFILTER_ON);
$this->assert_matches($modinfo);
filter_set_local_state('name', $activity1context->id, TEXTFILTER_ON);
$this->assert_matches($modinfo, $activity1context, $activity2context);
// Disable globally.
filter_set_global_state('name', TEXTFILTER_DISABLED);
$this->assert_matches($modinfo);
$this->assert_matches($modinfo, $activity1context, $activity2context);
// Add another 2 filters.
filter_set_global_state('frog', TEXTFILTER_ON);
filter_set_global_state('zombie', TEXTFILTER_ON);
$this->assert_matches($modinfo);
$this->assert_matches($modinfo, $activity1context, $activity2context);
// Disable random one of these in each context.
filter_set_local_state('zombie', $this->activity1context->id, TEXTFILTER_OFF);
filter_set_local_state('frog', $this->activity2context->id, TEXTFILTER_OFF);
$this->assert_matches($modinfo);
filter_set_local_state('zombie', $activity1context->id, TEXTFILTER_OFF);
filter_set_local_state('frog', $activity2context->id, TEXTFILTER_OFF);
$this->assert_matches($modinfo, $activity1context, $activity2context);
// Now do some filter options.
filter_set_local_config('name', $this->activity1context->id, 'a', 'x');
filter_set_local_config('zombie', $this->activity1context->id, 'a', 'y');
filter_set_local_config('frog', $this->activity1context->id, 'a', 'z');
filter_set_local_config('name', $activity1context->id, 'a', 'x');
filter_set_local_config('zombie', $activity1context->id, 'a', 'y');
filter_set_local_config('frog', $activity1context->id, 'a', 'z');
// These last two don't do anything as they are not at final level but I
// thought it would be good to have that verified in test.
filter_set_local_config('frog', $this->coursecontext->id, 'q', 'x');
filter_set_local_config('frog', $this->catcontext->id, 'q', 'z');
$this->assert_matches($modinfo);
filter_set_local_config('frog', $coursecontext->id, 'q', 'x');
filter_set_local_config('frog', $catcontext->id, 'q', 'z');
$this->assert_matches($modinfo, $activity1context, $activity2context);
}
public function test_filter_delete_all_for_filter() {
global $DB;
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
// Setup fixture.
filter_set_global_state('name', TEXTFILTER_ON);
@@ -588,6 +688,8 @@ class core_filterlib_testcase extends advanced_testcase {
public function test_filter_delete_all_for_context() {
global $DB;
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
// Setup fixture.
filter_set_global_state('name', TEXTFILTER_ON);
@@ -606,6 +708,7 @@ class core_filterlib_testcase extends advanced_testcase {
public function test_set() {
global $CFG;
$this->resetAfterTest();
$this->assertFileExists("$CFG->dirroot/filter/emailprotect"); // Any standard filter.
$this->assertFileExists("$CFG->dirroot/filter/tidy"); // Any standard filter.
@@ -631,6 +734,7 @@ class core_filterlib_testcase extends advanced_testcase {
public function test_unset_to_empty() {
global $CFG;
$this->resetAfterTest();
$this->assertFileExists("$CFG->dirroot/filter/tidy"); // Any standard filter.
@@ -646,6 +750,7 @@ class core_filterlib_testcase extends advanced_testcase {
public function test_unset_multi() {
global $CFG;
$this->resetAfterTest();
$this->assertFileExists("$CFG->dirroot/filter/emailprotect"); // Any standard filter.
$this->assertFileExists("$CFG->dirroot/filter/tidy"); // Any standard filter.
@@ -662,6 +767,7 @@ class core_filterlib_testcase extends advanced_testcase {
}
public function test_filter_manager_instance() {
$this->resetAfterTest();
set_config('perfdebug', 7);
filter_manager::reset_caches();
@@ -677,18 +783,26 @@ class core_filterlib_testcase extends advanced_testcase {
}
public function test_filter_get_globally_enabled_default() {
$this->resetAfterTest();
$enabledfilters = filter_get_globally_enabled();
$this->assertArrayNotHasKey('glossary', $enabledfilters);
}
public function test_filter_get_globally_enabled_after_change() {
$this->resetAfterTest();
filter_set_global_state('glossary', TEXTFILTER_ON);
$enabledfilters = filter_get_globally_enabled();
$this->assertArrayHasKey('glossary', $enabledfilters);
}
public function test_filter_get_globally_enabled_filters_with_config() {
$this->setup_available_in_context_tests();
$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
[
'syscontext' => $syscontext,
'childcontext' => $childcontext
] = $this->setup_available_in_context_tests();
$this->remove_all_filters_from_config(); // Remove all filters.
// Set few filters.
filter_set_global_state('one', TEXTFILTER_ON);
@@ -696,18 +810,18 @@ class core_filterlib_testcase extends advanced_testcase {
filter_set_global_state('two', TEXTFILTER_DISABLED);
// Set global config.
filter_set_local_config('one', $this->syscontext->id, 'test1a', 'In root');
filter_set_local_config('one', $this->syscontext->id, 'test1b', 'In root');
filter_set_local_config('two', $this->syscontext->id, 'test2a', 'In root');
filter_set_local_config('two', $this->syscontext->id, 'test2b', 'In root');
filter_set_local_config('one', $syscontext->id, 'test1a', 'In root');
filter_set_local_config('one', $syscontext->id, 'test1b', 'In root');
filter_set_local_config('two', $syscontext->id, 'test2a', 'In root');
filter_set_local_config('two', $syscontext->id, 'test2b', 'In root');
// Set child config.
filter_set_local_config('one', $this->childcontext->id, 'test1a', 'In child');
filter_set_local_config('one', $this->childcontext->id, 'test1b', 'In child');
filter_set_local_config('two', $this->childcontext->id, 'test2a', 'In child');
filter_set_local_config('two', $this->childcontext->id, 'test2b', 'In child');
filter_set_local_config('three', $this->childcontext->id, 'test3a', 'In child');
filter_set_local_config('three', $this->childcontext->id, 'test3b', 'In child');
filter_set_local_config('one', $childcontext->id, 'test1a', 'In child');
filter_set_local_config('one', $childcontext->id, 'test1b', 'In child');
filter_set_local_config('two', $childcontext->id, 'test2a', 'In child');
filter_set_local_config('two', $childcontext->id, 'test2b', 'In child');
filter_set_local_config('three', $childcontext->id, 'test3a', 'In child');
filter_set_local_config('three', $childcontext->id, 'test3b', 'In child');
// Check.
$actual = filter_get_globally_enabled_filters_with_config();