public function execute() { global $CFG, $DB; require_once $CFG->libdir . '/adminlib.php'; // various admin-only functions require_once $CFG->libdir . '/classes/plugin_manager.php'; $filtername = $this->arguments[0]; $newstate = $this->arguments[1]; if ($newstate != 1 && $newstate != -1 && $newstate != -9999) { cli_error("Invalid filter value, use: 1 for on, -1 per course, -9999 for off"); } // Clean up bogus filter states first. $plugininfos = core_plugin_manager::instance()->get_plugins_of_type('filter'); $filters = array(); $states = filter_get_global_states(); foreach ($states as $state) { if (!isset($plugininfos[$state->filter]) and !get_config('filter_' . $state->filter, 'version')) { // Purge messy leftovers after incorrectly uninstalled plugins and unfinished installs. $DB->delete_records('filter_active', array('filter' => $state->filter)); $DB->delete_records('filter_config', array('filter' => $state->filter)); error_log('Deleted bogus "filter_' . $state->filter . '" states and config data.'); } else { $filters[$state->filter] = $state; } } if (!isset($filters[$filtername])) { cli_error("Invalid filter name: '{$filtername}''. Possible values: " . implode(",", array_keys($filters)) . '.'); } filter_set_global_state($filtername, $newstate); if ($newstate == TEXTFILTER_DISABLED) { filter_set_applies_to_strings($filtername, false); } reset_text_filters_cache(); core_plugin_manager::reset_caches(); echo "Updated {$filtername} to state = {$newstate}\n"; }
/** * Tests the get_mimetype_description function. */ public function test_get_mimetype_description() { $this->resetAfterTest(); // Test example type (.doc). $this->assertEquals(get_string('application/msword', 'mimetypes'), get_mimetype_description(array('filename' => 'test.doc'))); // Test an unknown file type. $this->assertEquals(get_string('document/unknown', 'mimetypes'), get_mimetype_description(array('filename' => 'test.frog'))); // Test a custom filetype with no lang string specified. core_filetypes::add_type('frog', 'application/x-frog', 'document'); $this->assertEquals('application/x-frog', get_mimetype_description(array('filename' => 'test.frog'))); // Test custom description. core_filetypes::update_type('frog', 'frog', 'application/x-frog', 'document', array(), '', 'Froggy file'); $this->assertEquals('Froggy file', get_mimetype_description(array('filename' => 'test.frog'))); // Test custom description using multilang filter. filter_set_global_state('multilang', TEXTFILTER_ON); filter_set_applies_to_strings('multilang', true); core_filetypes::update_type('frog', 'frog', 'application/x-frog', 'document', array(), '', '<span lang="en" class="multilang">Green amphibian</span>' . '<span lang="fr" class="multilang">Amphibian vert</span>'); $this->assertEquals('Green amphibian', get_mimetype_description(array('filename' => 'test.frog'))); }
public function test_external_format_string() { $this->resetAfterTest(); $settings = external_settings::get_instance(); $currentraw = $settings->get_raw(); $currentfilter = $settings->get_filter(); // Enable multilang filter to on content and heading. filter_set_global_state('multilang', TEXTFILTER_ON); filter_set_applies_to_strings('multilang', 1); $filtermanager = filter_manager::instance(); $filtermanager->reset_caches(); $settings->set_raw(true); $settings->set_filter(true); $context = context_system::instance(); $test = '<span lang="en" class="multilang">EN</span><span lang="fr" class="multilang">FR</span> ' . '<script>hi</script> <h3>there</h3>!'; $correct = $test; $this->assertSame($correct, external_format_string($test, $context->id)); $settings->set_raw(false); $settings->set_filter(false); $test = '<span lang="en" class="multilang">EN</span><span lang="fr" class="multilang">FR</span> ' . '<script>hi</script> <h3>there</h3>?'; $correct = 'ENFR hi there?'; $this->assertSame($correct, external_format_string($test, $context->id)); $settings->set_filter(true); $test = '<span lang="en" class="multilang">EN</span><span lang="fr" class="multilang">FR</span> ' . '<script>hi</script> <h3>there</h3>@'; $correct = 'EN hi there@'; $this->assertSame($correct, external_format_string($test, $context->id)); // Filters can be opted out. $test = '<span lang="en" class="multilang">EN</span><span lang="fr" class="multilang">FR</span> ' . '<script>hi</script> <h3>there</h3>%'; $correct = 'ENFR hi there%'; $this->assertSame($correct, external_format_string($test, $context->id, false, ['filter' => false])); $settings->set_raw($currentraw); $settings->set_filter($currentfilter); }
/** * Test a categories ability to resort courses. */ public function test_resort_courses() { $this->resetAfterTest(true); $generator = $this->getDataGenerator(); $category = $generator->create_category(); $course1 = $generator->create_course(array('category' => $category->id, 'idnumber' => '006-01', 'shortname' => 'Biome Study', 'fullname' => '<span lang="ar" class="multilang">' . 'دراسة منطقة إحيائية' . '</span><span lang="en" class="multilang">Biome Study</span>', 'timecreated' => '1000000001')); $course2 = $generator->create_course(array('category' => $category->id, 'idnumber' => '007-02', 'shortname' => 'Chemistry Revision', 'fullname' => 'Chemistry Revision', 'timecreated' => '1000000002')); $course3 = $generator->create_course(array('category' => $category->id, 'idnumber' => '007-03', 'shortname' => 'Swiss Rolls and Sunflowers', 'fullname' => 'Aarkvarks guide to Swiss Rolls and Sunflowers', 'timecreated' => '1000000003')); $course4 = $generator->create_course(array('category' => $category->id, 'idnumber' => '006-04', 'shortname' => 'Scratch', 'fullname' => '<a href="test.php">Basic Scratch</a>', 'timecreated' => '1000000004')); $c1 = (int) $course1->id; $c2 = (int) $course2->id; $c3 = (int) $course3->id; $c4 = (int) $course4->id; $coursecat = coursecat::get($category->id); $this->assertTrue($coursecat->resort_courses('idnumber')); $this->assertSame(array($c1, $c4, $c2, $c3), array_keys($coursecat->get_courses())); $this->assertTrue($coursecat->resort_courses('shortname')); $this->assertSame(array($c1, $c2, $c4, $c3), array_keys($coursecat->get_courses())); $this->assertTrue($coursecat->resort_courses('timecreated')); $this->assertSame(array($c1, $c2, $c3, $c4), array_keys($coursecat->get_courses())); try { // Enable the multilang filter and set it to apply to headings and content. filter_manager::reset_caches(); filter_set_global_state('multilang', TEXTFILTER_ON); filter_set_applies_to_strings('multilang', true); $expected = array($c3, $c4, $c1, $c2); } catch (coding_exception $ex) { $expected = array($c3, $c4, $c2, $c1); } $this->assertTrue($coursecat->resort_courses('fullname')); $this->assertSame($expected, array_keys($coursecat->get_courses())); }
redirect($returnurl); } } switch ($action) { case 'setstate': if ($newstate = optional_param('newstate', '', PARAM_INTEGER)) { filter_set_global_state($filterpath, $newstate); if ($newstate == TEXTFILTER_DISABLED) { filter_set_applies_to_strings($filterpath, false); } unset($newfilters[$filterpath]); } break; case 'setapplyto': $applytostrings = optional_param('stringstoo', false, PARAM_BOOL); filter_set_applies_to_strings($filterpath, $applytostrings); break; case 'down': if (isset($filters[$filterpath])) { $oldpos = $filters[$filterpath]->sortorder; if ($oldpos <= count($filters)) { filter_set_global_state($filterpath, $filters[$filterpath]->active, $oldpos + 1); } } break; case 'up': if (isset($filters[$filterpath])) { $oldpos = $filters[$filterpath]->sortorder; if ($oldpos >= 1) { filter_set_global_state($filterpath, $filters[$filterpath]->active, $oldpos - 1); }
public function test_unset_multi() { global $CFG; // Setup fixture. $CFG->filterall = 1; $CFG->stringfilters = 'filter/name,filter/other'; // Exercise SUT. filter_set_applies_to_strings('filter/name', false); // Validate. $this->assertEqual('filter/other', $CFG->stringfilters); $this->assertTrue($CFG->filterall); }
/** * Delete all the data in the database relating to a filter, prior to deleting it. * * @param string $filter The filter name, for example 'tex'. */ function filter_delete_all_for_filter($filter) { global $DB; filter_set_applies_to_strings($filter, false); unset_all_config_for_plugin('filter_' . $filter); $DB->delete_records('filter_active', array('filter' => $filter)); $DB->delete_records('filter_config', array('filter' => $filter)); }
public function test_unset_multi() { global $CFG; $this->assertFileExists("{$CFG->dirroot}/filter/emailprotect"); // Any standard filter. $this->assertFileExists("{$CFG->dirroot}/filter/tidy"); // Any standard filter. $this->assertFileExists("{$CFG->dirroot}/filter/multilang"); // Any standard filter. // Setup fixture. set_config('filterall', 1); set_config('stringfilters', 'emailprotect,tidy,multilang'); // Exercise SUT. filter_set_applies_to_strings('tidy', false); // Validate. $this->assertEquals('emailprotect,multilang', $CFG->stringfilters); $this->assertEquals(1, $CFG->filterall); }
/** * Validate that custom user fields are synched over to Moodle when PM user is created * during an import */ public function test_user_multi_custom_field_on_user_create() { global $CFG, $DB, $USER; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once elis::lib('data/customfield.class.php'); require_once elis::file('eliscore/fields/moodleprofile/custom_fields.php'); require_once elispm::file('accesslib.php'); require_once elispm::lib('data/user.class.php'); require_once $CFG->dirroot . '/user/profile/lib.php'; require_once $CFG->dirroot . '/user/profile/definelib.php'; require_once $CFG->dirroot . '/user/profile/field/menu/define.class.php'; $CFG->filterall = true; $USER = get_admin(); $context = context_user::instance($USER->id); /* $filternames = filter_get_all_installed(); ob_start(); var_dump($filternames); $tmp = ob_get_contents(); ob_end_clean(); error_log("test_user_multi_custom_field_on_user_create: all-filters => {$tmp}"); */ // Note: >= m25 filter paths no longer prefixed with 'filter/' filter_set_global_state('multilang', TEXTFILTER_ON); filter_set_applies_to_strings('multilang', true); $multilangoption1 = '<span class="multilang" lang="en">Male</span><span class="multilang" lang="pt_br">Masculino</span>' . '<span class="multilang" lang="es">Masculino</span>'; $multilangoption2 = '<span class="multilang" lang="en">Female</span><span class="multilang" lang="pt_br">Feminino</span>' . '<span class="multilang" lang="es">Femenino</span>'; // The associated Moodle user profile field. $profiledefinemenu = new profile_define_menu(); $data = new stdClass(); $data->datatype = 'menu'; $data->categoryid = 99999; $data->shortname = 'testfieldgender'; $data->name = 'testfieldgender'; $data->param1 = "{$multilangoption1}\n{$multilangoption2}"; $data->defaultdata = $multilangoption2; $profiledefinemenu->define_save($data); // Reset cached custom fields. $user = new user(); $user->reset_custom_field_list(); // Field category. $fieldcategory = new field_category(array('name' => 'testcategoryname')); $fieldcategory->save(); // Custom field. $field = new field(array('categoryid' => $fieldcategory->id, 'shortname' => 'testfieldgender', 'name' => 'testfieldgender', 'datatype' => 'text', 'multivalued' => 1)); $field->save(); // Field owners field_owner::ensure_field_owner_exists($field, 'moodle_profile'); $manualowneroptions = array('required' => 0, 'edit_capability' => '', 'view_capability' => '', 'control' => 'menu', 'options' => "{$multilangoption1}\n{$multilangoption2}"); field_owner::ensure_field_owner_exists($field, 'manual', $manualowneroptions); // Field context level assocation. $fieldcontextlevel = new field_contextlevel(array('fieldid' => $field->id, 'contextlevel' => CONTEXT_ELIS_USER)); $fieldcontextlevel->save(); // Run the user create action. $record = new stdClass(); $record->action = 'create'; $record->idnumber = 'testuseridnumber'; $record->username = '******'; $record->firstname = 'testuserfirstname'; $record->lastname = 'testuserlastname'; $record->email = '*****@*****.**'; $record->address = 'testuseraddress'; $record->city = 'testusercity'; $record->country = 'CA'; $record->language = 'en'; $record->testfieldgender = 'Male/Female'; $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); $importplugin->process_record('user', $record, 'bogus'); // Validation. $userid = $DB->get_field(user::TABLE, 'id', array('username' => 'testuserusername')); $user = new user($userid); $user->load(); $user = $user->to_object(); /* $datars = field_data::get_for_context_and_field(\local_elisprogram\context\user::instance($user->id), 'testfieldgender'); foreach ($datars as $data) { ob_start(); var_dump($data); $tmp = ob_get_contents(); ob_end_clean(); error_log("test_user_multi_custom_field_on_user_create: data => {$tmp}"); } */ /* ob_start(); var_dump($user); $tmp = ob_get_contents(); ob_end_clean(); error_log("test_user_multi_custom_field_on_user_create: user => {$tmp}"); */ $this->assertEquals(array($multilangoption1, $multilangoption2), $user->field_testfieldgender); }