MDL-39915 cleanup core_pluginlib testcases

This commit is contained in:
Petr Škoda 2013-07-28 00:14:14 +02:00
parent 6125d61530
commit 0d123bd71d
2 changed files with 306 additions and 348 deletions

View file

@ -15,7 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** /**
* Tests for the block_manager class in ../blocklib.php. * Tests for the moodle_page class.
* *
* @package core * @package core
* @category phpunit * @category phpunit
@ -30,24 +30,7 @@ require_once($CFG->libdir . '/pagelib.php');
require_once($CFG->libdir . '/blocklib.php'); require_once($CFG->libdir . '/blocklib.php');
/** Test-specific subclass to make some protected things public. */ class core_moodle_page_testcase extends advanced_testcase {
class testable_moodle_page extends moodle_page {
public function initialise_default_pagetype($script = null) {
parent::initialise_default_pagetype($script);
}
public function url_to_class_name($url) {
return parent::url_to_class_name($url);
}
public function all_editing_caps() {
return parent::all_editing_caps();
}
}
/**
* Test functions that don't need to touch the database.
*/
class moodle_page_test extends advanced_testcase {
protected $testpage; protected $testpage;
public function setUp() { public function setUp() {
@ -58,77 +41,77 @@ class moodle_page_test extends advanced_testcase {
public function test_course_returns_site_before_set() { public function test_course_returns_site_before_set() {
global $SITE; global $SITE;
// Validate // Validated.
$this->assertSame($SITE, $this->testpage->course); $this->assertSame($SITE, $this->testpage->course);
} }
public function test_setting_course_works() { public function test_setting_course_works() {
// Setup fixture // Setup fixture.
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
$this->testpage->set_context(context_system::instance()); // Avoid trying to set the context. $this->testpage->set_context(context_system::instance()); // Avoid trying to set the context.
// Exercise SUT // Exercise SUT.
$this->testpage->set_course($course); $this->testpage->set_course($course);
// Validate // Validated.
$this->assertEquals($course, $this->testpage->course); $this->assertEquals($course, $this->testpage->course);
} }
public function test_global_course_and_page_course_are_same_with_global_page() { public function test_global_course_and_page_course_are_same_with_global_page() {
global $COURSE, $PAGE; global $COURSE, $PAGE;
// Setup fixture // Setup fixture.
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
$this->testpage->set_context(context_system::instance()); // Avoid trying to set the context. $this->testpage->set_context(context_system::instance()); // Avoid trying to set the context.
$PAGE = $this->testpage; $PAGE = $this->testpage;
// Exercise SUT // Exercise SUT.
$this->testpage->set_course($course); $this->testpage->set_course($course);
// Validate // Validated.
$this->assertSame($this->testpage->course, $COURSE); $this->assertSame($COURSE, $this->testpage->course);
} }
public function test_global_course_not_changed_with_non_global_page() { public function test_global_course_not_changed_with_non_global_page() {
global $COURSE; global $COURSE;
$originalcourse = $COURSE; $originalcourse = $COURSE;
// Setup fixture // Setup fixture.
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
$this->testpage->set_context(context_system::instance()); // Avoid trying to set the context. $this->testpage->set_context(context_system::instance()); // Avoid trying to set the context.
// Exercise SUT // Exercise SUT.
$this->testpage->set_course($course); $this->testpage->set_course($course);
// Validate // Validated.
$this->assertSame($originalcourse, $COURSE); $this->assertSame($originalcourse, $COURSE);
} }
public function test_cannot_set_course_once_theme_set() { public function test_cannot_set_course_once_theme_set() {
// Setup fixture // Setup fixture.
$this->testpage->force_theme(theme_config::DEFAULT_THEME); $this->testpage->force_theme(theme_config::DEFAULT_THEME);
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
// Set expectation. // Set expectation.
$this->setExpectedException('coding_exception'); $this->setExpectedException('coding_exception');
// Exercise SUT // Exercise SUT.
$this->testpage->set_course($course); $this->testpage->set_course($course);
} }
public function test_cannot_set_category_once_theme_set() { public function test_cannot_set_category_once_theme_set() {
// Setup fixture // Setup fixture.
$this->testpage->force_theme(theme_config::DEFAULT_THEME); $this->testpage->force_theme(theme_config::DEFAULT_THEME);
// Set expectation. // Set expectation.
$this->setExpectedException('coding_exception'); $this->setExpectedException('coding_exception');
// Exercise SUT // Exercise SUT.
$this->testpage->set_category_by_id(123); $this->testpage->set_category_by_id(123);
} }
public function test_cannot_set_category_once_course_set() { public function test_cannot_set_category_once_course_set() {
// Setup fixture // Setup fixture.
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
$this->testpage->set_context(context_system::instance()); // Avoid trying to set the context. $this->testpage->set_context(context_system::instance()); // Avoid trying to set the context.
$this->testpage->set_course($course); $this->testpage->set_course($course);
// Set expectation. // Set expectation.
$this->setExpectedException('coding_exception'); $this->setExpectedException('coding_exception');
// Exercise SUT // Exercise SUT.
$this->testpage->set_category_by_id(123); $this->testpage->set_category_by_id(123);
} }
public function test_categories_array_empty_for_front_page() { public function test_categories_array_empty_for_front_page() {
global $SITE; global $SITE;
// Setup fixture // Setup fixture.
$this->testpage->set_context(context_system::instance()); // Avoid trying to set the context. $this->testpage->set_context(context_system::instance()); // Avoid trying to set the context.
$this->testpage->set_course($SITE); $this->testpage->set_course($SITE);
// Exercise SUT and validate. // Exercise SUT and validate.
@ -155,12 +138,12 @@ class moodle_page_test extends advanced_testcase {
public function test_set_state_cannot_skip_one() { public function test_set_state_cannot_skip_one() {
// Set expectation. // Set expectation.
$this->setExpectedException('coding_exception'); $this->setExpectedException('coding_exception');
// Exercise SUT // Exercise SUT.
$this->testpage->set_state(moodle_page::STATE_IN_BODY); $this->testpage->set_state(moodle_page::STATE_IN_BODY);
} }
public function test_header_printed_false_initially() { public function test_header_printed_false_initially() {
// Validate // Validated.
$this->assertFalse($this->testpage->headerprinted); $this->assertFalse($this->testpage->headerprinted);
} }
@ -169,367 +152,324 @@ class moodle_page_test extends advanced_testcase {
$this->testpage->set_context(context_system::instance()); $this->testpage->set_context(context_system::instance());
$this->testpage->set_course($course); $this->testpage->set_course($course);
// Exercise SUT // Exercise SUT.
$this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER); $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER);
$this->testpage->set_state(moodle_page::STATE_IN_BODY); $this->testpage->set_state(moodle_page::STATE_IN_BODY);
// Validate // Validated.
$this->assertTrue($this->testpage->headerprinted); $this->assertTrue($this->testpage->headerprinted);
} }
public function test_set_context() { public function test_set_context() {
// Setup fixture // Setup fixture.
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
$context = context_course::instance($course->id); $context = context_course::instance($course->id);
// Exercise SUT // Exercise SUT.
$this->testpage->set_context($context); $this->testpage->set_context($context);
// Validate // Validated.
$this->assertSame($context, $this->testpage->context); $this->assertSame($context, $this->testpage->context);
} }
public function test_pagetype_defaults_to_script() { public function test_pagetype_defaults_to_script() {
global $SCRIPT; global $SCRIPT;
// Exercise SUT and validate // Exercise SUT and validate.
$SCRIPT = '/index.php'; $SCRIPT = '/index.php';
$this->testpage->initialise_default_pagetype(); $this->testpage->initialise_default_pagetype();
$this->assertEquals('site-index', $this->testpage->pagetype); $this->assertSame('site-index', $this->testpage->pagetype);
} }
public function test_set_pagetype() { public function test_set_pagetype() {
// Exercise SUT // Exercise SUT.
$this->testpage->set_pagetype('a-page-type'); $this->testpage->set_pagetype('a-page-type');
// Validate // Validated.
$this->assertEquals('a-page-type', $this->testpage->pagetype); $this->assertSame('a-page-type', $this->testpage->pagetype);
} }
public function test_initialise_default_pagetype() { public function test_initialise_default_pagetype() {
// Exercise SUT // Exercise SUT.
$this->testpage->initialise_default_pagetype('admin/tool/unittest/index.php'); $this->testpage->initialise_default_pagetype('admin/tool/unittest/index.php');
// Validate // Validated.
$this->assertEquals('admin-tool-unittest-index', $this->testpage->pagetype); $this->assertSame('admin-tool-unittest-index', $this->testpage->pagetype);
} }
public function test_initialise_default_pagetype_fp() { public function test_initialise_default_pagetype_fp() {
// Exercise SUT // Exercise SUT.
$this->testpage->initialise_default_pagetype('index.php'); $this->testpage->initialise_default_pagetype('index.php');
// Validate // Validated.
$this->assertEquals('site-index', $this->testpage->pagetype); $this->assertSame('site-index', $this->testpage->pagetype);
} }
public function test_get_body_classes_empty() { public function test_get_body_classes_empty() {
// Validate // Validated.
$this->assertEquals('', $this->testpage->bodyclasses); $this->assertSame('', $this->testpage->bodyclasses);
} }
public function test_get_body_classes_single() { public function test_get_body_classes_single() {
// Exercise SUT // Exercise SUT.
$this->testpage->add_body_class('aclassname'); $this->testpage->add_body_class('aclassname');
// Validate // Validated.
$this->assertEquals('aclassname', $this->testpage->bodyclasses); $this->assertSame('aclassname', $this->testpage->bodyclasses);
} }
public function test_get_body_classes() { public function test_get_body_classes() {
// Exercise SUT // Exercise SUT.
$this->testpage->add_body_classes(array('aclassname', 'anotherclassname')); $this->testpage->add_body_classes(array('aclassname', 'anotherclassname'));
// Validate // Validated.
$this->assertEquals('aclassname anotherclassname', $this->testpage->bodyclasses); $this->assertSame('aclassname anotherclassname', $this->testpage->bodyclasses);
} }
public function test_url_to_class_name() { public function test_url_to_class_name() {
$this->assertEquals('example-com', $this->testpage->url_to_class_name('http://example.com')); $this->assertSame('example-com', $this->testpage->url_to_class_name('http://example.com'));
$this->assertEquals('example-com--80', $this->testpage->url_to_class_name('http://example.com:80')); $this->assertSame('example-com--80', $this->testpage->url_to_class_name('http://example.com:80'));
$this->assertEquals('example-com--moodle', $this->testpage->url_to_class_name('https://example.com/moodle')); $this->assertSame('example-com--moodle', $this->testpage->url_to_class_name('https://example.com/moodle'));
$this->assertEquals('example-com--8080--nested-moodle', $this->testpage->url_to_class_name('https://example.com:8080/nested/moodle')); $this->assertSame('example-com--8080--nested-moodle', $this->testpage->url_to_class_name('https://example.com:8080/nested/moodle'));
} }
public function test_set_docs_path() { public function test_set_docs_path() {
// Exercise SUT // Exercise SUT.
$this->testpage->set_docs_path('a/file/path'); $this->testpage->set_docs_path('a/file/path');
// Validate // Validated.
$this->assertEquals('a/file/path', $this->testpage->docspath); $this->assertSame('a/file/path', $this->testpage->docspath);
} }
public function test_docs_path_defaults_from_pagetype() { public function test_docs_path_defaults_from_pagetype() {
// Exercise SUT // Exercise SUT.
$this->testpage->set_pagetype('a-page-type'); $this->testpage->set_pagetype('a-page-type');
// Validate // Validated.
$this->assertEquals('a/page/type', $this->testpage->docspath); $this->assertSame('a/page/type', $this->testpage->docspath);
} }
public function test_set_url_root() { public function test_set_url_root() {
global $CFG; global $CFG;
// Exercise SUT // Exercise SUT.
$this->testpage->set_url('/'); $this->testpage->set_url('/');
// Validate // Validated.
$this->assertEquals($CFG->wwwroot . '/', $this->testpage->url->out()); $this->assertSame($CFG->wwwroot . '/', $this->testpage->url->out());
} }
public function test_set_url_one_param() { public function test_set_url_one_param() {
global $CFG; global $CFG;
// Exercise SUT // Exercise SUT.
$this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123)); $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123));
// Validate // Validated.
$this->assertEquals($CFG->wwwroot . '/mod/quiz/attempt.php?attempt=123', $this->testpage->url->out()); $this->assertSame($CFG->wwwroot . '/mod/quiz/attempt.php?attempt=123', $this->testpage->url->out());
} }
public function test_set_url_two_params() { public function test_set_url_two_params() {
global $CFG; global $CFG;
// Exercise SUT // Exercise SUT.
$this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7)); $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
// Validate // Validated.
$this->assertEquals($CFG->wwwroot . '/mod/quiz/attempt.php?attempt=123&amp;page=7', $this->testpage->url->out()); $this->assertSame($CFG->wwwroot . '/mod/quiz/attempt.php?attempt=123&amp;page=7', $this->testpage->url->out());
} }
public function test_set_url_using_moodle_url() { public function test_set_url_using_moodle_url() {
global $CFG; global $CFG;
// Fixture setup // Fixture setup.
$url = new moodle_url('/mod/workshop/allocation.php', array('cmid' => 29, 'method' => 'manual')); $url = new moodle_url('/mod/workshop/allocation.php', array('cmid' => 29, 'method' => 'manual'));
// Exercise SUT // Exercise SUT.
$this->testpage->set_url($url); $this->testpage->set_url($url);
// Validate // Validated.
$this->assertEquals($CFG->wwwroot . '/mod/workshop/allocation.php?cmid=29&amp;method=manual', $this->testpage->url->out()); $this->assertSame($CFG->wwwroot . '/mod/workshop/allocation.php?cmid=29&amp;method=manual', $this->testpage->url->out());
} }
public function test_set_url_sets_page_type() { public function test_set_url_sets_page_type() {
// Exercise SUT // Exercise SUT.
$this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7)); $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
// Validate // Validated.
$this->assertEquals('mod-quiz-attempt', $this->testpage->pagetype); $this->assertSame('mod-quiz-attempt', $this->testpage->pagetype);
} }
public function test_set_url_does_not_change_explicit_page_type() { public function test_set_url_does_not_change_explicit_page_type() {
// Setup fixture // Setup fixture.
$this->testpage->set_pagetype('a-page-type'); $this->testpage->set_pagetype('a-page-type');
// Exercise SUT // Exercise SUT.
$this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7)); $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7));
// Validate // Validated.
$this->assertEquals('a-page-type', $this->testpage->pagetype); $this->assertSame('a-page-type', $this->testpage->pagetype);
} }
public function test_set_subpage() { public function test_set_subpage() {
// Exercise SUT // Exercise SUT.
$this->testpage->set_subpage('somestring'); $this->testpage->set_subpage('somestring');
// Validate // Validated.
$this->assertEquals('somestring', $this->testpage->subpage); $this->assertSame('somestring', $this->testpage->subpage);
} }
public function test_set_heading() { public function test_set_heading() {
// Exercise SUT // Exercise SUT.
$this->testpage->set_heading('a heading'); $this->testpage->set_heading('a heading');
// Validate // Validated.
$this->assertEquals('a heading', $this->testpage->heading); $this->assertSame('a heading', $this->testpage->heading);
} }
public function test_set_title() { public function test_set_title() {
// Exercise SUT // Exercise SUT.
$this->testpage->set_title('a title'); $this->testpage->set_title('a title');
// Validate // Validated.
$this->assertEquals('a title', $this->testpage->title); $this->assertSame('a title', $this->testpage->title);
} }
public function test_default_pagelayout() { public function test_default_pagelayout() {
// Exercise SUT and Validate // Exercise SUT and Validate.
$this->assertEquals('base', $this->testpage->pagelayout); $this->assertSame('base', $this->testpage->pagelayout);
} }
public function test_set_pagelayout() { public function test_set_pagelayout() {
// Exercise SUT // Exercise SUT.
$this->testpage->set_pagelayout('type'); $this->testpage->set_pagelayout('type');
// Validate // Validated.
$this->assertEquals('type', $this->testpage->pagelayout); $this->assertSame('type', $this->testpage->pagelayout);
}
}
/**
* Test functions that rely on the context table.
*/
class moodle_page_with_context_table_test extends advanced_testcase {
protected $testpage;
protected function setUp() {
parent::setUp();
$this->testpage = new moodle_page();
$this->resetAfterTest();
} }
public function test_setting_course_sets_context() { public function test_setting_course_sets_context() {
// Setup fixture // Setup fixture.
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
$context = context_course::instance($course->id); $context = context_course::instance($course->id);
// Exercise SUT // Exercise SUT.
$this->testpage->set_course($course); $this->testpage->set_course($course);
// Validate // Validated.
$this->assertSame($context, $this->testpage->context); $this->assertSame($context, $this->testpage->context);
} }
}
/**
* Test functions that rely on the context table.
*/
class moodle_page_categories_test extends advanced_testcase {
protected $testpage;
protected function setUp() {
parent::setUp();
$this->testpage = new moodle_page();
$this->resetAfterTest();
}
public function test_set_category_top_level() { public function test_set_category_top_level() {
global $DB; global $DB;
// Setup fixture // Setup fixture.
$cat = $this->getDataGenerator()->create_category(); $cat = $this->getDataGenerator()->create_category();
$catdbrecord = $DB->get_record('course_categories', array('id' => $cat->id)); $catdbrecord = $DB->get_record('course_categories', array('id' => $cat->id));
// Exercise SUT // Exercise SUT.
$this->testpage->set_category_by_id($cat->id); $this->testpage->set_category_by_id($cat->id);
// Validate // Validated.
$this->assertEquals($catdbrecord, $this->testpage->category); $this->assertEquals($catdbrecord, $this->testpage->category);
$this->assertSame(context_coursecat::instance($cat->id), $this->testpage->context); $this->assertSame(context_coursecat::instance($cat->id), $this->testpage->context);
} }
public function test_set_nested_categories() { public function test_set_nested_categories() {
global $DB; global $DB;
// Setup fixture // Setup fixture.
$topcat = $this->getDataGenerator()->create_category(); $topcat = $this->getDataGenerator()->create_category();
$topcatdbrecord = $DB->get_record('course_categories', array('id' => $topcat->id)); $topcatdbrecord = $DB->get_record('course_categories', array('id' => $topcat->id));
$subcat = $this->getDataGenerator()->create_category(array('parent'=>$topcat->id)); $subcat = $this->getDataGenerator()->create_category(array('parent'=>$topcat->id));
$subcatdbrecord = $DB->get_record('course_categories', array('id' => $subcat->id)); $subcatdbrecord = $DB->get_record('course_categories', array('id' => $subcat->id));
// Exercise SUT // Exercise SUT.
$this->testpage->set_category_by_id($subcat->id); $this->testpage->set_category_by_id($subcat->id);
// Validate // Validated.
$categories = $this->testpage->categories; $categories = $this->testpage->categories;
$this->assertEquals(2, count($categories)); $this->assertCount(2, $categories);
$this->assertEquals($topcatdbrecord, array_pop($categories)); $this->assertEquals($topcatdbrecord, array_pop($categories));
$this->assertEquals($subcatdbrecord, array_pop($categories)); $this->assertEquals($subcatdbrecord, array_pop($categories));
} }
}
/**
* Test functions that rely on the context table.
*/
class moodle_page_cm_test extends advanced_testcase {
protected $testpage;
protected function setUp() {
parent::setUp();
$this->testpage = new moodle_page();
$this->resetAfterTest();
}
public function test_cm_null_initially() { public function test_cm_null_initially() {
// Validate // Validated.
$this->assertNull($this->testpage->cm); $this->assertNull($this->testpage->cm);
} }
public function test_set_cm() { public function test_set_cm() {
// Setup fixture // Setup fixture.
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
$forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id)); $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
$cm = get_coursemodule_from_id('forum', $forum->cmid); $cm = get_coursemodule_from_id('forum', $forum->cmid);
// Exercise SUT // Exercise SUT.
$this->testpage->set_cm($cm); $this->testpage->set_cm($cm);
// Validate // Validated.
$this->assertEquals($cm->id, $this->testpage->cm->id); $this->assertEquals($cm->id, $this->testpage->cm->id);
} }
public function test_cannot_set_activity_record_before_cm() { public function test_cannot_set_activity_record_before_cm() {
// Setup fixture // Setup fixture.
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
$forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id)); $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
$cm = get_coursemodule_from_id('forum', $forum->cmid); $cm = get_coursemodule_from_id('forum', $forum->cmid);
// Set expectation // Set expectation.
$this->setExpectedException('coding_exception'); $this->setExpectedException('coding_exception');
// Exercise SUT // Exercise SUT.
$this->testpage->set_activity_record($forum); $this->testpage->set_activity_record($forum);
} }
public function test_setting_cm_sets_context() { public function test_setting_cm_sets_context() {
// Setup fixture // Setup fixture.
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
$forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id)); $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
$cm = get_coursemodule_from_id('forum', $forum->cmid); $cm = get_coursemodule_from_id('forum', $forum->cmid);
// Exercise SUT // Exercise SUT.
$this->testpage->set_cm($cm); $this->testpage->set_cm($cm);
// Validate // Validated.
$this->assertSame(context_module::instance($cm->id), $this->testpage->context); $this->assertSame(context_module::instance($cm->id), $this->testpage->context);
} }
public function test_activity_record_loaded_if_not_set() { public function test_activity_record_loaded_if_not_set() {
// Setup fixture // Setup fixture.
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
$forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id)); $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
$cm = get_coursemodule_from_id('forum', $forum->cmid); $cm = get_coursemodule_from_id('forum', $forum->cmid);
// Exercise SUT // Exercise SUT.
$this->testpage->set_cm($cm); $this->testpage->set_cm($cm);
// Validate // Validated.
unset($forum->cmid); unset($forum->cmid);
$this->assertEquals($forum, $this->testpage->activityrecord); $this->assertEquals($forum, $this->testpage->activityrecord);
} }
public function test_set_activity_record() { public function test_set_activity_record() {
// Setup fixture // Setup fixture.
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
$forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id)); $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
$cm = get_coursemodule_from_id('forum', $forum->cmid); $cm = get_coursemodule_from_id('forum', $forum->cmid);
$this->testpage->set_cm($cm); $this->testpage->set_cm($cm);
// Exercise SUT // Exercise SUT.
$this->testpage->set_activity_record($forum); $this->testpage->set_activity_record($forum);
// Validate // Validated.
unset($forum->cmid); unset($forum->cmid);
$this->assertEquals($forum, $this->testpage->activityrecord); $this->assertEquals($forum, $this->testpage->activityrecord);
} }
public function test_cannot_set_inconsistent_activity_record_course() { public function test_cannot_set_inconsistent_activity_record_course() {
// Setup fixture // Setup fixture.
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
$forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id)); $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
$cm = get_coursemodule_from_id('forum', $forum->cmid); $cm = get_coursemodule_from_id('forum', $forum->cmid);
$this->testpage->set_cm($cm); $this->testpage->set_cm($cm);
// Set expectation // Set expectation.
$this->setExpectedException('coding_exception'); $this->setExpectedException('coding_exception');
// Exercise SUT // Exercise SUT.
$forum->course = 13; $forum->course = 13;
$this->testpage->set_activity_record($forum); $this->testpage->set_activity_record($forum);
} }
public function test_cannot_set_inconsistent_activity_record_instance() { public function test_cannot_set_inconsistent_activity_record_instance() {
// Setup fixture // Setup fixture.
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
$forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id)); $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
$cm = get_coursemodule_from_id('forum', $forum->cmid); $cm = get_coursemodule_from_id('forum', $forum->cmid);
$this->testpage->set_cm($cm); $this->testpage->set_cm($cm);
// Set expectation // Set expectation.
$this->setExpectedException('coding_exception'); $this->setExpectedException('coding_exception');
// Exercise SUT // Exercise SUT.
$forum->id = 13; $forum->id = 13;
$this->testpage->set_activity_record($forum); $this->testpage->set_activity_record($forum);
} }
public function test_setting_cm_sets_course() { public function test_setting_cm_sets_course() {
// Setup fixture // Setup fixture.
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
$forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id)); $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
$cm = get_coursemodule_from_id('forum', $forum->cmid); $cm = get_coursemodule_from_id('forum', $forum->cmid);
// Exercise SUT // Exercise SUT.
$this->testpage->set_cm($cm); $this->testpage->set_cm($cm);
// Validate // Validated.
$this->assertEquals($course->id, $this->testpage->course->id); $this->assertEquals($course->id, $this->testpage->course->id);
} }
public function test_set_cm_with_course_and_activity_no_db() { public function test_set_cm_with_course_and_activity_no_db() {
// Setup fixture // Setup fixture.
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
$forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id)); $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
$cm = get_coursemodule_from_id('forum', $forum->cmid); $cm = get_coursemodule_from_id('forum', $forum->cmid);
// This only works without db if we already have modinfo cache // This only works without db if we already have modinfo cache
// Exercise SUT // Exercise SUT.
$this->testpage->set_cm($cm, $course, $forum); $this->testpage->set_cm($cm, $course, $forum);
// Validate // Validated.
$this->assertEquals($cm->id, $this->testpage->cm->id); $this->assertEquals($cm->id, $this->testpage->cm->id);
$this->assertEquals($course->id, $this->testpage->course->id); $this->assertEquals($course->id, $this->testpage->course->id);
unset($forum->cmid); unset($forum->cmid);
@ -537,93 +477,113 @@ class moodle_page_cm_test extends advanced_testcase {
} }
public function test_cannot_set_cm_with_inconsistent_course() { public function test_cannot_set_cm_with_inconsistent_course() {
// Setup fixture // Setup fixture.
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
$forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id)); $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
$cm = get_coursemodule_from_id('forum', $forum->cmid); $cm = get_coursemodule_from_id('forum', $forum->cmid);
// Set expectation // Set expectation.
$this->setExpectedException('coding_exception'); $this->setExpectedException('coding_exception');
// Exercise SUT // Exercise SUT.
$cm->course = 13; $cm->course = 13;
$this->testpage->set_cm($cm, $course); $this->testpage->set_cm($cm, $course);
} }
public function test_get_activity_name() { public function test_get_activity_name() {
// Setup fixture // Setup fixture.
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
$forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id)); $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
$cm = get_coursemodule_from_id('forum', $forum->cmid); $cm = get_coursemodule_from_id('forum', $forum->cmid);
// Exercise SUT // Exercise SUT.
$this->testpage->set_cm($cm, $course, $forum); $this->testpage->set_cm($cm, $course, $forum);
// Validate // Validated.
$this->assertEquals('forum', $this->testpage->activityname); $this->assertSame('forum', $this->testpage->activityname);
}
}
/**
* Test functions that affect filter_active table with contextid = $syscontextid.
*/
class moodle_page_editing_test extends advanced_testcase {
protected $testpage;
protected $originaluserediting;
protected function setUp() {
parent::setUp();
$this->setAdminUser();
$this->testpage = new testable_moodle_page();
$this->testpage->set_context(context_system::instance());
$this->resetAfterTest();
} }
// We are relying on the fact that unit tests are alwyas run by admin, to
// ensure the user_allows_editing call returns true.
public function test_user_is_editing_on() { public function test_user_is_editing_on() {
// Setup fixture // We are relying on the fact that unit tests are always run by admin, to
// ensure the user_allows_editing call returns true.
// Setup fixture.
global $USER; global $USER;
$this->testpage->set_context(context_system::instance());
$this->setAdminUser();
$USER->editing = true; $USER->editing = true;
// Validate // Validated.
$this->assertTrue($this->testpage->user_is_editing()); $this->assertTrue($this->testpage->user_is_editing());
} }
// We are relying on the fact that unit tests are alwyas run by admin, to
// ensure the user_allows_editing call returns true.
public function test_user_is_editing_off() { public function test_user_is_editing_off() {
// Setup fixture // We are relying on the fact that unit tests are always run by admin, to
// ensure the user_allows_editing call returns true.
// Setup fixture.
global $USER; global $USER;
$this->testpage->set_context(context_system::instance());
$this->setAdminUser();
$USER->editing = false; $USER->editing = false;
// Validate // Validated.
$this->assertFalse($this->testpage->user_is_editing()); $this->assertFalse($this->testpage->user_is_editing());
} }
public function test_default_editing_capabilities() { public function test_default_editing_capabilities() {
// Validate $this->testpage->set_context(context_system::instance());
$this->setAdminUser();
// Validated.
$this->assertEquals(array('moodle/site:manageblocks'), $this->testpage->all_editing_caps()); $this->assertEquals(array('moodle/site:manageblocks'), $this->testpage->all_editing_caps());
} }
public function test_other_block_editing_cap() { public function test_other_block_editing_cap() {
// Exercise SUT $this->testpage->set_context(context_system::instance());
$this->setAdminUser();
// Exercise SUT.
$this->testpage->set_blocks_editing_capability('moodle/my:manageblocks'); $this->testpage->set_blocks_editing_capability('moodle/my:manageblocks');
// Validate // Validated.
$this->assertEquals(array('moodle/my:manageblocks'), $this->testpage->all_editing_caps()); $this->assertEquals(array('moodle/my:manageblocks'), $this->testpage->all_editing_caps());
} }
public function test_other_editing_cap() { public function test_other_editing_cap() {
// Exercise SUT $this->testpage->set_context(context_system::instance());
$this->setAdminUser();
// Exercise SUT.
$this->testpage->set_other_editing_capability('moodle/course:manageactivities'); $this->testpage->set_other_editing_capability('moodle/course:manageactivities');
// Validate // Validated.
$actualcaps = $this->testpage->all_editing_caps(); $actualcaps = $this->testpage->all_editing_caps();
$expectedcaps = array('moodle/course:manageactivities', 'moodle/site:manageblocks'); $expectedcaps = array('moodle/course:manageactivities', 'moodle/site:manageblocks');
$this->assertEquals(array_values($expectedcaps), array_values($actualcaps)); $this->assertEquals(array_values($expectedcaps), array_values($actualcaps));
} }
public function test_other_editing_caps() { public function test_other_editing_caps() {
// Exercise SUT $this->testpage->set_context(context_system::instance());
$this->setAdminUser();
// Exercise SUT.
$this->testpage->set_other_editing_capability(array('moodle/course:manageactivities', 'moodle/site:other')); $this->testpage->set_other_editing_capability(array('moodle/course:manageactivities', 'moodle/site:other'));
// Validate // Validated.
$actualcaps = $this->testpage->all_editing_caps(); $actualcaps = $this->testpage->all_editing_caps();
$expectedcaps = array('moodle/course:manageactivities', 'moodle/site:other', 'moodle/site:manageblocks'); $expectedcaps = array('moodle/course:manageactivities', 'moodle/site:other', 'moodle/site:manageblocks');
$this->assertEquals(array_values($expectedcaps), array_values($actualcaps)); $this->assertEquals(array_values($expectedcaps), array_values($actualcaps));
} }
} }
/**
* Test-specific subclass to make some protected things public.
*/
class testable_moodle_page extends moodle_page {
public function initialise_default_pagetype($script = null) {
parent::initialise_default_pagetype($script);
}
public function url_to_class_name($url) {
return parent::url_to_class_name($url);
}
public function all_editing_caps() {
return parent::all_editing_caps();
}
}

