From f59f03ad2d48c9a1cf022a2272a7421367a7b7a5 Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Sat, 21 Apr 2012 16:27:30 +0200 Subject: [PATCH] MDL-32569 drop more core simpletests that were already migrated --- blog/simpletest/testbloglib.php | 170 ----------- course/simpletest/testcourselib.php | 136 --------- .../simpletest/testfiltermediaplugin.php | 114 ------- filter/urltolink/simpletest/sample.txt | 16 - filter/urltolink/simpletest/testfilter.php | 201 ------------- grade/grading/simpletest/testlib.php | 172 ----------- grade/simpletest/testedittree.php | 57 ---- lib/ajax/simpletest/testajaxlib.php | 129 -------- lib/form/simpletest/testduration.php | 130 -------- rating/simpletest/testrating.php | 278 ------------------ 10 files changed, 1403 deletions(-) delete mode 100644 blog/simpletest/testbloglib.php delete mode 100644 course/simpletest/testcourselib.php delete mode 100644 filter/mediaplugin/simpletest/testfiltermediaplugin.php delete mode 100644 filter/urltolink/simpletest/sample.txt delete mode 100644 filter/urltolink/simpletest/testfilter.php delete mode 100644 grade/grading/simpletest/testlib.php delete mode 100644 grade/simpletest/testedittree.php delete mode 100644 lib/ajax/simpletest/testajaxlib.php delete mode 100644 lib/form/simpletest/testduration.php delete mode 100644 rating/simpletest/testrating.php diff --git a/blog/simpletest/testbloglib.php b/blog/simpletest/testbloglib.php deleted file mode 100644 index fdc379ee89aaf..0000000000000 --- a/blog/simpletest/testbloglib.php +++ /dev/null @@ -1,170 +0,0 @@ -. - - -/** - * Unit tests for blog - * - * @package moodlecore - * @subpackage blog - * @copyright 2009 Nicolas Connault - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -require_once($CFG->dirroot . '/blog/locallib.php'); -require_once($CFG->dirroot . '/blog/lib.php'); - -/** - * Test functions that rely on the DB tables - */ -class bloglib_test extends UnitTestCaseUsingDatabase { - - public static $includecoverage = array('blog/locallib.php', 'blog/lib.php'); - - private $courseid; // To store important ids to be used in tests - private $groupid; - private $userid; - private $tagid; - - public function setUp() { - parent::setUp(); - $this->create_test_tables(array('course', 'groups', 'context', 'user', 'modules', 'course_modules', 'post', 'tag'), 'lib'); - $this->switch_to_test_db(); - - // Create default course - $course = new stdClass(); - $course->category = 1; - $course->fullname = 'Anonymous test course'; - $course->shortname = 'ANON'; - $course->summary = ''; - $course->id = $this->testdb->insert_record('course', $course); - - // Create default group - $group = new stdClass(); - $group->courseid = $course->id; - $group->name = 'ANON'; - $group->id = $this->testdb->insert_record('groups', $group); - - // Create required contexts - $contexts = array(CONTEXT_SYSTEM => 1, CONTEXT_COURSE => $course->id, CONTEXT_MODULE => 1); - foreach ($contexts as $level => $instance) { - $context = new stdClass; - $context->contextlevel = $level; - $context->instanceid = $instance; - $context->path = 'not initialised'; - $context->depth = '13'; - $this->testdb->insert_record('context', $context); - } - - // Create default user - $user = new stdClass(); - $user->username = 'testuser'; - $user->confirmed = 1; - $user->firstname = 'Jimmy'; - $user->lastname = 'Kinnon'; - $user->id = $this->testdb->insert_record('user', $user); - - // Create default tag - $tag = new stdClass(); - $tag->userid = $user->id; - $tag->name = 'testtagname'; - $tag->rawname = 'Testtagname'; - $tag->tagtype = 'official'; - $tag->id = $this->testdb->insert_record('tag', $tag); - - // Create default post - $post = new stdClass(); - $post->userid = $user->id; - $post->groupid = $group->id; - $post->content = 'test post content text'; - $post->id = $this->testdb->insert_record('post', $post); - - // Grab important ids - $this->courseid = $course->id; - $this->groupid = $group->id; - $this->userid = $user->id; - $this->tagid = $tag->id; - } - - public function tearDown() { - parent::tearDown(); - } - - - public function test_overrides() { - - // Try all the filters at once: Only the entry filter is active - $filters = array('site' => 1, 'course' => $this->courseid, 'module' => 1, - 'group' => $this->groupid, 'user' => 1, 'tag' => 1, 'entry' => 1); - $blog_listing = new blog_listing($filters); - $this->assertFalse(array_key_exists('site', $blog_listing->filters)); - $this->assertFalse(array_key_exists('course', $blog_listing->filters)); - $this->assertFalse(array_key_exists('module', $blog_listing->filters)); - $this->assertFalse(array_key_exists('group', $blog_listing->filters)); - $this->assertFalse(array_key_exists('user', $blog_listing->filters)); - $this->assertFalse(array_key_exists('tag', $blog_listing->filters)); - $this->assertTrue(array_key_exists('entry', $blog_listing->filters)); - - // Again, but without the entry filter: This time, the tag, user and module filters are active - $filters = array('site' => 1, 'course' => $this->courseid, 'module' => 1, - 'group' => $this->groupid, 'user' => 1, 'tag' => 1); - $blog_listing = new blog_listing($filters); - $this->assertFalse(array_key_exists('site', $blog_listing->filters)); - $this->assertFalse(array_key_exists('course', $blog_listing->filters)); - $this->assertFalse(array_key_exists('group', $blog_listing->filters)); - $this->assertTrue(array_key_exists('module', $blog_listing->filters)); - $this->assertTrue(array_key_exists('user', $blog_listing->filters)); - $this->assertTrue(array_key_exists('tag', $blog_listing->filters)); - - // We should get the same result by removing the 3 inactive filters: site, course and group: - $filters = array('module' => 1, 'user' => 1, 'tag' => 1); - $blog_listing = new blog_listing($filters); - $this->assertFalse(array_key_exists('site', $blog_listing->filters)); - $this->assertFalse(array_key_exists('course', $blog_listing->filters)); - $this->assertFalse(array_key_exists('group', $blog_listing->filters)); - $this->assertTrue(array_key_exists('module', $blog_listing->filters)); - $this->assertTrue(array_key_exists('user', $blog_listing->filters)); - $this->assertTrue(array_key_exists('tag', $blog_listing->filters)); - - } - - // The following series of 'test_blog..' functions correspond to the blog_get_headers() function within blog/lib.php. - // Some cases are omitted due to the optional_param variables used. - - public function test_blog_get_headers_case_1() { - global $CFG, $PAGE, $OUTPUT; - $blog_headers = blog_get_headers(); - $this->assertEqual($blog_headers['heading'], get_string('siteblog', 'blog', 'ANON')); - } - - public function test_blog_get_headers_case_6() { - global $CFG, $PAGE, $OUTPUT; - $blog_headers = blog_get_headers($this->courseid, NULL, $this->userid); - $this->assertNotEqual($blog_headers['heading'], ''); - } - - public function test_blog_get_headers_case_7() { - global $CFG, $PAGE, $OUTPUT; - $blog_headers = blog_get_headers(NULL, 1); - $this->assertNotEqual($blog_headers['heading'], ''); - } - public function test_blog_get_headers_case_10() { - global $CFG, $PAGE, $OUTPUT; - $blog_headers = blog_get_headers($this->courseid); - $this->assertNotEqual($blog_headers['heading'], ''); - } -} diff --git a/course/simpletest/testcourselib.php b/course/simpletest/testcourselib.php deleted file mode 100644 index 857d13e4ba83a..0000000000000 --- a/course/simpletest/testcourselib.php +++ /dev/null @@ -1,136 +0,0 @@ -dirroot . '/course/lib.php'); - -class courselib_test extends UnitTestCase { - var $realDB; - public static $includecoverage = array('course/lib.php'); - - function setUp() { - global $DB; - Mock::generate(get_class($DB), 'mockDB'); - $this->realDB = $DB; - $DB = new mockDB(); - } - - function tearDown() { - global $DB; - $DB = $this->realDB; - } - - function testMoveSection() { - global $DB; - $course = new stdClass(); - $course->numsections = 10; - $course->id = 1; - $sections = array(20 => 0, 21 => 1, 22 => 2, 23 => 3, 24 => 4, 25 => 5); - - $DB->setReturnValueAt(0, 'get_records_menu', $sections); - $DB->expectAt(0, 'set_field', array('course_sections', 'section', 0, array('id' => 20))); - $DB->expectAt(1, 'set_field', array('course_sections', 'section', 1, array('id' => 21))); - $DB->expectAt(2, 'set_field', array('course_sections', 'section', 2, array('id' => 23))); - $DB->expectAt(3, 'set_field', array('course_sections', 'section', 3, array('id' => 24))); - $DB->expectAt(4, 'set_field', array('course_sections', 'section', 4, array('id' => 22))); - $DB->expectAt(5, 'set_field', array('course_sections', 'section', 5, array('id' => 25))); - move_section_to($course, 2, 4); - - $DB->setReturnValueAt(1, 'get_records_menu', $sections); - $DB->expectAt(6, 'set_field', array('course_sections', 'section', 0, array('id' => 20))); - $DB->expectAt(7, 'set_field', array('course_sections', 'section', 1, array('id' => 24))); - $DB->expectAt(8, 'set_field', array('course_sections', 'section', 2, array('id' => 21))); - $DB->expectAt(9, 'set_field', array('course_sections', 'section', 3, array('id' => 22))); - $DB->expectAt(10, 'set_field', array('course_sections', 'section', 4, array('id' => 23))); - $DB->expectAt(11, 'set_field', array('course_sections', 'section', 5, array('id' => 25))); - move_section_to($course, 4, 0); - } - - function testReorderSections() { - $sections = array(20 => 0, 21 => 1, 22 => 2, 23 => 3, 24 => 4, 25 => 5); - $this->assertFalse(reorder_sections(1,3,4)); - - $newsections = reorder_sections($sections, 2, 4); - $newsections_flipped = array_flip($newsections); - - $this->assertEqual(20, reset($newsections_flipped)); - $this->assertEqual(21, next($newsections_flipped)); - $this->assertEqual(23, next($newsections_flipped)); - $this->assertEqual(24, next($newsections_flipped)); - $this->assertEqual(22, next($newsections_flipped)); - $this->assertEqual(25, next($newsections_flipped)); - - $newsections = reorder_sections($sections, 4, 0); - $newsections_flipped = array_flip($newsections); - - $this->assertEqual(20, reset($newsections_flipped)); - $this->assertEqual(24, next($newsections_flipped)); - $this->assertEqual(21, next($newsections_flipped)); - $this->assertEqual(22, next($newsections_flipped)); - $this->assertEqual(23, next($newsections_flipped)); - $this->assertEqual(25, next($newsections_flipped)); - - $newsections = reorder_sections($sections, 1, 5); - $newsections_flipped = array_flip($newsections); - - $this->assertEqual(20, reset($newsections_flipped)); - $this->assertEqual(22, next($newsections_flipped)); - $this->assertEqual(23, next($newsections_flipped)); - $this->assertEqual(24, next($newsections_flipped)); - $this->assertEqual(25, next($newsections_flipped)); - $this->assertEqual(21, next($newsections_flipped)); - } - - function test_get_course_display_name_for_list() { - global $CFG; - - $course = (object)array('shortname' => 'FROG101', - 'fullname' => 'Introduction to pond life'); - - // Store config value in case other tests rely on it - $oldcfg = $CFG->courselistshortnames; - - $CFG->courselistshortnames = 0; - $this->assertEqual('Introduction to pond life', - get_course_display_name_for_list($course)); - - $CFG->courselistshortnames = 1; - $this->assertEqual('FROG101 Introduction to pond life', - get_course_display_name_for_list($course)); - - $CFG->courselistshortnames = $oldcfg; - } -} diff --git a/filter/mediaplugin/simpletest/testfiltermediaplugin.php b/filter/mediaplugin/simpletest/testfiltermediaplugin.php deleted file mode 100644 index e74bb2a482df8..0000000000000 --- a/filter/mediaplugin/simpletest/testfiltermediaplugin.php +++ /dev/null @@ -1,114 +0,0 @@ -. - -/** - * Unit test for the filter_mediaplugin - * - * @package filter - * @subpackage Mediaplugin - * @copyright 2011 Rossiani Wijaya - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -defined('MOODLE_INTERNAL') || die(); - -require_once($CFG->dirroot . '/filter/mediaplugin/filter.php'); // Include the code to test - -/** - * Test cases for filter_mediaplugin class - */ -class filter_mediaplugin_test extends UnitTestCase { - - function test_filter_mediaplugin_link() { - global $CFG; - - // we need to enable the plugins somehow - $oldcfg = clone($CFG); // very, very ugly hack - $CFG->filter_mediaplugin_enable_youtube = 1; - $CFG->filter_mediaplugin_enable_vimeo = 1; - $CFG->filter_mediaplugin_enable_mp3 = 1; - $CFG->filter_mediaplugin_enable_flv = 1; - $CFG->filter_mediaplugin_enable_swf = 1; - $CFG->filter_mediaplugin_enable_html5audio = 1; - $CFG->filter_mediaplugin_enable_html5video = 1; - $CFG->filter_mediaplugin_enable_qt = 1; - $CFG->filter_mediaplugin_enable_wmp = 1; - $CFG->filter_mediaplugin_enable_rm = 1; - - - $filterplugin = new filter_mediaplugin(null, array()); - - $validtexts = array ( - 'test mp3', - 'test ogg', - 'test mpg', - 'test', - 'test file', - 'test file', - 'test flv', - 'test file', - 'test mp3', - 'test mp3', - 'test mp3', - 'test mp3', - 'youtube\'s', - ' - test mp3', - 'test mp3 - ', - 'youtube\'s' - ); - - //test for valid link - foreach ($validtexts as $text) { - $msg = "Testing text: ". $text; - $filter = $filterplugin->filter($text); - $this->assertNotEqual($text, $filter, $msg); - } - - $invalidtexts = array( - 'href="http://moodle.org/testfile/test.mp3"', - 'test test', - 'test test', - 'test test', - 'test test', - 'sample', - '', - 'test', - 'test mp3', - '', - 'test', - 'test', - 'test mp3', - 'test mp3', - 'test mp3' - ); - - //test for invalid link - foreach ($invalidtexts as $text) { - $msg = "Testing text: ". $text; - $filter = $filterplugin->filter($text); - $this->assertEqual($text, $filter, $msg); - } - - $CFG = $oldcfg; // very, very ugly hack - } -} diff --git a/filter/urltolink/simpletest/sample.txt b/filter/urltolink/simpletest/sample.txt deleted file mode 100644 index 23a45fef8f30d..0000000000000 --- a/filter/urltolink/simpletest/sample.txt +++ /dev/null @@ -1,16 +0,0 @@ -http://www.lipsum.com -Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. -Why do we use it?dummy - -It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). - -Where does it come from? - -Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. - -The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham. -Where can I get some? - -There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc. -Wikipedia -http://www.lorem-ipsum.info/ diff --git a/filter/urltolink/simpletest/testfilter.php b/filter/urltolink/simpletest/testfilter.php deleted file mode 100644 index 2310378532808..0000000000000 --- a/filter/urltolink/simpletest/testfilter.php +++ /dev/null @@ -1,201 +0,0 @@ -. - -/** - * Unit test for the filter_urltolink - * - * @package filter - * @subpackage urltolink - * @copyright 2010 David Mudrak - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -defined('MOODLE_INTERNAL') || die(); - -require_once($CFG->dirroot . '/filter/urltolink/filter.php'); // Include the code to test - -/** - * Test subclass that makes all the protected methods we want to test public. - */ -class testable_filter_urltolink extends filter_urltolink { - public function __construct() { - } - public function convert_urls_into_links(&$text) { - parent::convert_urls_into_links($text); - } -} - -/** - * Test cases for filter_urltolink class - */ -class filter_urltolink_test extends UnitTestCase { - - /** - * Helper function that represents the legacy implementation - * of convert_urls_into_links() - */ - protected function old_convert_urls_into_links(&$text) { - /// Make lone URLs into links. eg http://moodle.com/ - $text = preg_replace("%([[:space:]]|^|\(|\[)([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])%i", - '$1$2://$3$4', $text); - /// eg www.moodle.com - $text = preg_replace("%([[:space:]]|^|\(|\[)www\.([^[:space:]]*)([[:alnum:]#?/&=])%i", - '$1www.$2$3', $text); - } - - function test_convert_urls_into_links() { - $texts = array ( - //just a url - 'http://moodle.org - URL' => 'http://moodle.org - URL', - 'www.moodle.org - URL' => 'www.moodle.org - URL', - //url with params - 'URL: http://moodle.org/s/i=1&j=2' => 'URL: http://moodle.org/s/i=1&j=2', - //url with escaped params - 'URL: www.moodle.org/s/i=1&j=2' => 'URL: www.moodle.org/s/i=1&j=2', - //https url with params - 'URL: https://moodle.org/s/i=1&j=2' => 'URL: https://moodle.org/s/i=1&j=2', - //url with port and params - 'URL: http://moodle.org:8080/s/i=1' => 'URL: http://moodle.org:8080/s/i=1', - //url in brackets - '(http://moodle.org) - URL' => '(http://moodle.org) - URL', - '(www.moodle.org) - URL' => '(www.moodle.org) - URL', - //url in square brackets - '[http://moodle.org] - URL' => '[http://moodle.org] - URL', - '[www.moodle.org] - URL' => '[www.moodle.org] - URL', - //url in brackets with anchor - '[http://moodle.org/main#anchor] - URL' => '[http://moodle.org/main#anchor] - URL', - '[www.moodle.org/main#anchor] - URL' => '[www.moodle.org/main#anchor] - URL', - //brackets within the url - 'URL: http://cc.org/url_(withpar)_go/?i=2' => 'URL: http://cc.org/url_(withpar)_go/?i=2', - 'URL: www.cc.org/url_(withpar)_go/?i=2' => 'URL: www.cc.org/url_(withpar)_go/?i=2', - 'URL: http://cc.org/url_(with)_(par)_go/?i=2' => 'URL: http://cc.org/url_(with)_(par)_go/?i=2', - 'URL: www.cc.org/url_(with)_(par)_go/?i=2' => 'URL: www.cc.org/url_(with)_(par)_go/?i=2', - 'http://en.wikipedia.org/wiki/Slash_(punctuation)'=>'http://en.wikipedia.org/wiki/Slash_(punctuation)', - 'http://en.wikipedia.org/wiki/%28#Parentheses_.28_.29 - URL' => 'http://en.wikipedia.org/wiki/%28#Parentheses_.28_.29 - URL', - 'http://en.wikipedia.org/wiki/(#Parentheses_.28_.29 - URL' => 'http://en.wikipedia.org/wiki/(#Parentheses_.28_.29 - URL', - //escaped brackets in url - 'http://en.wikipedia.org/wiki/Slash_%28punctuation%29'=>'http://en.wikipedia.org/wiki/Slash_%28punctuation%29', - //anchor tag - 'URL: http://moodle.org' => 'URL: http://moodle.org', - 'URL: www.moodle.org' => 'URL: www.moodle.org', - 'URL: http://moodle.org' => 'URL: http://moodle.org', - 'URL: www.moodle.org' => 'URL: www.moodle.org', - //escaped anchor tag. Commented out as part of MDL-21183 - //htmlspecialchars('escaped anchor tag www.moodle.org') => 'escaped anchor tag <a href="http://moodle.org"> www.moodle.org</a>', - //trailing fullstop - 'URL: http://moodle.org/s/i=1&j=2.' => 'URL: http://moodle.org/s/i=1&j=2.', - 'URL: www.moodle.org/s/i=1&j=2.' => 'URL: www.moodle.org/s/i=1&j=2.', - //trailing unmatched bracket - 'URL: http://moodle.org)
' => 'URL: http://moodle.org)
', - //partially escaped html - 'URL:

text www.moodle.org</p> text' => 'URL:

text www.moodle.org</p> text', - //decimal url parameter - 'URL: www.moodle.org?u=1.23' => 'URL: www.moodle.org?u=1.23', - //escaped space in url - 'URL: www.moodle.org?u=test+param&' => 'URL: www.moodle.org?u=test+param&', - //odd characters in url param - 'URL: www.moodle.org?param=:)' => 'URL: www.moodle.org?param=:)', - //multiple urls - 'URL: http://moodle.org www.moodle.org' - => 'URL: http://moodle.org www.moodle.org', - //containing anchor tags including a class parameter and a url to convert - 'URL: http://moodle.org www.moodle.org http://moodle.org' - => 'URL: http://moodle.org www.moodle.org http://moodle.org', - //subdomain - 'http://subdomain.moodle.org - URL' => 'http://subdomain.moodle.org - URL', - //multiple subdomains - 'http://subdomain.subdomain.moodle.org - URL' => 'http://subdomain.subdomain.moodle.org - URL', - //looks almost like a link but isnt - 'This contains http, http:// and www but no actual links.'=>'This contains http, http:// and www but no actual links.', - //no link at all - 'This is a story about moodle.coming to a cinema near you.'=>'This is a story about moodle.coming to a cinema near you.', - //URLs containing utf 8 characters - 'http://Iñtërnâtiônàlizætiøn.com?ô=nëø'=>'http://Iñtërnâtiônàlizætiøn.com?ô=nëø', - 'www.Iñtërnâtiônàlizætiøn.com?ô=nëø'=>'www.Iñtërnâtiônàlizætiøn.com?ô=nëø', - //text containing utf 8 characters outside of a url - 'Iñtërnâtiônàlizætiøn is important to http://moodle.org'=>'Iñtërnâtiônàlizætiøn is important to http://moodle.org', - //too hard to identify without additional regexs - 'moodle.org' => 'moodle.org', - //some text with no link between related html tags - 'no link here' => 'no link here', - //some text with a link between related html tags - 'a link here www.moodle.org' => 'a link here www.moodle.org', - //some text containing a link within unrelated tags - '
This is some text. www.moodle.com then some more text
' => '
This is some text. www.moodle.com then some more text
', - //check we aren't modifying img tags - 'image' => 'image', - 'image' => 'image', - //and another url within one tag - ' ' => ' ', - ' ' => ' ', - '

