2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * PHPUnit integration unit tests
22 * @copyright 2012 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
30 * Test basic_testcase extra features and PHPUnit Moodle integration.
34 * @copyright 2012 Petr Skoda {@link http://skodak.org}
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class core_phpunit_basic_testcase extends basic_testcase {
40 * Tests that bootstrapping has occurred correctly
43 public function test_bootstrap() {
45 $this->assertTrue(isset($CFG->httpswwwroot));
46 $this->assertEquals($CFG->httpswwwroot, $CFG->wwwroot);
47 $this->assertEquals($CFG->prefix, $CFG->phpunit_prefix);
51 * This is just a verification if I understand the PHPUnit assert docs right --skodak
54 public function test_assert_behaviour() {
56 $a = array('a', 'b', 'c');
57 $b = array('a', 'c', 'b');
58 $c = array('a', 'b', 'c');
59 $d = array('a', 'b', 'C');
60 $this->assertNotEquals($a, $b);
61 $this->assertNotEquals($a, $d);
62 $this->assertEquals($a, $c);
63 $this->assertEquals($a, $b, '', 0, 10, true);
69 $b = new stdClass(); // switched order
77 $this->assertEquals($a, $b);
78 $this->assertNotSame($a, $b);
79 $this->assertEquals($a, $c);
80 $this->assertSame($a, $c);
81 $this->assertNotEquals($a, $d);
84 $this->assertEquals(1, '1');
85 $this->assertEquals(null, '');
87 $this->assertNotEquals(1, '1 ');
88 $this->assertNotEquals(0, '');
89 $this->assertNotEquals(null, '0');
90 $this->assertNotEquals(array(), '');
93 $this->assertEquals(null, null);
94 $this->assertEquals(false, null);
95 $this->assertEquals(0, null);
98 $this->assertEmpty(0);
99 $this->assertEmpty(0.0);
100 $this->assertEmpty('');
101 $this->assertEmpty('0');
102 $this->assertEmpty(false);
103 $this->assertEmpty(null);
104 $this->assertEmpty(array());
106 $this->assertNotEmpty(1);
107 $this->assertNotEmpty(0.1);
108 $this->assertNotEmpty(-1);
109 $this->assertNotEmpty(' ');
110 $this->assertNotEmpty('0 ');
111 $this->assertNotEmpty(true);
112 $this->assertNotEmpty(array(null));
113 $this->assertNotEmpty(new stdClass());
116 // Uncomment following tests to see logging of unexpected changes in global state and database
118 public function test_db_modification() {
120 $DB->set_field('user', 'confirmed', 1, array('id'=>-1));
123 public function test_cfg_modification() {
127 $CFG->rolesactive = 0;
130 public function test_user_modification() {
135 public function test_course_modification() {
140 public function test_all_modifications() {
141 global $DB, $CFG, $USER, $COURSE;
142 $DB->set_field('user', 'confirmed', 1, array('id'=>-1));
145 $CFG->rolesactive = 0;
150 public function test_transaction_problem() {
152 $DB->start_delegated_transaction();
159 * Test advanced_testcase extra features.
163 * @copyright 2012 Petr Skoda {@link http://skodak.org}
164 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
166 class core_phpunit_advanced_testcase extends advanced_testcase {
168 public function test_set_user() {
171 $this->assertEquals(0, $USER->id);
172 $this->assertSame($_SESSION['USER'], $USER);
174 $user = $DB->get_record('user', array('id'=>2));
175 $this->setUser($user);
176 $this->assertEquals(2, $USER->id);
177 $this->assertEquals(2, $_SESSION['USER']->id);
178 $this->assertSame($_SESSION['USER'], $USER);
181 $this->assertEquals(3, $USER->id);
182 $this->assertEquals(3, $_SESSION['USER']->id);
183 $this->assertSame($_SESSION['USER'], $USER);
185 session_set_user($user);
186 $this->assertEquals(2, $USER->id);
187 $this->assertEquals(2, $_SESSION['USER']->id);
188 $this->assertSame($_SESSION['USER'], $USER);
190 $USER = $DB->get_record('user', array('id'=>1));
191 $this->assertEquals(1, $USER->id);
192 $this->assertEquals(1, $_SESSION['USER']->id);
193 $this->assertSame($_SESSION['USER'], $USER);
195 $this->setUser(null);
196 $this->assertEquals(0, $USER->id);
197 $this->assertSame($_SESSION['USER'], $USER);
200 public function test_database_reset() {
203 $this->resetAfterTest(true);
205 $this->preventResetByRollback();
207 $this->assertEquals(1, $DB->count_records('course')); // only frontpage in new site
209 // this is weird table - id is NOT a sequence here
210 $this->assertEquals(0, $DB->count_records('context_temp'));
211 $DB->import_record('context_temp', array('id'=>5, 'path'=>'/1/2', 'depth'=>2));
212 $record = $DB->get_record('context_temp', array());
213 $this->assertEquals(5, $record->id);
216 $this->assertEquals(0, $DB->count_records('course_display'));
217 $originaldisplayid = $DB->insert_record('course_display', array('userid'=>2, 'course'=>1, 'display'=>1));
218 $this->assertEquals(1, $originaldisplayid);
221 $course = $this->getDataGenerator()->create_course();
222 $this->assertEquals(2, $course->id);
224 $this->assertEquals(2, $DB->count_records('user'));
225 $DB->delete_records('user', array('id'=>1));
226 $this->assertEquals(1, $DB->count_records('user'));
230 $this->resetAllData();
232 $this->assertEquals(1, $DB->count_records('course')); // only frontpage in new site
233 $this->assertEquals(0, $DB->count_records('context_temp')); // only frontpage in new site
234 $course = $this->getDataGenerator()->create_course();
235 $this->assertEquals(2, $course->id);
238 $displayid = $DB->insert_record('course_display', array('userid'=>2, 'course'=>1, 'display'=>1));
239 $this->assertEquals($originaldisplayid, $displayid);
242 $this->assertEquals(2, $DB->count_records('user'));
243 $DB->delete_records('user', array('id'=>2));
244 $user = $this->getDataGenerator()->create_user();
245 $this->assertEquals(3, $user->id);
249 $this->resetAllData();
251 $course = $this->getDataGenerator()->create_course();
252 $this->assertEquals(2, $course->id);
254 $this->assertEquals(2, $DB->count_records('user'));
255 $DB->delete_records('user', array('id'=>2));
259 $this->resetAllData();
261 $course = $this->getDataGenerator()->create_course();
262 $this->assertEquals(2, $course->id);
264 $this->assertEquals(2, $DB->count_records('user'));
267 public function test_change_detection() {
268 global $DB, $CFG, $COURSE, $SITE, $USER;
270 $this->preventResetByRollback();
271 phpunit_util::reset_all_data(true);
274 $this->assertEquals(1, $DB->get_field('user', 'confirmed', array('id'=>2)));
275 $DB->set_field('user', 'confirmed', 0, array('id'=>2));
277 phpunit_util::reset_all_data(true);
278 } catch (Exception $e) {
279 $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $e);
281 $this->assertEquals(1, $DB->get_field('user', 'confirmed', array('id'=>2)));
286 $CFG->rolesactive = 0;
288 phpunit_util::reset_all_data(true);
289 } catch (Exception $e) {
290 $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $e);
291 $this->assertContains('xx', $e->getMessage());
292 $this->assertContains('admin', $e->getMessage());
293 $this->assertContains('rolesactive', $e->getMessage());
295 $this->assertFalse(isset($CFG->xx));
296 $this->assertTrue(isset($CFG->admin));
297 $this->assertEquals(1, $CFG->rolesactive);
300 $_SERVER['xx'] = 'yy';
301 phpunit_util::reset_all_data(true);
302 $this->assertFalse(isset($_SERVER['xx']));
306 $COURSE = new stdClass();
309 phpunit_util::reset_all_data(true);
310 } catch (Exception $e) {
311 $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $e);
312 $this->assertEquals(1, $SITE->id);
313 $this->assertSame($SITE, $COURSE);
314 $this->assertSame($SITE, $COURSE);
320 phpunit_util::reset_all_data(true);
321 } catch (Exception $e) {
322 $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $e);
323 $this->assertEquals(0, $USER->id);
327 public function test_getDataGenerator() {
328 $generator = $this->getDataGenerator();
329 $this->assertInstanceOf('phpunit_data_generator', $generator);
332 public function test_database_mock1() {
336 $DB->get_record('pokus', array());
337 $this->fail('Exception expected when accessing non existent table');
338 } catch (dml_exception $e) {
339 $this->assertTrue(true);
341 $DB = $this->getMock(get_class($DB));
342 $this->assertNull($DB->get_record('pokus', array()));
343 // test continues after reset
346 public function test_database_mock2() {
349 // now the database should be back to normal
350 $this->assertFalse($DB->get_record('user', array('id'=>9999)));
353 public function test_load_dataset() {
356 $this->resetAfterTest(true);
358 $this->assertFalse($DB->record_exists('user', array('id'=>5)));
359 $this->assertFalse($DB->record_exists('user', array('id'=>7)));
360 $dataset = $this->createXMLDataSet(__DIR__.'/fixtures/sample_dataset.xml');
361 $this->loadDataSet($dataset);
362 $this->assertTrue($DB->record_exists('user', array('id'=>5)));
363 $this->assertTrue($DB->record_exists('user', array('id'=>7)));
364 $user5 = $DB->get_record('user', array('id'=>5));
365 $user7 = $DB->get_record('user', array('id'=>7));
366 $this->assertEquals('john.doe', $user5->username);
367 $this->assertEquals('jane.doe', $user7->username);
369 $dataset = $this->createCsvDataSet(array('user'=>__DIR__.'/fixtures/sample_dataset.csv'));
370 $this->loadDataSet($dataset);
371 $this->assertEquals(8, $DB->get_field('user', 'id', array('username'=>'pepa.novak')));
372 $this->assertEquals(9, $DB->get_field('user', 'id', array('username'=>'bozka.novakova')));
376 array('username', 'email'),
377 array('top.secret', 'top@example.com'),
378 array('low.secret', 'low@example.com'),
381 $dataset = $this->createArrayDataSet($data);
382 $this->loadDataSet($dataset);
383 $this->assertTrue($DB->record_exists('user', array('email'=>'top@example.com')));
384 $this->assertTrue($DB->record_exists('user', array('email'=>'low@example.com')));
388 array('username'=>'noidea', 'email'=>'noidea@example.com'),
389 array('username'=>'onemore', 'email'=>'onemore@example.com'),
392 $dataset = $this->createArrayDataSet($data);
393 $this->loadDataSet($dataset);
394 $this->assertTrue($DB->record_exists('user', array('username'=>'noidea')));
395 $this->assertTrue($DB->record_exists('user', array('username'=>'onemore')));
401 * Test data generator
405 * @copyright 2012 Petr Skoda {@link http://skodak.org}
406 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
408 class core_phpunit_generator_testcase extends advanced_testcase {
409 public function test_create() {
412 $this->resetAfterTest(true);
413 $generator = $this->getDataGenerator();
415 $count = $DB->count_records('user');
416 $user = $generator->create_user();
417 $this->assertEquals($count+1, $DB->count_records('user'));
419 $count = $DB->count_records('course_categories');
420 $category = $generator->create_category();
421 $this->assertEquals($count+1, $DB->count_records('course_categories'));
423 $count = $DB->count_records('course');
424 $course = $generator->create_course();
425 $this->assertEquals($count+1, $DB->count_records('course'));
427 $section = $generator->create_course_section(array('course'=>$course->id, 'section'=>3));
428 $this->assertEquals($course->id, $section->course);
430 $scale = $generator->create_scale();
431 $this->assertNotEmpty($scale);
434 public function test_create_module() {
436 if (!file_exists("$CFG->dirroot/mod/page/")) {
437 $this->markTestSkipped('Can not find standard Page module');
440 $this->resetAfterTest(true);
441 $generator = $this->getDataGenerator();
443 $page = $generator->create_module('page', array('course'=>$SITE->id));
444 $this->assertNotEmpty($page);
447 public function test_create_block() {
449 if (!file_exists("$CFG->dirroot/blocks/online_users/")) {
450 $this->markTestSkipped('Can not find standard Online users block');
453 $this->resetAfterTest(true);
454 $generator = $this->getDataGenerator();
456 $page = $generator->create_block('online_users');
457 $this->assertNotEmpty($page);