/** * Tests the get_year_for_tables function. */ public function test_get_year_for_tables() { $this->resetAfterTest(); // Create a couple of courses for testing. $course1 = $this->getDataGenerator()->create_course(array('startdate' => strtotime('2013-01-04 10:00'))); $course2 = $this->getDataGenerator()->create_course(array('startdate' => strtotime('2020-01-04 10:00'))); $course3 = $this->getDataGenerator()->create_course(array('startdate' => strtotime('2021-01-04 10:00'))); // System not turned on. $this->assertFalse(year_tables::get_year_for_tables()); // System turned on, initially transferring. Test with course and no course. set_config(year_tables::CONFIG_ENABLED, year_tables::ENABLED_TRANSFERRING, 'local_ousearch'); $this->assertFalse(year_tables::get_year_for_tables()); $this->assertFalse(year_tables::get_year_for_tables($course1)); // If any course is transferring, the non-course ones will return default. set_config(year_tables::CONFIG_TRANSFERRING_COURSE, $course1->id, 'local_ousearch'); $this->assertEquals(year_tables::NON_COURSE_YEAR, year_tables::get_year_for_tables()); $this->assertFalse(year_tables::get_year_for_tables($course1)); // Once course 1 is finished, course 2 will still return false and course 1 // should return its year. set_config(year_tables::CONFIG_TRANSFERRING_COURSE, $course2->id, 'local_ousearch'); $this->assertEquals(2013, year_tables::get_year_for_tables($course1)); $this->assertFalse(year_tables::get_year_for_tables($course2)); // Now we'll set it to mark that everything was transferred. set_config(year_tables::CONFIG_ENABLED, year_tables::ENABLED_ON, 'local_ousearch'); unset_config(year_tables::CONFIG_TRANSFERRING_COURSE, 'local_ousearch'); // 2020 should show a warning that we're running out of tables. $this->assertEquals(2020, year_tables::get_year_for_tables($course2)); $this->assertEquals(1, count(phpunit_util::get_debugging_messages())); phpunit_util::reset_debugging(); // 2021 should throw exception. try { year_tables::get_year_for_tables($course3); $this->fail(); } catch (moodle_exception $e) { $this->assertContains('start date beyond that supported by the OU search system', $e->getMessage()); } }
/** * Tests the info_section class (is_available, get_full_information). */ public function test_info_section() { global $DB; // Create a course. $this->setAdminUser(); $this->resetAfterTest(); $generator = $this->getDataGenerator(); $course = $generator->create_course(array('numsections' => 4), array('createsections' => true)); // Set up the availability option for the sections to mock options. $DB->set_field('course_sections', 'availability', '{"op":"|","show":true,"c":[' . '{"type":"mock","a":false,"m":"public"}]}', array('course' => $course->id, 'section' => 1)); $DB->set_field('course_sections', 'availability', '{"op":"|","show":true,"c":[' . '{"type":"mock","a":true,"m":"enemy"}]}', array('course' => $course->id, 'section' => 2)); // Third section is invalid. (Fourth has no availability setting.) $DB->set_field('course_sections', 'availability', '{{{', array('course' => $course->id, 'section' => 3)); $modinfo = get_fast_modinfo($course); $sections = $modinfo->get_section_info_all(); // Do availability and full information checks. $info = new info_section($sections[1]); $information = ''; $this->assertFalse($info->is_available($information)); $this->assertEquals('SA: public', $information); $this->assertEquals('SA: [FULL]public', $info->get_full_information()); $info = new info_section($sections[2]); $this->assertTrue($info->is_available($information)); $this->assertEquals('', $information); $this->assertEquals('SA: [FULL]enemy', $info->get_full_information()); // Check invalid one. $info = new info_section($sections[3]); $this->assertFalse($info->is_available($information)); $debugging = phpunit_util::get_debugging_messages(); phpunit_util::reset_debugging(); $this->assertEquals(1, count($debugging)); $this->assertContains('Invalid availability', $debugging[0]->message); // Check empty one. $info = new info_section($sections[4]); $this->assertTrue($info->is_available($information)); $this->assertEquals('', $information); $this->assertEquals('', $info->get_full_information()); }
/** * Return debugging messages from the current test. * @return array with instances having 'message', 'level' and 'stacktrace' property. */ public function getDebuggingMessages() { return phpunit_util::get_debugging_messages(); }
/** * Test is_user_access_restricted_by_conditional_access() * * The underlying conditional access system is more thoroughly tested in lib/tests/conditionlib_test.php */ public function test_is_user_access_restricted_by_conditional_access() { global $DB, $CFG; $this->resetAfterTest(); // Enable conditional availability before creating modules, otherwise the condition data is not written in DB. $CFG->enableavailability = true; // Create a course. $course = $this->getDataGenerator()->create_course(); // 1. Create an activity that is currently unavailable and hidden entirely (for students). $assign1 = $this->getDataGenerator()->create_module('assign', array('course' => $course->id), array('availability' => '{"op":"|","show":false,"c":[' . '{"type":"date","d":">=","t":' . (time() + 10000) . '}]}')); // 2. Create an activity that is currently available. $assign2 = $this->getDataGenerator()->create_module('assign', array('course' => $course->id)); // 3. Create an activity that is currently unavailable and set to be greyed out. $assign3 = $this->getDataGenerator()->create_module('assign', array('course' => $course->id), array('availability' => '{"op":"|","show":true,"c":[' . '{"type":"date","d":">=","t":' . (time() + 10000) . '}]}')); // Set up a teacher. $coursecontext = context_course::instance($course->id); $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'), '*', MUST_EXIST); $teacher = $this->getDataGenerator()->create_user(); role_assign($teacherrole->id, $teacher->id, $coursecontext); // If conditional availability is disabled the activity will always be unrestricted. $CFG->enableavailability = false; $cm = get_fast_modinfo($course)->instances['assign'][$assign1->id]; $this->assertTrue($cm->uservisible); // Test deprecated function. $this->assertFalse($cm->is_user_access_restricted_by_conditional_access()); $this->assertEquals(1, count(phpunit_util::get_debugging_messages())); phpunit_util::reset_debugging(); // Turn on conditional availability and reset the get_fast_modinfo cache. $CFG->enableavailability = true; get_fast_modinfo($course, 0, true); // The unavailable, hidden entirely activity should now be restricted. $cm = get_fast_modinfo($course)->instances['assign'][$assign1->id]; $this->assertFalse($cm->uservisible); $this->assertFalse($cm->available); $this->assertEquals('', $cm->availableinfo); // Test deprecated function. $this->assertTrue($cm->is_user_access_restricted_by_conditional_access()); $this->assertEquals(1, count(phpunit_util::get_debugging_messages())); phpunit_util::reset_debugging(); // If the activity is available it should not be restricted. $cm = get_fast_modinfo($course)->instances['assign'][$assign2->id]; $this->assertTrue($cm->uservisible); $this->assertTrue($cm->available); // If the activity is unavailable and set to be greyed out it should not be restricted. $cm = get_fast_modinfo($course)->instances['assign'][$assign3->id]; $this->assertFalse($cm->uservisible); $this->assertFalse($cm->available); $this->assertNotEquals('', (string) $cm->availableinfo); // Test deprecated function (weird case, it actually checks visibility). $this->assertFalse($cm->is_user_access_restricted_by_conditional_access()); $this->assertEquals(1, count(phpunit_util::get_debugging_messages())); phpunit_util::reset_debugging(); // If the activity is unavailable and set to be hidden entirely its restricted unless user has 'moodle/course:viewhiddenactivities'. // Switch to a teacher and reload the context info. $this->setUser($teacher); $this->assertTrue(has_capability('moodle/course:viewhiddenactivities', $coursecontext)); $cm = get_fast_modinfo($course)->instances['assign'][$assign1->id]; $this->assertTrue($cm->uservisible); $this->assertFalse($cm->available); }
/** * Call when no debugging() messages expected. * @param string $message */ public function assertDebuggingNotCalled($message = '') { $debugging = phpunit_util::get_debugging_messages(); $count = count($debugging); if ($message === '') { $message = 'Expectation failed, debugging() was triggered.'; } $this->assertEquals(0, $count, $message); }
/** * Test that the deprecated twf_is_subscribed accepts numeric twf IDs. */ public function test_twf_is_subscribed_numeric() { global $DB; $this->resetAfterTest(true); // Create a course, with a twf. $course = $this->getDataGenerator()->create_course(); $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE); $twf = $this->getDataGenerator()->create_module('twf', $options); // Create a user enrolled in the course as a students. list($author) = $this->helper_create_users($course, 1); // Check that the user is currently unsubscribed to the twf. $this->assertFalse(twf_is_subscribed($author->id, $twf->id)); $this->assertEquals(1, count(phpunit_util::get_debugging_messages())); phpunit_util::reset_debugging(); // It should match the result of when it's called with the twf object. $this->assertFalse(twf_is_subscribed($author->id, $twf)); $this->assertEquals(1, count(phpunit_util::get_debugging_messages())); phpunit_util::reset_debugging(); // And when the user is subscribed, we should also get the correct result. \mod_twf\subscriptions::subscribe_user($author->id, $twf); $this->assertTrue(twf_is_subscribed($author->id, $twf->id)); $this->assertEquals(1, count(phpunit_util::get_debugging_messages())); phpunit_util::reset_debugging(); // It should match the result of when it's called with the twf object. $this->assertTrue(twf_is_subscribed($author->id, $twf)); $this->assertEquals(1, count(phpunit_util::get_debugging_messages())); phpunit_util::reset_debugging(); }
public function test_ousearch_query() { global $DB; $this->resetAfterTest(); // Create a bunch of search documents within test_zombie plugin. self::$zombiedocuments = array(1 => (object) array('title' => 'Document title', 'content' => 'First zombie document'), 2 => (object) array('title' => 'Another title', 'content' => 'Title title first'), 3 => (object) array('title' => 'Document title', 'content' => 'Not a zombie document title'), 4 => (object) array('title' => 'Delete me', 'content' => 'Delete me'), 100 => (object) array('title' => 'Bottle quantity', 'content' => 'There are this many bottles on the wall: 0')); for ($i = 101; $i <= 199; $i++) { self::$zombiedocuments[$i] = self::$zombiedocuments[100]; self::$zombiedocuments[$i]->content = str_replace(': 0', ': ' . ($i - 100), self::$zombiedocuments[$i]->content); } foreach (self::$zombiedocuments as $key => $content) { $doc = new local_ousearch_document(); $doc->init_test('zombie'); $doc->set_int_refs($key); $doc->update($content->title, $content->content, null, null, null); } // Search for single unique term. $result = $this->do_zombie_query('not'); $this->assertTrue($result->success); $this->assertEquals(array(3), $this->get_result_ids($result)); // Search for nonexistent word. $result = $this->do_zombie_query('xyzzy'); $this->assertFalse($result->success); $this->assertEquals('xyzzy', $result->problemword); // Search for nothing. $result = $this->do_zombie_query(' '); $this->assertFalse($result->success); $this->assertEquals('', $result->problemword); // Search for pair of terms. $result = $this->do_zombie_query('first document'); $this->assertTrue($result->success); $this->assertEquals(array(1), $this->get_result_ids($result)); // Search for quoted terms. $result = $this->do_zombie_query('"title title"'); $this->assertTrue($result->success); $this->assertEquals(array(2), $this->get_result_ids($result)); // Negative terms. $result = $this->do_zombie_query('title -not'); $this->assertTrue($result->success); $this->assertEquals(array(1, 2), $this->get_result_ids($result)); // Negative quoted terms. $result = $this->do_zombie_query('title -"not frog"'); $this->assertTrue($result->success); $this->assertEquals(array(1, 2, 3), $this->get_result_ids($result)); $result = $this->do_zombie_query('title -"not a"'); $this->assertTrue($result->success); $this->assertEquals(array(1, 2), $this->get_result_ids($result)); // Deleting stale results (those which the module responsible can no // longer find). $before = $DB->count_records('local_ousearch_documents'); unset(self::$zombiedocuments[4]); $result = $this->do_zombie_query('delete'); $this->assertEquals(1, count(phpunit_util::get_debugging_messages())); phpunit_util::reset_debugging(); $this->assertTrue($result->success); $this->assertEquals(array(), $this->get_result_ids($result)); $this->assertEquals($before - 1, $DB->count_records('local_ousearch_documents')); // Ranking based on title vs content and number of occurrences. $result = $this->do_zombie_query('title'); $this->assertTrue($result->success); $this->assertEquals(array(1, 2, 3), $this->get_result_ids($result)); $this->assertEquals(2, $result->results[0]->intref1); $this->assertEquals(18, $result->results[0]->totalscore); // Managing result lists. $found = array(); $dbstart = 0; for ($i = 0; $i < 10; $i++) { $result = $this->do_zombie_query('bottles', $dbstart); $this->assertTrue($result->success); $this->assertEquals(10, count($result->results)); foreach ($result->results as $thing) { $found[$thing->intref1] = true; } $dbstart = $result->dbstart; } $this->assertEquals(100, count($found)); $result = $this->do_zombie_query('bottles', $dbstart); $this->assertTrue($result->success); $this->assertEquals(0, count($result->results)); }