View file

@ -34,11 +34,11 @@ require_once($CFG->libdir.'/pluginlib.php');
/** /**
* Tests of the basic API of the plugin manager * Tests of the basic API of the plugin manager.
* *
* @group core_plugin * @group core_plugin
*/ */
class plugin_manager_test extends advanced_testcase { class core_plugin_manager_testcase extends advanced_testcase {
public function setUp() { public function setUp() {
$this->resetAfterTest(); $this->resetAfterTest();
@ -46,29 +46,29 @@ class plugin_manager_test extends advanced_testcase {
public function test_plugin_manager_instance() { public function test_plugin_manager_instance() {
$pluginman = testable_plugin_manager::instance(); $pluginman = testable_plugin_manager::instance();
$this->assertTrue($pluginman instanceof testable_plugin_manager); $this->assertInstanceOf('testable_plugin_manager', $pluginman);
} }
public function test_get_plugins_of_type() { public function test_get_plugins_of_type() {
$pluginman = testable_plugin_manager::instance(); $pluginman = testable_plugin_manager::instance();
$mods = $pluginman->get_plugins_of_type('mod'); $mods = $pluginman->get_plugins_of_type('mod');
$this->assertEquals('array', gettype($mods)); $this->assertSame('array', gettype($mods));
$this->assertEquals(5, count($mods)); $this->assertCount(5, $mods);
$this->assertTrue($mods['foo'] instanceof testable_plugininfo_mod); $this->assertInstanceOf('testable_plugininfo_mod', $mods['foo']);
$this->assertTrue($mods['bar'] instanceof testable_plugininfo_mod); $this->assertInstanceOf('testable_plugininfo_mod', $mods['bar']);
$this->assertTrue($mods['baz'] instanceof testable_plugininfo_mod); $this->assertInstanceOf('testable_plugininfo_mod', $mods['baz']);
$this->assertTrue($mods['qux'] instanceof testable_plugininfo_mod); $this->assertInstanceOf('testable_plugininfo_mod', $mods['qux']);
$this->assertTrue($mods['new'] instanceof testable_plugininfo_mod); $this->assertInstanceOf('testable_plugininfo_mod', $mods['new']);
$foolishes = $pluginman->get_plugins_of_type('foolish'); $foolishes = $pluginman->get_plugins_of_type('foolish');
$this->assertEquals(2, count($foolishes)); $this->assertCount(2, $foolishes);
$this->assertTrue($foolishes['frog'] instanceof testable_pluginfo_foolish); $this->assertInstanceOf('testable_pluginfo_foolish', $foolishes['frog']);
$this->assertTrue($foolishes['hippo'] instanceof testable_pluginfo_foolish); $this->assertInstanceOf('testable_pluginfo_foolish', $foolishes['hippo']);
$bazmegs = $pluginman->get_plugins_of_type('bazmeg'); $bazmegs = $pluginman->get_plugins_of_type('bazmeg');
$this->assertEquals(1, count($bazmegs)); $this->assertCount(1, $bazmegs);
$this->assertTrue($bazmegs['one'] instanceof testable_pluginfo_bazmeg); $this->assertInstanceOf('testable_pluginfo_bazmeg', $bazmegs['one']);
$quxcats = $pluginman->get_plugins_of_type('quxcat'); $quxcats = $pluginman->get_plugins_of_type('quxcat');
$this->assertEquals(1, count($quxcats)); $this->assertCount(1, $quxcats);
$this->assertTrue($quxcats['one'] instanceof testable_pluginfo_quxcat); $this->assertInstanceOf('testable_pluginfo_quxcat', $quxcats['one']);
$unknown = $pluginman->get_plugins_of_type('muhehe'); $unknown = $pluginman->get_plugins_of_type('muhehe');
$this->assertSame(array(), $unknown); $this->assertSame(array(), $unknown);
} }
@ -76,21 +76,21 @@ class plugin_manager_test extends advanced_testcase {
public function test_get_plugins() { public function test_get_plugins() {
$pluginman = testable_plugin_manager::instance(); $pluginman = testable_plugin_manager::instance();
$plugins = $pluginman->get_plugins(); $plugins = $pluginman->get_plugins();
$this->assertEquals('array', gettype($plugins)); $this->assertSame('array', gettype($plugins));
$this->assertTrue(isset($plugins['mod']['foo'])); $this->assertTrue(isset($plugins['mod']['foo']));
$this->assertTrue(isset($plugins['mod']['bar'])); $this->assertTrue(isset($plugins['mod']['bar']));
$this->assertTrue(isset($plugins['mod']['baz'])); $this->assertTrue(isset($plugins['mod']['baz']));
$this->assertTrue(isset($plugins['mod']['new'])); $this->assertTrue(isset($plugins['mod']['new']));
$this->assertTrue(isset($plugins['foolish']['frog'])); $this->assertTrue(isset($plugins['foolish']['frog']));
$this->assertTrue(isset($plugins['foolish']['hippo'])); $this->assertTrue(isset($plugins['foolish']['hippo']));
$this->assertTrue($plugins['mod']['foo'] instanceof testable_plugininfo_mod); $this->assertInstanceOf('testable_plugininfo_mod', $plugins['mod']['foo']);
$this->assertTrue($plugins['mod']['bar'] instanceof testable_plugininfo_mod); $this->assertInstanceOf('testable_plugininfo_mod', $plugins['mod']['bar']);
$this->assertTrue($plugins['mod']['baz'] instanceof testable_plugininfo_mod); $this->assertInstanceOf('testable_plugininfo_mod', $plugins['mod']['baz']);
$this->assertTrue($plugins['mod']['new'] instanceof testable_plugininfo_mod); $this->assertInstanceOf('testable_plugininfo_mod', $plugins['mod']['new']);
$this->assertTrue($plugins['foolish']['frog'] instanceof testable_pluginfo_foolish); $this->assertInstanceOf('testable_pluginfo_foolish', $plugins['foolish']['frog']);
$this->assertTrue($plugins['foolish']['hippo'] instanceof testable_pluginfo_foolish); $this->assertInstanceOf('testable_pluginfo_foolish', $plugins['foolish']['hippo']);
$this->assertTrue($plugins['bazmeg']['one'] instanceof testable_pluginfo_bazmeg); $this->assertInstanceOf('testable_pluginfo_bazmeg', $plugins['bazmeg']['one']);
$this->assertTrue($plugins['quxcat']['one'] instanceof testable_pluginfo_quxcat); $this->assertInstanceOf('testable_pluginfo_quxcat', $plugins['quxcat']['one']);
} }
public function test_get_subplugins_of_plugin() { public function test_get_subplugins_of_plugin() {
@ -98,18 +98,18 @@ class plugin_manager_test extends advanced_testcase {
$this->assertSame(array(), $pluginman->get_subplugins_of_plugin('mod_missing')); $this->assertSame(array(), $pluginman->get_subplugins_of_plugin('mod_missing'));
$this->assertSame(array(), $pluginman->get_subplugins_of_plugin('mod_bar')); $this->assertSame(array(), $pluginman->get_subplugins_of_plugin('mod_bar'));
$foosubs = $pluginman->get_subplugins_of_plugin('mod_foo'); $foosubs = $pluginman->get_subplugins_of_plugin('mod_foo');
$this->assertEquals('array', gettype($foosubs)); $this->assertSame('array', gettype($foosubs));
$this->assertEquals(2, count($foosubs)); $this->assertCount(2, $foosubs);
$this->assertTrue($foosubs['foolish_frog'] instanceof testable_pluginfo_foolish); $this->assertInstanceOf('testable_pluginfo_foolish', $foosubs['foolish_frog']);
$this->assertTrue($foosubs['foolish_hippo'] instanceof testable_pluginfo_foolish); $this->assertInstanceOf('testable_pluginfo_foolish', $foosubs['foolish_hippo']);
$bazsubs = $pluginman->get_subplugins_of_plugin('mod_baz'); $bazsubs = $pluginman->get_subplugins_of_plugin('mod_baz');
$this->assertEquals('array', gettype($bazsubs)); $this->assertSame('array', gettype($bazsubs));
$this->assertEquals(1, count($bazsubs)); $this->assertCount(1, $bazsubs);
$this->assertTrue($bazsubs['bazmeg_one'] instanceof testable_pluginfo_bazmeg); $this->assertInstanceOf('testable_pluginfo_bazmeg', $bazsubs['bazmeg_one']);
$quxsubs = $pluginman->get_subplugins_of_plugin('mod_qux'); $quxsubs = $pluginman->get_subplugins_of_plugin('mod_qux');
$this->assertEquals('array', gettype($quxsubs)); $this->assertSame('array', gettype($quxsubs));
$this->assertEquals(1, count($quxsubs)); $this->assertCount(1, $quxsubs);
$this->assertTrue($quxsubs['quxcat_one'] instanceof testable_pluginfo_quxcat); $this->assertInstanceOf('testable_pluginfo_quxcat', $quxsubs['quxcat_one']);
} }
public function test_get_subplugins() { public function test_get_subplugins() {
@ -122,40 +122,40 @@ class plugin_manager_test extends advanced_testcase {
public function test_get_parent_of_subplugin() { public function test_get_parent_of_subplugin() {
$pluginman = testable_plugin_manager::instance(); $pluginman = testable_plugin_manager::instance();
$this->assertEquals('mod_foo', $pluginman->get_parent_of_subplugin('foolish')); $this->assertSame('mod_foo', $pluginman->get_parent_of_subplugin('foolish'));
$this->assertEquals('mod_baz', $pluginman->get_parent_of_subplugin('bazmeg')); $this->assertSame('mod_baz', $pluginman->get_parent_of_subplugin('bazmeg'));
$this->assertEquals('mod_qux', $pluginman->get_parent_of_subplugin('quxcat')); $this->assertSame('mod_qux', $pluginman->get_parent_of_subplugin('quxcat'));
$this->assertSame(false, $pluginman->get_parent_of_subplugin('mod')); $this->assertFalse($pluginman->get_parent_of_subplugin('mod'));
$this->assertSame(false, $pluginman->get_parent_of_subplugin('unknown')); $this->assertFalse($pluginman->get_parent_of_subplugin('unknown'));
$plugins = $pluginman->get_plugins(); $plugins = $pluginman->get_plugins();
$this->assertFalse($plugins['mod']['foo']->is_subplugin()); $this->assertFalse($plugins['mod']['foo']->is_subplugin());
$this->assertSame(false, $plugins['mod']['foo']->get_parent_plugin()); $this->assertFalse($plugins['mod']['foo']->get_parent_plugin());
$this->assertTrue($plugins['foolish']['frog']->is_subplugin()); $this->assertTrue($plugins['foolish']['frog']->is_subplugin());
$this->assertEquals('mod_foo', $plugins['foolish']['frog']->get_parent_plugin()); $this->assertSame('mod_foo', $plugins['foolish']['frog']->get_parent_plugin());
} }
public function test_plugin_name() { public function test_plugin_name() {
$pluginman = testable_plugin_manager::instance(); $pluginman = testable_plugin_manager::instance();
$this->assertEquals('Foo', $pluginman->plugin_name('mod_foo')); $this->assertSame('Foo', $pluginman->plugin_name('mod_foo'));
$this->assertEquals('Bar', $pluginman->plugin_name('mod_bar')); $this->assertSame('Bar', $pluginman->plugin_name('mod_bar'));
$this->assertEquals('Frog', $pluginman->plugin_name('foolish_frog')); $this->assertSame('Frog', $pluginman->plugin_name('foolish_frog'));
$this->assertEquals('Hippo', $pluginman->plugin_name('foolish_hippo')); $this->assertSame('Hippo', $pluginman->plugin_name('foolish_hippo'));
$this->assertEquals('One', $pluginman->plugin_name('bazmeg_one')); $this->assertSame('One', $pluginman->plugin_name('bazmeg_one'));
$this->assertEquals('One', $pluginman->plugin_name('quxcat_one')); $this->assertSame('One', $pluginman->plugin_name('quxcat_one'));
} }
public function test_get_plugin_info() { public function test_get_plugin_info() {
$pluginman = testable_plugin_manager::instance(); $pluginman = testable_plugin_manager::instance();
$this->assertTrue($pluginman->get_plugin_info('mod_foo') instanceof testable_plugininfo_mod); $this->assertInstanceOf('testable_plugininfo_mod', $pluginman->get_plugin_info('mod_foo'));
$this->assertTrue($pluginman->get_plugin_info('foolish_frog') instanceof testable_pluginfo_foolish); $this->assertInstanceOf('testable_pluginfo_foolish', $pluginman->get_plugin_info('foolish_frog'));
} }
public function test_other_plugins_that_require() { public function test_other_plugins_that_require() {
$pluginman = testable_plugin_manager::instance(); $pluginman = testable_plugin_manager::instance();
$this->assertEquals(array('foolish_frog'), $pluginman->other_plugins_that_require('mod_foo')); $this->assertEquals(array('foolish_frog'), $pluginman->other_plugins_that_require('mod_foo'));
$this->assertEquals(2, count($pluginman->other_plugins_that_require('foolish_frog'))); $this->assertCount(2, $pluginman->other_plugins_that_require('foolish_frog'));
$this->assertTrue(in_array('foolish_hippo', $pluginman->other_plugins_that_require('foolish_frog'))); $this->assertContains('foolish_hippo', $pluginman->other_plugins_that_require('foolish_frog'));
$this->assertTrue(in_array('mod_foo', $pluginman->other_plugins_that_require('foolish_frog'))); $this->assertContains('mod_foo', $pluginman->other_plugins_that_require('foolish_frog'));
$this->assertEquals(array(), $pluginman->other_plugins_that_require('foolish_hippo')); $this->assertEquals(array(), $pluginman->other_plugins_that_require('foolish_hippo'));
$this->assertEquals(array('mod_foo'), $pluginman->other_plugins_that_require('mod_bar')); $this->assertEquals(array('mod_foo'), $pluginman->other_plugins_that_require('mod_bar'));
$this->assertEquals(array('mod_foo'), $pluginman->other_plugins_that_require('mod_missing')); $this->assertEquals(array('mod_foo'), $pluginman->other_plugins_that_require('mod_missing'));
@ -184,24 +184,24 @@ class plugin_manager_test extends advanced_testcase {
$pluginman = testable_plugin_manager::instance(); $pluginman = testable_plugin_manager::instance();
$failedplugins = array(); $failedplugins = array();
$this->assertFalse($pluginman->all_plugins_ok(2013010100, $failedplugins)); $this->assertFalse($pluginman->all_plugins_ok(2013010100, $failedplugins));
$this->assertTrue(in_array('mod_foo', $failedplugins)); // Requires mod_missing $this->assertContains('mod_foo', $failedplugins); // Requires mod_missing.
$this->assertFalse(in_array('mod_bar', $failedplugins)); $this->assertNotContains('mod_bar', $failedplugins);
$this->assertFalse(in_array('foolish_frog', $failedplugins)); $this->assertNotContains('foolish_frog', $failedplugins);
$this->assertFalse(in_array('foolish_hippo', $failedplugins)); $this->assertNotContains('foolish_hippo', $failedplugins);
$failedplugins = array(); $failedplugins = array();
$this->assertFalse($pluginman->all_plugins_ok(2012010100, $failedplugins)); $this->assertFalse($pluginman->all_plugins_ok(2012010100, $failedplugins));
$this->assertTrue(in_array('mod_foo', $failedplugins)); // Requires mod_missing $this->assertContains('mod_foo', $failedplugins); // Requires mod_missing.
$this->assertFalse(in_array('mod_bar', $failedplugins)); $this->assertNotContains('mod_bar', $failedplugins);
$this->assertTrue(in_array('foolish_frog', $failedplugins)); // Requires Moodle 2013010100 $this->assertContains('foolish_frog', $failedplugins); // Requires Moodle 2013010100.
$this->assertFalse(in_array('foolish_hippo', $failedplugins)); $this->assertNotContains('foolish_hippo', $failedplugins);
$failedplugins = array(); $failedplugins = array();
$this->assertFalse($pluginman->all_plugins_ok(2011010100, $failedplugins)); $this->assertFalse($pluginman->all_plugins_ok(2011010100, $failedplugins));
$this->assertTrue(in_array('mod_foo', $failedplugins)); // Requires mod_missing and Moodle 2012010100 $this->assertContains('mod_foo', $failedplugins); // Requires mod_missing and Moodle 2012010100.
$this->assertTrue(in_array('mod_bar', $failedplugins)); // Requires Moodle 2012010100 $this->assertContains('mod_bar', $failedplugins); // Requires Moodle 2012010100.
$this->assertTrue(in_array('foolish_frog', $failedplugins)); // Requires Moodle 2013010100 $this->assertContains('foolish_frog', $failedplugins); // Requires Moodle 2013010100.
$this->assertTrue(in_array('foolish_hippo', $failedplugins)); // Requires Moodle 2012010100 $this->assertContains('foolish_hippo', $failedplugins); // Requires Moodle 2012010100.
} }
public function test_some_plugins_updatable() { public function test_some_plugins_updatable() {
@ -229,7 +229,7 @@ class plugin_manager_test extends advanced_testcase {
$pluginman = testable_plugin_manager::instance(); $pluginman = testable_plugin_manager::instance();
$plugins = $pluginman->get_plugins(); $plugins = $pluginman->get_plugins();
$this->assertNull($plugins['mod']['bar']->available_updates()); $this->assertNull($plugins['mod']['bar']->available_updates());
$this->assertEquals('array', gettype($plugins['mod']['foo']->available_updates())); $this->assertSame('array', gettype($plugins['mod']['foo']->available_updates()));
foreach ($plugins['mod']['foo']->available_updates() as $availableupdate) { foreach ($plugins['mod']['foo']->available_updates() as $availableupdate) {
$this->assertInstanceOf('available_update_info', $availableupdate); $this->assertInstanceOf('available_update_info', $availableupdate);
} }
@ -253,7 +253,7 @@ class plugin_manager_test extends advanced_testcase {
$pluginman = testable_plugin_manager::instance(); $pluginman = testable_plugin_manager::instance();
foreach ($pluginman->get_plugins() as $plugintype => $plugininfos) { foreach ($pluginman->get_plugins() as $plugintype => $plugininfos) {
foreach ($plugininfos as $plugininfo) { foreach ($plugininfos as $plugininfo) {
$this->assertTrue($plugininfo->get_uninstall_url() instanceof moodle_url); $this->assertInstanceOf('moodle_url', $plugininfo->get_uninstall_url());
} }
} }
} }
@ -261,23 +261,23 @@ class plugin_manager_test extends advanced_testcase {
/** /**
* Tests of the basic API of the available update checker * Tests of the basic API of the available update checker.
* *
* @group core_plugin * @group core_plugin
*/ */
class available_update_checker_test extends advanced_testcase { class core_available_update_checker_testcase extends advanced_testcase {
public function test_core_available_update() { public function test_core_available_update() {
$provider = testable_available_update_checker::instance(); $provider = testable_available_update_checker::instance();
$this->assertTrue($provider instanceof available_update_checker); $this->assertInstanceOf('available_update_checker', $provider);
$provider->fake_current_environment(2012060102.00, '2.3.2 (Build: 20121012)', '2.3', array()); $provider->fake_current_environment(2012060102.00, '2.3.2 (Build: 20121012)', '2.3', array());
$updates = $provider->get_update_info('core'); $updates = $provider->get_update_info('core');
$this->assertEquals(count($updates), 2); $this->assertCount(2, $updates);
$provider->fake_current_environment(2012060103.00, '2.3.3 (Build: 20121212)', '2.3', array()); $provider->fake_current_environment(2012060103.00, '2.3.3 (Build: 20121212)', '2.3', array());
$updates = $provider->get_update_info('core'); $updates = $provider->get_update_info('core');
$this->assertEquals(count($updates), 1); $this->assertCount(1, $updates);
$provider->fake_current_environment(2012060103.00, '2.3.3 (Build: 20121212)', '2.3', array()); $provider->fake_current_environment(2012060103.00, '2.3.3 (Build: 20121212)', '2.3', array());
$updates = $provider->get_update_info('core', array('minmaturity' => MATURITY_STABLE)); $updates = $provider->get_update_info('core', array('minmaturity' => MATURITY_STABLE));
@ -285,7 +285,7 @@ class available_update_checker_test extends advanced_testcase {
} }
/** /**
* If there are no fetched data yet, the first cron should fetch them * If there are no fetched data yet, the first cron should fetch them.
*/ */
public function test_cron_initial_fetch() { public function test_cron_initial_fetch() {
$provider = testable_available_update_checker::instance(); $provider = testable_available_update_checker::instance();
@ -296,29 +296,29 @@ class available_update_checker_test extends advanced_testcase {
} }
/** /**
* If there is a fresh fetch available, no cron execution is expected * If there is a fresh fetch available, no cron execution is expected.
*/ */
public function test_cron_has_fresh_fetch() { public function test_cron_has_fresh_fetch() {
$provider = testable_available_update_checker::instance(); $provider = testable_available_update_checker::instance();
$provider->fakerecentfetch = time() - 23 * HOURSECS; // fetched 23 hours ago $provider->fakerecentfetch = time() - 23 * HOURSECS; // Fetched 23 hours ago.
$provider->fakecurrenttimestamp = -1; $provider->fakecurrenttimestamp = -1;
$provider->cron(); $provider->cron();
$this->assertTrue(true); // we should get here with no exception thrown $this->assertTrue(true); // We should get here with no exception thrown.
} }
/** /**
* If there is an outdated fetch, the cron execution is expected * If there is an outdated fetch, the cron execution is expected.
*/ */
public function test_cron_has_outdated_fetch() { public function test_cron_has_outdated_fetch() {
$provider = testable_available_update_checker::instance(); $provider = testable_available_update_checker::instance();
$provider->fakerecentfetch = time() - 49 * HOURSECS; // fetched 49 hours ago $provider->fakerecentfetch = time() - 49 * HOURSECS; // Fetched 49 hours ago.
$provider->fakecurrenttimestamp = -1; $provider->fakecurrenttimestamp = -1;
$this->setExpectedException('testable_available_update_checker_cron_executed'); $this->setExpectedException('testable_available_update_checker_cron_executed');
$provider->cron(); $provider->cron();
} }
/** /**
* The first cron after 01:42 AM today should fetch the data * The first cron after 01:42 AM today should fetch the data.
* *
* @see testable_available_update_checker::cron_execution_offset() * @see testable_available_update_checker::cron_execution_offset()
*/ */
@ -327,7 +327,7 @@ class available_update_checker_test extends advanced_testcase {
$provider->fakecurrenttimestamp = mktime(1, 40, 02); // 01:40:02 AM today $provider->fakecurrenttimestamp = mktime(1, 40, 02); // 01:40:02 AM today
$provider->fakerecentfetch = $provider->fakecurrenttimestamp - 24 * HOURSECS; $provider->fakerecentfetch = $provider->fakecurrenttimestamp - 24 * HOURSECS;
$provider->cron(); $provider->cron();
$this->assertTrue(true); // we should get here with no exception thrown $this->assertTrue(true); // We should get here with no exception thrown.
} }
/** /**
@ -339,7 +339,7 @@ class available_update_checker_test extends advanced_testcase {
public function test_cron_offset_execution() { public function test_cron_offset_execution() {
$provider = testable_available_update_checker::instance(); $provider = testable_available_update_checker::instance();
// the cron at 01:45 should fetch the data // The cron at 01:45 should fetch the data.
$provider->fakecurrenttimestamp = mktime(1, 45, 02); // 01:45:02 AM today $provider->fakecurrenttimestamp = mktime(1, 45, 02); // 01:45:02 AM today
$provider->fakerecentfetch = $provider->fakecurrenttimestamp - 24 * HOURSECS - 1; $provider->fakerecentfetch = $provider->fakecurrenttimestamp - 24 * HOURSECS - 1;
$executed = false; $executed = false;
@ -350,7 +350,7 @@ class available_update_checker_test extends advanced_testcase {
} }
$this->assertTrue($executed, 'Cron should be executed at 01:45:02 but it was not.'); $this->assertTrue($executed, 'Cron should be executed at 01:45:02 but it was not.');
// another cron at 06:45 should still consider data as fresh enough // Another cron at 06:45 should still consider data as fresh enough.
$provider->fakerecentfetch = $provider->fakecurrenttimestamp; $provider->fakerecentfetch = $provider->fakecurrenttimestamp;
$provider->fakecurrenttimestamp = mktime(6, 45, 03); // 06:45:03 AM $provider->fakecurrenttimestamp = mktime(6, 45, 03); // 06:45:03 AM
$executed = false; $executed = false;
@ -361,7 +361,7 @@ class available_update_checker_test extends advanced_testcase {
} }
$this->assertFalse($executed, 'Cron should not be executed at 06:45:03 but it was.'); $this->assertFalse($executed, 'Cron should not be executed at 06:45:03 but it was.');
// the next scheduled execution should happen the next day // The next scheduled execution should happen the next day.
$provider->fakecurrenttimestamp = $provider->fakerecentfetch + 24 * HOURSECS + 1; $provider->fakecurrenttimestamp = $provider->fakerecentfetch + 24 * HOURSECS + 1;
$executed = false; $executed = false;
try { try {
@ -377,8 +377,8 @@ class available_update_checker_test extends advanced_testcase {
$old = array(); $old = array();
$new = array(); $new = array();
$cmp = $provider->compare_responses($old, $new); $cmp = $provider->compare_responses($old, $new);
$this->assertEquals('array', gettype($cmp)); $this->assertSame('array', gettype($cmp));
$this->assertTrue(empty($cmp)); $this->assertEmpty($cmp);
} }
public function test_compare_responses_old_empty() { public function test_compare_responses_old_empty() {
@ -394,10 +394,10 @@ class available_update_checker_test extends advanced_testcase {
) )
); );
$cmp = $provider->compare_responses($old, $new); $cmp = $provider->compare_responses($old, $new);
$this->assertEquals('array', gettype($cmp)); $this->assertSame('array', gettype($cmp));
$this->assertFalse(empty($cmp)); $this->assertNotEmpty($cmp);
$this->assertTrue(isset($cmp['core'][0]['version'])); $this->assertTrue(isset($cmp['core'][0]['version']));
$this->assertEquals($cmp['core'][0]['version'], 2012060103); $this->assertEquals(2012060103, $cmp['core'][0]['version']);
} }
public function test_compare_responses_no_change() { public function test_compare_responses_no_change() {
@ -420,8 +420,8 @@ class available_update_checker_test extends advanced_testcase {
) )
); );
$cmp = $provider->compare_responses($old, $new); $cmp = $provider->compare_responses($old, $new);
$this->assertEquals('array', gettype($cmp)); $this->assertSame('array', gettype($cmp));
$this->assertTrue(empty($cmp)); $this->assertEmpty($cmp);
} }
public function test_compare_responses_new_and_missing_update() { public function test_compare_responses_new_and_missing_update() {
@ -453,11 +453,11 @@ class available_update_checker_test extends advanced_testcase {
) )
); );
$cmp = $provider->compare_responses($old, $new); $cmp = $provider->compare_responses($old, $new);
$this->assertEquals('array', gettype($cmp)); $this->assertSame('array', gettype($cmp));
$this->assertFalse(empty($cmp)); $this->assertNotEmpty($cmp);
$this->assertEquals(count($cmp), 1); $this->assertCount(1, $cmp);
$this->assertEquals(count($cmp['core']), 1); $this->assertCount(1, $cmp['core']);
$this->assertEquals($cmp['core'][0]['version'], 2012120100); $this->assertEquals(2012120100, $cmp['core'][0]['version']);
} }
public function test_compare_responses_modified_update() { public function test_compare_responses_modified_update() {
@ -481,17 +481,17 @@ class available_update_checker_test extends advanced_testcase {
) )
); );
$cmp = $provider->compare_responses($old, $new); $cmp = $provider->compare_responses($old, $new);
$this->assertEquals('array', gettype($cmp)); $this->assertSame('array', gettype($cmp));
$this->assertFalse(empty($cmp)); $this->assertNotEmpty($cmp);
$this->assertEquals(count($cmp), 1); $this->assertCount(1, $cmp);
$this->assertEquals(count($cmp['mod_foo']), 1); $this->assertCount(1, $cmp['mod_foo']);
$this->assertEquals($cmp['mod_foo'][0]['version'], 2011010102); $this->assertEquals(2011010102, $cmp['mod_foo'][0]['version']);
} }
public function test_compare_responses_invalid_format() { public function test_compare_responses_invalid_format() {
$provider = testable_available_update_checker::instance(); $provider = testable_available_update_checker::instance();
$broken = array( $broken = array(
'status' => 'ERROR' // no 'updates' key here 'status' => 'ERROR' // No 'updates' key here.
); );
$this->setExpectedException('available_update_checker_exception'); $this->setExpectedException('available_update_checker_exception');
$cmp = $provider->compare_responses($broken, $broken); $cmp = $provider->compare_responses($broken, $broken);
@ -505,7 +505,7 @@ class available_update_checker_test extends advanced_testcase {
$this->assertFalse($provider->is_same_release('2.3dev', '2.3 dev')); $this->assertFalse($provider->is_same_release('2.3dev', '2.3 dev'));
$this->assertFalse($provider->is_same_release('2.3.1', '2.3')); $this->assertFalse($provider->is_same_release('2.3.1', '2.3'));
$this->assertFalse($provider->is_same_release('2.3.1', '2.3.2')); $this->assertFalse($provider->is_same_release('2.3.1', '2.3.2'));
$this->assertTrue($provider->is_same_release('2.3.2+', '2.3.2')); // yes, really $this->assertTrue($provider->is_same_release('2.3.2+', '2.3.2')); // Yes, really!
$this->assertTrue($provider->is_same_release('2.3.2 (Build: 123456)', '2.3.2+ (Build: 123457)')); $this->assertTrue($provider->is_same_release('2.3.2 (Build: 123456)', '2.3.2+ (Build: 123457)'));
$this->assertFalse($provider->is_same_release('3.0 Community Edition', '3.0 Enterprise Edition')); $this->assertFalse($provider->is_same_release('3.0 Community Edition', '3.0 Enterprise Edition'));
$this->assertTrue($provider->is_same_release('3.0 Community Edition', '3.0 Community Edition (Build: 20290101)')); $this->assertTrue($provider->is_same_release('3.0 Community Edition', '3.0 Community Edition (Build: 20290101)'));
@ -534,7 +534,7 @@ class testable_plugininfo_base extends plugininfo_base {
/** /**
* Modified {@link plugininfo_mod} suitable for testing purposes * Modified {@link plugininfo_mod} suitable for testing purposes.
*/ */
class testable_plugininfo_mod extends plugininfo_mod { class testable_plugininfo_mod extends plugininfo_mod {
@ -567,7 +567,7 @@ class testable_plugininfo_mod extends plugininfo_mod {
/** /**
* Testable class representing subplugins of testable mod_foo * Testable class representing subplugins of testable mod_foo.
*/ */
class testable_pluginfo_foolish extends testable_plugininfo_base { class testable_pluginfo_foolish extends testable_plugininfo_base {
@ -586,7 +586,7 @@ class testable_pluginfo_foolish extends testable_plugininfo_base {
/** /**
* Testable class representing subplugins of testable mod_baz * Testable class representing subplugins of testable mod_baz.
*/ */
class testable_pluginfo_bazmeg extends testable_plugininfo_base { class testable_pluginfo_bazmeg extends testable_plugininfo_base {
@ -605,7 +605,7 @@ class testable_pluginfo_bazmeg extends testable_plugininfo_base {
/** /**
* Testable class representing subplugins of testable mod_qux * Testable class representing subplugins of testable mod_qux.
*/ */
class testable_pluginfo_quxcat extends testable_plugininfo_base { class testable_pluginfo_quxcat extends testable_plugininfo_base {
@ -653,7 +653,7 @@ class testable_plugin_manager extends plugin_manager {
* @param bool $disablecache ignored in this class * @param bool $disablecache ignored in this class
* @return array * @return array
*/ */
public function get_plugins($disablecache=false) { public function get_plugins($disablecache = false) {
$dirroot = dirname(__FILE__).'/fixtures/mockplugins'; $dirroot = dirname(__FILE__).'/fixtures/mockplugins';
@ -707,7 +707,7 @@ class testable_plugin_manager extends plugin_manager {
* @param bool $disablecache ignored in this class * @param bool $disablecache ignored in this class
* @return array * @return array
*/ */
public function get_subplugins($disablecache=false) { public function get_subplugins($disablecache = false) {
$this->subpluginsinfo = array( $this->subpluginsinfo = array(
'mod_foo' => array( 'mod_foo' => array(
@ -755,7 +755,7 @@ class testable_plugin_manager extends plugin_manager {
} }
public function plugintype_name_plural($type) { public function plugintype_name_plural($type) {
return ucfirst($type).'s'; // Simple, isn't it? ;-) return ucfirst($type).'s'; // Simple, isn't it? ;-).
} }
public function plugin_external_source($component) { public function plugin_external_source($component) {
@ -768,7 +768,7 @@ class testable_plugin_manager extends plugin_manager {
/** /**
* Modified version of {@link available_update_checker} suitable for testing * Modified version of {@link available_update_checker} suitable for testing.
*/ */
class testable_available_update_checker extends available_update_checker { class testable_available_update_checker extends available_update_checker {
@ -780,7 +780,7 @@ class testable_available_update_checker extends available_update_checker {
public $fakecurrenttimestamp = -1; public $fakecurrenttimestamp = -1;
/** /**
* Factory method for this class * Factory method for this class.
* *
* @return testable_available_update_checker the singleton instance * @return testable_available_update_checker the singleton instance
*/ */
@ -896,7 +896,7 @@ class testable_available_update_checker extends available_update_checker {
} }
protected function cron_execution_offset() { protected function cron_execution_offset() {
// autofetch should run by the first cron after 01:42 AM // Autofetch should run by the first cron after 01:42 AM.
return 42 * MINSECS; return 42 * MINSECS;
} }
@ -907,27 +907,25 @@ class testable_available_update_checker extends available_update_checker {
/** /**
* Exception used to detect {@link available_update_checker::cron_execute()} calls * Exception used to detect {@link available_update_checker::cron_execute()} calls.
*/ */
class testable_available_update_checker_cron_executed extends Exception { class testable_available_update_checker_cron_executed extends Exception {
} }
/** /**
* Modified {@link available_update_deployer} suitable for testing purposes * Modified {@link available_update_deployer} suitable for testing purposes.
*/ */
class testable_available_update_deployer extends available_update_deployer { class testable_available_update_deployer extends available_update_deployer {
} }
/** /**
* Test cases for {@link available_update_deployer} class * Test cases for {@link available_update_deployer} class.
* *
* @group core_plugin * @group core_plugin
*/ */
class available_update_deployer_test extends advanced_testcase { class core_available_update_deployer_testcase extends advanced_testcase {
public function test_magic_setters() { public function test_magic_setters() {
$deployer = testable_available_update_deployer::instance(); $deployer = testable_available_update_deployer::instance();
@ -944,9 +942,9 @@ class available_update_deployer_test extends advanced_testcase {
$filename = $CFG->phpunit_dataroot.'/mdeploy/auth/'.$passfile; $filename = $CFG->phpunit_dataroot.'/mdeploy/auth/'.$passfile;
$this->assertFileExists($filename); $this->assertFileExists($filename);
$stored = file($filename, FILE_IGNORE_NEW_LINES); $stored = file($filename, FILE_IGNORE_NEW_LINES);
$this->assertEquals(count($stored), 2); $this->assertCount(2, $stored);
$this->assertGreaterThan(23, strlen($stored[0])); $this->assertGreaterThan(23, strlen($stored[0]));
$this->assertSame($stored[0], $password); $this->assertSame($stored[0], $password);
$this->assertTrue(time() - (int)$stored[1] < 60); $this->assertLessThan(60, time() - (int)$stored[1]);
} }
} }