'=>'', - //partially escaped img tag - 'partially escaped img tag <img src="http://moodle.org/logo/logo-240x60.gif" />' => 'partially escaped img tag <img src="http://moodle.org/logo/logo-240x60.gif" />', - //fully escaped img tag. Commented out as part of MDL-21183 - //htmlspecialchars('fully escaped img tag ') => 'fully escaped img tag <img src="http://moodle.org/logo/logo-240x60.gif" />', - //Double http with www - 'One more link like http://www.moodle.org to test' => 'One more link like http://www.moodle.org to test', - //Encoded URLs in the path - 'URL: http://127.0.0.1/one%28parenthesis%29/path?param=value' => 'URL: http://127.0.0.1/one%28parenthesis%29/path?param=value', - 'URL: www.localhost.com/one%28parenthesis%29/path?param=value' => 'URL: www.localhost.com/one%28parenthesis%29/path?param=value', - //Encoded URLs in the query - 'URL: http://127.0.0.1/path/to?param=value_with%28parenthesis%29¶m2=1' => 'URL: http://127.0.0.1/path/to?param=value_with%28parenthesis%29¶m2=1', - 'URL: www.localhost.com/path/to?param=value_with%28parenthesis%29¶m2=1' => 'URL: www.localhost.com/path/to?param=value_with%28parenthesis%29¶m2=1', - //URLs in Javascript. Commented out as part of MDL-21183 - //'var url="http://moodle.org";'=>'var url="http://moodle.org";', - //'var url = "http://moodle.org";'=>'var url = "http://moodle.org";', - //'var url="www.moodle.org";'=>'var url="www.moodle.org";', - //'var url = "www.moodle.org";'=>'var url = "www.moodle.org";', - //doctype. do we care about this failing? - //''=>'' - ); - - $testablefilter = new testable_filter_urltolink(); - - foreach ($texts as $text => $correctresult) { - $msg = "Testing text: ". str_replace('%', '%%', $text) . ": %s"; // Escape original '%' so sprintf() wont get confused - - $testablefilter->convert_urls_into_links($text); - - $this->assertEqual($text, $correctresult, $msg); - } - - //performance testing - $reps = 1000; - $text = file_get_contents(dirname(__FILE__) . '/sample.txt'); - $time_start = microtime(true); - for($i=0;$i<$reps;$i++) { - $testablefilter->convert_urls_into_links($text); - } - $time_end = microtime(true); - $new_time = $time_end - $time_start; - - $time_start = microtime(true); - for($i=0;$i<$reps;$i++) { - $this->old_convert_urls_into_links($text); - } - $time_end = microtime(true); - $old_time = $time_end - $time_start; - - $fast_enough = false; - if( $new_time < $old_time ) { - $fast_enough = true; - } - - $this->assertEqual($fast_enough, true, 'Timing test: ' . $new_time . 'secs (new) < ' . $old_time . 'secs (old)'); - } -} diff --git a/grade/grading/simpletest/testlib.php b/grade/grading/simpletest/testlib.php deleted file mode 100644 index 3e489b8c4931b..0000000000000 --- a/grade/grading/simpletest/testlib.php +++ /dev/null @@ -1,172 +0,0 @@ -. - -/** - * Unit tests for the advanced grading subsystem - * - * @package core - * @subpackage grading - * @copyright 2011 David Mudrak - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -defined('MOODLE_INTERNAL') || die(); - -global $DB, $CFG; - -if (empty($CFG->unittestprefix)) { - die('You must define $CFG->unittestprefix to run these unit tests.'); -} - -if ($CFG->unittestprefix == $CFG->prefix) { - die('Eh? Do you play with the fire? Fireman Sam won\'t come dude. The unittestprefix must be different from the standard prefix.'); -} - -require_once($CFG->dirroot . '/grade/grading/lib.php'); // Include the code to test - -/** - * Makes protected method accessible for testing purposes - */ -class testable_grading_manager extends grading_manager { -} - -/** - * Test cases for the grading manager API - */ -class grading_manager_test extends UnitTestCase { - - /** @var moodle_database current real driver instance */ - protected $realDB; - - public function setUp() { - global $DB, $CFG; - - $this->realDB = $DB; - $dbclass = get_class($this->realDB); - $DB = new $dbclass(); - $DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->unittestprefix); - $dbman = $DB->get_manager(); - - // drop everything we have in the mock DB - $dbman->delete_tables_from_xmldb_file($CFG->dirroot . '/lib/db/install.xml'); - // create all tables we need for this test case - $dbman->install_one_table_from_xmldb_file($CFG->dirroot . '/lib/db/install.xml', 'grading_areas'); - } - - public function tearDown() { - global $DB, $CFG; - - // clean everything we have in the mock DB - //$DB->get_manager()->delete_tables_from_xmldb_file($CFG->dirroot . '/lib/db/install.xml'); - // switch to the real database - $DB = $this->realDB; - } - - public function test_basic_instantiation() { - - $manager1 = get_grading_manager(); - - $fakecontext = (object)array( - 'id' => 42, - 'contextlevel' => CONTEXT_MODULE, - 'instanceid' => 22, - 'path' => '/1/3/15/42', - 'depth' => 4); - - $manager2 = get_grading_manager($fakecontext); - $manager3 = get_grading_manager($fakecontext, 'assignment_upload'); - $manager4 = get_grading_manager($fakecontext, 'assignment_upload', 'submission'); - } - - public function test_set_and_get_grading_area() { - global $DB; - - sleep(2); // to make sure the microtime will always return unique values - $areaname1 = 'area1-' . (string)microtime(true); - $areaname2 = 'area2-' . (string)microtime(true); - $fakecontext = (object)array( - 'id' => 42, - 'contextlevel' => CONTEXT_MODULE, - 'instanceid' => 22, - 'path' => '/1/3/15/42', - 'depth' => 4); - - // non-existing area - $gradingman = get_grading_manager($fakecontext, 'mod_foobar', $areaname1); - $this->assertNull($gradingman->get_active_method()); - - // creates area implicitly and sets active method - $this->assertTrue($gradingman->set_active_method('rubric')); - $this->assertEqual('rubric', $gradingman->get_active_method()); - - // repeat setting of already set active method - $this->assertFalse($gradingman->set_active_method('rubric')); - - // switch the manager to another area - $gradingman->set_area($areaname2); - $this->assertNull($gradingman->get_active_method()); - - // switch back and ask again - $gradingman->set_area($areaname1); - $this->assertEqual('rubric', $gradingman->get_active_method()); - - // attempting to set an invalid method - $this->expectException('moodle_exception'); - $gradingman->set_active_method('no_one_should_ever_try_to_implement_a_method_with_this_silly_name'); - } - - public function test_tokenize() { - - $UTFfailuremessage = 'A test using UTF-8 characters has failed. Consider updating PHP and PHP\'s PCRE or INTL extensions (MDL-30494)'; - - $needle = " šašek, \n\n \r a král; \t"; - $tokens = testable_grading_manager::tokenize($needle); - $this->assertEqual(2, count($tokens), $UTFfailuremessage); - $this->assertTrue(in_array('šašek', $tokens), $UTFfailuremessage); - $this->assertTrue(in_array('král', $tokens), $UTFfailuremessage); - - $needle = ' " šašek a král " '; - $tokens = testable_grading_manager::tokenize($needle); - $this->assertEqual(1, count($tokens)); - $this->assertTrue(in_array('šašek a král', $tokens)); - - $needle = '""'; - $tokens = testable_grading_manager::tokenize($needle); - $this->assertTrue(empty($tokens)); - - $needle = '"0"'; - $tokens = testable_grading_manager::tokenize($needle); - $this->assertEqual(1, count($tokens)); - $this->assertTrue(in_array('0', $tokens)); - - $needle = 'Aha, then who\'s a bad guy here he?'; - $tokens = testable_grading_manager::tokenize($needle); - $this->assertEqual(8, count($tokens)); - $this->assertTrue(in_array('span', $tokens)); // Extracted the tag name - $this->assertTrue(in_array('Aha', $tokens)); - $this->assertTrue(in_array('who', $tokens)); // Removed the trailing 's - $this->assertTrue(!in_array('a', $tokens)); //Single letter token was dropped - $this->assertTrue(in_array('he', $tokens)); // Removed the trailing ? - - $needle = 'grammar, "english language"'; - $tokens = testable_grading_manager::tokenize($needle); - $this->assertTrue(in_array('grammar', $tokens)); - $this->assertTrue(in_array('english', $tokens)); - $this->assertTrue(in_array('language', $tokens)); - $this->assertTrue(!in_array('english language', $tokens)); // Quoting part of the string is not supported - } -} diff --git a/grade/simpletest/testedittree.php b/grade/simpletest/testedittree.php deleted file mode 100644 index 7f1b8caf8df74..0000000000000 --- a/grade/simpletest/testedittree.php +++ /dev/null @@ -1,57 +0,0 @@ -. - -/** - * Unit tests for grade/edit/tree/lib.php. - * - * @author Andrew Davis - * @license http://www.gnu.org/copyleft/gpl.html GNU Public License - * @package moodlecore - */ - - -if (!defined('MOODLE_INTERNAL')) { - die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page -} - -require_once($CFG->dirroot.'/grade/edit/tree/lib.php'); - -/** - * Tests grade_edit_tree (deals with the data on the categories and items page in the gradebook) - */ -class gradeedittreelib_test extends UnitTestCase { - var $courseid = 1; - var $context = null; - var $grade_edit_tree = null; - public static $includecoverage = array('grade/edit/tree/lib.php'); - - function setUp() { - } - - public function test_format_number() { - $numinput = array( 0, 1, 1.01, '1.010', 1.2345); - $numoutput = array(0.0, 1.0, 1.01, 1.01, 1.2345); - - for ($i=0; $iassertEqual(grade_edit_tree::format_number($numinput[$i]),$numoutput[$i],$msg); - } - } - -} - - diff --git a/lib/ajax/simpletest/testajaxlib.php b/lib/ajax/simpletest/testajaxlib.php deleted file mode 100644 index 261ef6266bf6e..0000000000000 --- a/lib/ajax/simpletest/testajaxlib.php +++ /dev/null @@ -1,129 +0,0 @@ -. - - -/** - * Unit tests for (some of) ../ajaxlib.php. - * - * @copyright 2009 Tim Hunt - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -if (!defined('MOODLE_INTERNAL')) { - die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page -} -require_once($CFG->libdir . '/ajax/ajaxlib.php'); - - -/** - * Unit tests for ../ajaxlib.php functions. - * - * @copyright 2008 Tim Hunt - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -class ajax_test extends UnitTestCase { - - var $user_agents = array( - 'MSIE' => array( - '5.5' => array('Windows 2000' => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)'), - '6.0' => array('Windows XP SP2' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'), - '7.0' => array('Windows XP SP2' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; YPC 3.0.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)') - ), - 'Firefox' => array( - '1.0.6' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.10) Gecko/20050716 Firefox/1.0.6'), - '1.5' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8) Gecko/20051107 Firefox/1.5'), - '1.5.0.1' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1'), - '2.0' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1', - 'Ubuntu Linux AMD64' => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1) Gecko/20060601 Firefox/2.0 (Ubuntu-edgy)') - ), - 'Safari' => array( - '312' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/312.1 (KHTML, like Gecko) Safari/312'), - '2.0' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/412 (KHTML, like Gecko) Safari/412') - ), - 'Opera' => array( - '8.51' => array('Windows XP' => 'Opera/8.51 (Windows NT 5.1; U; en)'), - '9.0' => array('Windows XP' => 'Opera/9.0 (Windows NT 5.1; U; en)', - 'Debian Linux' => 'Opera/9.01 (X11; Linux i686; U; en)') - ) - ); - - /** - * Uses the array of user agents to test ajax_lib::ajaxenabled - */ - function test_ajaxenabled() - { - global $CFG, $USER; - $CFG->enableajax = true; - $USER->ajax = true; - - // Should be true - $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP']; - $this->assertTrue(ajaxenabled()); - - $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['1.5']['Windows XP']; - $this->assertTrue(ajaxenabled()); - - $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X']; - $this->assertTrue(ajaxenabled()); - - $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP']; - $this->assertTrue(ajaxenabled()); - - $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2']; - $this->assertTrue(ajaxenabled()); - - // Should be false - $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['1.0.6']['Windows XP']; - $this->assertFalse(ajaxenabled()); - - $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['312']['Mac OS X']; - $this->assertFalse(ajaxenabled()); - - $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['8.51']['Windows XP']; - $this->assertFalse(ajaxenabled()); - - $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['5.5']['Windows 2000']; - $this->assertFalse(ajaxenabled()); - - // Test array of tested browsers - $tested_browsers = array('MSIE' => 6.0, 'Gecko' => 20061111); - $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP']; - $this->assertTrue(ajaxenabled($tested_browsers)); - - $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['7.0']['Windows XP SP2']; - $this->assertTrue(ajaxenabled($tested_browsers)); - - $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X']; - $this->assertFalse(ajaxenabled($tested_browsers)); - - $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP']; - $this->assertFalse(ajaxenabled($tested_browsers)); - - $tested_browsers = array('Safari' => 412, 'Opera' => 9.0); - $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP']; - $this->assertFalse(ajaxenabled($tested_browsers)); - - $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['7.0']['Windows XP SP2']; - $this->assertFalse(ajaxenabled($tested_browsers)); - - $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X']; - $this->assertTrue(ajaxenabled($tested_browsers)); - - $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP']; - $this->assertTrue(ajaxenabled($tested_browsers)); - } -} diff --git a/lib/form/simpletest/testduration.php b/lib/form/simpletest/testduration.php deleted file mode 100644 index 4b9446f351513..0000000000000 --- a/lib/form/simpletest/testduration.php +++ /dev/null @@ -1,130 +0,0 @@ -. - - -/** - * Unit tests for forms lib. - * - * This file contains all unit test related to forms library. - * - * @package core_form - * @copyright 2009 Tim Hunt - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -if (!defined('MOODLE_INTERNAL')) { - die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page -} - -global $CFG; -require_once($CFG->libdir . '/form/duration.php'); - -/** - * Unit tests for MoodleQuickForm_duration - * - * Contains test cases for testing MoodleQuickForm_duration - * - * @package core_form - * @category unittest - * @copyright 2009 Tim Hunt - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -class duration_form_element_test extends UnitTestCase { - /** @var MoodleQuickForm_duration Keeps reference of MoodleQuickForm_duration object */ - private $element; - - /** @var array Path of MoodleQuickForm_duration, to be analysed by the coverage report */ - public static $includecoverage = array('lib/form/duration.php'); - - /** - * Initalize test wide variable, it is called in start of the testcase - */ - function setUp() { - $this->element = new MoodleQuickForm_duration(); - } - - /** - * Clears the data set in the setUp() method call. - * @see duration_form_element_test::setUp() - */ - function tearDown() { - $this->element = null; - } - - /** - * Testcase for testing contructor. - */ - function test_constructor() { - // Test trying to create with an invalid unit. - $this->expectException(); - $this->element = new MoodleQuickForm_duration('testel', null, array('defaultunit' => 123)); - } - - /** - * Testcase for testing units (seconds, minutes, hours and days) - */ - function test_get_units() { - $units = $this->element->get_units(); - ksort($units); - $this->assertEqual($units, array(1 => get_string('seconds'), 60 => get_string('minutes'), - 3600 => get_string('hours'), 86400 => get_string('days'))); - } - - /** - * Testcase for testing conversion of seconds to the best possible unit - */ - function test_seconds_to_unit() { - $this->assertEqual($this->element->seconds_to_unit(0), array(0, 60)); // Zero minutes, for a nice default unit. - $this->assertEqual($this->element->seconds_to_unit(1), array(1, 1)); - $this->assertEqual($this->element->seconds_to_unit(3601), array(3601, 1)); - $this->assertEqual($this->element->seconds_to_unit(60), array(1, 60)); - $this->assertEqual($this->element->seconds_to_unit(180), array(3, 60)); - $this->assertEqual($this->element->seconds_to_unit(3600), array(1, 3600)); - $this->assertEqual($this->element->seconds_to_unit(7200), array(2, 3600)); - $this->assertEqual($this->element->seconds_to_unit(86400), array(1, 86400)); - $this->assertEqual($this->element->seconds_to_unit(90000), array(25, 3600)); - - $this->element = new MoodleQuickForm_duration('testel', null, array('defaultunit' => 86400)); - $this->assertEqual($this->element->seconds_to_unit(0), array(0, 86400)); // Zero minutes, for a nice default unit. - } - - /** - * Testcase to check generated timestamp - */ - function test_exportValue() { - $el = new MoodleQuickForm_duration('testel'); - $el->_createElements(); - $values = array('testel' => array('number' => 10, 'timeunit' => 1)); - $this->assertEqual($el->exportValue($values), array('testel' => 10)); - $values = array('testel' => array('number' => 3, 'timeunit' => 60)); - $this->assertEqual($el->exportValue($values), array('testel' => 180)); - $values = array('testel' => array('number' => 1.5, 'timeunit' => 60)); - $this->assertEqual($el->exportValue($values), array('testel' => 90)); - $values = array('testel' => array('number' => 2, 'timeunit' => 3600)); - $this->assertEqual($el->exportValue($values), array('testel' => 7200)); - $values = array('testel' => array('number' => 1, 'timeunit' => 86400)); - $this->assertEqual($el->exportValue($values), array('testel' => 86400)); - $values = array('testel' => array('number' => 0, 'timeunit' => 3600)); - $this->assertEqual($el->exportValue($values), array('testel' => 0)); - - $el = new MoodleQuickForm_duration('testel', null, array('optional' => true)); - $el->_createElements(); - $values = array('testel' => array('number' => 10, 'timeunit' => 1)); - $this->assertEqual($el->exportValue($values), array('testel' => 0)); - $values = array('testel' => array('number' => 20, 'timeunit' => 1, 'enabled' => 1)); - $this->assertEqual($el->exportValue($values), array('testel' => 20)); - } -} diff --git a/rating/simpletest/testrating.php b/rating/simpletest/testrating.php deleted file mode 100644 index 88cfc634cb141..0000000000000 --- a/rating/simpletest/testrating.php +++ /dev/null @@ -1,278 +0,0 @@ -. - -/** - * Unit tests for rating/lib.php - * - * @package moodlecore - * @subpackage rating - * @copyright 2011 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -defined('MOODLE_INTERNAL') || die(); - -// Include all the needed stuff -require_once($CFG->dirroot . '/rating/lib.php'); - -/** - * Unit test case for all the rating/lib.php requiring DB mockup & manipulation - */ -class rating_db_test extends UnitTestCaseUsingDatabase { - - public static $includecoverage = array( - 'rating/lib.php' - ); - - protected $testtables = array( - 'lib' => array( - 'rating', 'scale', 'context', 'capabilities', 'role_assignments', 'role_capabilities', 'course')); - - protected $syscontext; - protected $neededcaps = array('view', 'viewall', 'viewany', 'rate'); - protected $originaldefaultfrontpageroleid; - - public function setUp() { - global $CFG; - parent::setUp(); - - foreach ($this->testtables as $dir => $tables) { - $this->create_test_tables($tables, $dir); // Create tables - } - - $this->switch_to_test_db(); // Switch to test DB for all the execution - - // Nasty hack, recreate the system context record (the accesslib API does not allow to create it easily) - $this->create_system_context_record(); - // Reset all caches - accesslib_clear_all_caches_for_unit_testing(); - - $this->syscontext = get_context_instance(CONTEXT_SYSTEM); - - $this->fill_records(); - - // Ignore any frontpageroleid, that would require to crete more contexts - $this->originaldefaultfrontpageroleid = $CFG->defaultfrontpageroleid; - $CFG->defaultfrontpageroleid = null; - } - - public function tearDown() { - global $CFG; - // Recover original frontpageroleid - $CFG->defaultfrontpageroleid = $this->originaldefaultfrontpageroleid; - parent::tearDown(); - } - - private function fill_records() { - global $DB; - - // Add the capabilities used by ratings - foreach ($this->neededcaps as $neededcap) { - $this->testdb->insert_record('capabilities', (object)array('name' => 'moodle/rating:' . $neededcap, - 'contextlevel' => CONTEXT_COURSE)); - } - } - - /** - * Test the current get_ratings method main sql - */ - function test_get_ratings_sql() { - - // We load 3 items. Each is rated twice. For simplicity itemid == user id of the item owner - $ctxid = $this->syscontext->id; - $this->load_test_data('rating', - array('contextid', 'component', 'ratingarea', 'itemid', 'scaleid', 'rating', 'userid', 'timecreated', 'timemodified'), array( - - //user 1's items. Average == 2 - array( $ctxid , 'mod_forum', 'post', 1 , 10 , 1 , 2 , 1 , 1), - array( $ctxid , 'mod_forum', 'post', 1 , 10 , 3 , 3 , 1 , 1), - - //user 2's items. Average == 3 - array( $ctxid , 'mod_forum', 'post', 2 , 10 , 1 , 1 , 1 , 1), - array( $ctxid , 'mod_forum', 'post', 2 , 10 , 5 , 3 , 1 , 1), - - //user 3's items. Average == 4 - array( $ctxid , 'mod_forum', 'post', 3 , 10 , 3 , 1 , 1 , 1), - array( $ctxid , 'mod_forum', 'post', 3 , 10 , 5 , 2 , 1 , 1) - )); - - // a post (item) by user 1 (rated above by user 2 and 3 with average = 2) - $user1posts = array( - (object)array('id' => 1, 'userid' => 1, 'message' => 'hello')); - // a post (item) by user 2 (rated above by user 1 and 3 with average = 3) - $user2posts = array( - (object)array('id' => 2, 'userid' => 2, 'message' => 'world')); - // a post (item) by user 3 (rated above by user 1 and 2 with average = 4) - $user3posts = array( - (object)array('id' => 3, 'userid' => 3, 'message' => 'moodle')); - - // Prepare the default options - $defaultoptions = array ( - 'context' => $this->syscontext, - 'component' => 'mod_forum', - 'ratingarea' => 'post', - 'scaleid' => 10, - 'aggregate' => RATING_AGGREGATE_AVERAGE); - - $rm = new mockup_rating_manager(); - - // STEP 1: Retreive ratings using the current user - - // Get results for user 1's item (expected average 1 + 3 / 2 = 2) - $toptions = (object)array_merge($defaultoptions, array('items' => $user1posts)); - $result = $rm->get_ratings($toptions); - $this->assertEqual(count($result), count($user1posts)); - $this->assertEqual($result[0]->id, $user1posts[0]->id); - $this->assertEqual($result[0]->userid, $user1posts[0]->userid); - $this->assertEqual($result[0]->message, $user1posts[0]->message); - $this->assertEqual($result[0]->rating->count, 2); - $this->assertEqual($result[0]->rating->aggregate, 2); - // Note that $result[0]->rating->rating is somewhat random - // We didn't supply a user ID so $USER was used which will vary depending on who runs the tests - - // Get results for items of user 2 (expected average 1 + 5 / 2 = 3) - $toptions = (object)array_merge($defaultoptions, array('items' => $user2posts)); - $result = $rm->get_ratings($toptions); - $this->assertEqual(count($result), count($user2posts)); - $this->assertEqual($result[0]->id, $user2posts[0]->id); - $this->assertEqual($result[0]->userid, $user2posts[0]->userid); - $this->assertEqual($result[0]->message, $user2posts[0]->message); - $this->assertEqual($result[0]->rating->count, 2); - $this->assertEqual($result[0]->rating->aggregate, 3); - // Note that $result[0]->rating->rating is somewhat random - // We didn't supply a user ID so $USER was used which will vary depending on who runs the tests - - // Get results for items of user 3 (expected average 3 + 5 / 2 = 4) - $toptions = (object)array_merge($defaultoptions, array('items' => $user3posts)); - $result = $rm->get_ratings($toptions); - $this->assertEqual(count($result), count($user3posts)); - $this->assertEqual($result[0]->id, $user3posts[0]->id); - $this->assertEqual($result[0]->userid, $user3posts[0]->userid); - $this->assertEqual($result[0]->message, $user3posts[0]->message); - $this->assertEqual($result[0]->rating->count, 2); - $this->assertEqual($result[0]->rating->aggregate, 4); - // Note that $result[0]->rating->rating is somewhat random - // We didn't supply a user ID so $USER was used which will vary depending on who runs the tests - - // Get results for items of user 1 & 2 together (expected averages are 2 and 3, as tested above) - $posts = array_merge($user1posts, $user2posts); - $toptions = (object)array_merge($defaultoptions, array('items' => $posts)); - $result = $rm->get_ratings($toptions); - $this->assertEqual(count($result), count($posts)); - $this->assertEqual($result[0]->id, $posts[0]->id); - $this->assertEqual($result[0]->userid, $posts[0]->userid); - $this->assertEqual($result[0]->message, $posts[0]->message); - $this->assertEqual($result[0]->rating->count, 2); - $this->assertEqual($result[0]->rating->aggregate, 2); - // Note that $result[0]->rating->rating is somewhat random - // We didn't supply a user ID so $USER was used which will vary depending on who runs the tests - - $this->assertEqual($result[1]->id, $posts[1]->id); - $this->assertEqual($result[1]->userid, $posts[1]->userid); - $this->assertEqual($result[1]->message, $posts[1]->message); - $this->assertEqual($result[1]->rating->count, 2); - $this->assertEqual($result[1]->rating->aggregate, 3); - // Note that $result[0]->rating->rating is somewhat random - // We didn't supply a user ID so $USER was used which will vary depending on who runs the tests - - // STEP 2: Retrieve ratings by a specified user - // We still expect complete aggregations and counts - - // Get results for items of user 1 rated by user 2 (avg 2, rating 1) - $toptions = (object)array_merge($defaultoptions, array('items' => $user1posts, 'userid' => 2)); - $result = $rm->get_ratings($toptions); - $this->assertEqual(count($result), count($user1posts)); - $this->assertEqual($result[0]->id, $user1posts[0]->id); - $this->assertEqual($result[0]->userid, $user1posts[0]->userid); - $this->assertEqual($result[0]->message, $user1posts[0]->message); - $this->assertEqual($result[0]->rating->count, 2); - $this->assertEqual($result[0]->rating->aggregate, 2); - $this->assertEqual($result[0]->rating->rating, 1); //user 2 rated user 1 "1" - $this->assertEqual($result[0]->rating->userid, $toptions->userid); // Must be the passed userid - - // Get results for items of user 1 rated by user 3 - $toptions = (object)array_merge($defaultoptions, array('items' => $user1posts, 'userid' => 3)); - $result = $rm->get_ratings($toptions); - $this->assertEqual(count($result), count($user1posts)); - $this->assertEqual($result[0]->id, $user1posts[0]->id); - $this->assertEqual($result[0]->userid, $user1posts[0]->userid); - $this->assertEqual($result[0]->message, $user1posts[0]->message); - $this->assertEqual($result[0]->rating->count, 2); - $this->assertEqual($result[0]->rating->aggregate, 2); - $this->assertEqual($result[0]->rating->rating, 3); //user 3 rated user 1 "3" - $this->assertEqual($result[0]->rating->userid, $toptions->userid); // Must be the passed userid - - // Get results for items of user 1 & 2 together rated by user 3 - $posts = array_merge($user1posts, $user2posts); - $toptions = (object)array_merge($defaultoptions, array('items' => $posts, 'userid' => 3)); - $result = $rm->get_ratings($toptions); - $this->assertEqual(count($result), count($posts)); - $this->assertEqual($result[0]->id, $posts[0]->id); - $this->assertEqual($result[0]->userid, $posts[0]->userid); - $this->assertEqual($result[0]->message, $posts[0]->message); - $this->assertEqual($result[0]->rating->count, 2); - $this->assertEqual($result[0]->rating->aggregate, 2); - $this->assertEqual($result[0]->rating->rating, 3); //user 3 rated user 1 "3" - $this->assertEqual($result[0]->rating->userid, $toptions->userid); // Must be the passed userid - - $this->assertEqual($result[1]->id, $posts[1]->id); - $this->assertEqual($result[1]->userid, $posts[1]->userid); - $this->assertEqual($result[1]->message, $posts[1]->message); - $this->assertEqual($result[1]->rating->count, 2); - $this->assertEqual($result[1]->rating->aggregate, 3); - $this->assertEqual($result[0]->rating->rating, 3); //user 3 rated user 2 "5" - $this->assertEqual($result[1]->rating->userid, $toptions->userid); // Must be the passed userid - - // STEP 3: Some special cases - - // Get results for user 1's items (expected average 1 + 3 / 2 = 2) - // supplying a non-existent user id so no rating from that user should be found - $toptions = (object)array_merge($defaultoptions, array('items' => $user1posts)); - $toptions->userid = 123456; //non-existent user - $result = $rm->get_ratings($toptions); - $this->assertNull($result[0]->rating->userid); - $this->assertNull($result[0]->rating->rating); - $this->assertEqual($result[0]->rating->aggregate, 2);//should still get the aggregate - - // Get results for items of user 2 (expected average 1 + 5 / 2 = 3) - // Supplying the user id of the user who owns the items so no rating should be found - $toptions = (object)array_merge($defaultoptions, array('items' => $user2posts)); - $toptions->userid = 2; //user 2 viewing the ratings of their own item - $result = $rm->get_ratings($toptions); - //these should be null as the user is viewing their own item and thus cannot rate - $this->assertNull($result[0]->rating->userid); - $this->assertNull($result[0]->rating->rating); - $this->assertEqual($result[0]->rating->aggregate, 3);//should still get the aggregate - } -} - -/** - * rating_manager subclass for unit testing without requiring capabilities to be loaded - */ -class mockup_rating_manager extends rating_manager { - - /** - * Overwrite get_plugin_permissions_array() so it always return granted perms for unit testing - */ - public function get_plugin_permissions_array($contextid, $component, $ratingarea) { - return array( - 'rate' => true, - 'view' => true, - 'viewany' => true, - 'viewall' => true); - } - -}