MDL-32505 course: drop course_display table and settings
[moodle.git] / lib / tests / phpunit_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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.
13 //
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/>.
17 /**
18  * PHPUnit integration unit tests
19  *
20  * @package    core
21  * @category   phpunit
22  * @copyright  2012 Petr Skoda {@link http://skodak.org}
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die();
29 /**
30  * Test basic_testcase extra features and PHPUnit Moodle integration.
31  *
32  * @package    core
33  * @category   phpunit
34  * @copyright  2012 Petr Skoda {@link http://skodak.org}
35  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36  */
37 class core_phpunit_basic_testcase extends basic_testcase {
39     /**
40      * Tests that bootstrapping has occurred correctly
41      * @return void
42      */
43     public function test_bootstrap() {
44         global $CFG;
45         $this->assertTrue(isset($CFG->httpswwwroot));
46         $this->assertEquals($CFG->httpswwwroot, $CFG->wwwroot);
47         $this->assertEquals($CFG->prefix, $CFG->phpunit_prefix);
48     }
50     /**
51      * This is just a verification if I understand the PHPUnit assert docs right --skodak
52      * @return void
53      */
54     public function test_assert_behaviour() {
55         // arrays
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);
65         // objects
66         $a = new stdClass();
67         $a->x = 'x';
68         $a->y = 'y';
69         $b = new stdClass(); // switched order
70         $b->y = 'y';
71         $b->x = 'x';
72         $c = $a;
73         $d = new stdClass();
74         $d->x = 'x';
75         $d->y = 'y';
76         $d->z = 'z';
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);
83         // string comparison
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(), '');
92         // other comparison
93         $this->assertEquals(null, null);
94         $this->assertEquals(false, null);
95         $this->assertEquals(0, null);
97         // emptiness
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());
114     }
116 // Uncomment following tests to see logging of unexpected changes in global state and database
117 /*
118     public function test_db_modification() {
119         global $DB;
120         $DB->set_field('user', 'confirmed', 1, array('id'=>-1));
121     }
123     public function test_cfg_modification() {
124         global $CFG;
125         $CFG->xx = 'yy';
126         unset($CFG->admin);
127         $CFG->rolesactive = 0;
128     }
130     public function test_user_modification() {
131         global $USER;
132         $USER->id = 10;
133     }
135     public function test_course_modification() {
136         global $COURSE;
137         $COURSE->id = 10;
138     }
140     public function test_all_modifications() {
141         global $DB, $CFG, $USER, $COURSE;
142         $DB->set_field('user', 'confirmed', 1, array('id'=>-1));
143         $CFG->xx = 'yy';
144         unset($CFG->admin);
145         $CFG->rolesactive = 0;
146         $USER->id = 10;
147         $COURSE->id = 10;
148     }
150     public function test_transaction_problem() {
151         global $DB;
152         $DB->start_delegated_transaction();
153     }
154 */
158 /**
159  * Test advanced_testcase extra features.
160  *
161  * @package    core
162  * @category   phpunit
163  * @copyright  2012 Petr Skoda {@link http://skodak.org}
164  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
165  */
166 class core_phpunit_advanced_testcase extends advanced_testcase {
168     public function test_set_user() {
169         global $USER, $DB;
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);
180         $USER->id = 3;
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);
198     }
200     public function test_database_reset() {
201         global $DB;
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);
215         /** FIXME: danp
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);
219          */
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'));
228         //=========
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);
237         /** FIXME: danp
238         $displayid = $DB->insert_record('course_display', array('userid'=>2, 'course'=>1, 'display'=>1));
239         $this->assertEquals($originaldisplayid, $displayid);
240          */
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);
247         // =========
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));
257         //==========
259         $this->resetAllData();
261         $course = $this->getDataGenerator()->create_course();
262         $this->assertEquals(2, $course->id);
264         $this->assertEquals(2, $DB->count_records('user'));
265     }
267     public function test_change_detection() {
268         global $DB, $CFG, $COURSE, $SITE, $USER;
270         $this->preventResetByRollback();
271         phpunit_util::reset_all_data(true);
273         // database change
274         $this->assertEquals(1, $DB->get_field('user', 'confirmed', array('id'=>2)));
275         $DB->set_field('user', 'confirmed', 0, array('id'=>2));
276         try {
277             phpunit_util::reset_all_data(true);
278         } catch (Exception $e) {
279             $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $e);
280         }
281         $this->assertEquals(1, $DB->get_field('user', 'confirmed', array('id'=>2)));
283         // config change
284         $CFG->xx = 'yy';
285         unset($CFG->admin);
286         $CFG->rolesactive = 0;
287         try {
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());
294         }
295         $this->assertFalse(isset($CFG->xx));
296         $this->assertTrue(isset($CFG->admin));
297         $this->assertEquals(1, $CFG->rolesactive);
299         //silent changes
300         $_SERVER['xx'] = 'yy';
301         phpunit_util::reset_all_data(true);
302         $this->assertFalse(isset($_SERVER['xx']));
304         // COURSE
305         $SITE->id = 10;
306         $COURSE = new stdClass();
307         $COURSE->id = 7;
308         try {
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);
315         }
317         // USER change
318         $this->setUser(2);
319         try {
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);
324         }
325     }
327     public function test_getDataGenerator() {
328         $generator = $this->getDataGenerator();
329         $this->assertInstanceOf('phpunit_data_generator', $generator);
330     }
332     public function test_database_mock1() {
333         global $DB;
335         try {
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);
340         }
341         $DB = $this->getMock(get_class($DB));
342         $this->assertNull($DB->get_record('pokus', array()));
343         // test continues after reset
344     }
346     public function test_database_mock2() {
347         global $DB;
349         // now the database should be back to normal
350         $this->assertFalse($DB->get_record('user', array('id'=>9999)));
351     }
353     public function test_load_dataset() {
354         global $DB;
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')));
374         $data = array(
375             'user' => array(
376                 array('username', 'email'),
377                 array('top.secret', 'top@example.com'),
378                 array('low.secret', 'low@example.com'),
379             ),
380         );
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')));
386         $data = array(
387             'user' => array(
388                 array('username'=>'noidea', 'email'=>'noidea@example.com'),
389                 array('username'=>'onemore', 'email'=>'onemore@example.com'),
390             ),
391         );
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')));
396     }
400 /**
401  * Test data generator
402  *
403  * @package    core
404  * @category   phpunit
405  * @copyright  2012 Petr Skoda {@link http://skodak.org}
406  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
407  */
408 class core_phpunit_generator_testcase extends advanced_testcase {
409     public function test_create() {
410         global $DB;
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);
432     }
434     public function test_create_module() {
435         global $CFG, $SITE;
436         if (!file_exists("$CFG->dirroot/mod/page/")) {
437             $this->markTestSkipped('Can not find standard Page module');
438         }
440         $this->resetAfterTest(true);
441         $generator = $this->getDataGenerator();
443         $page = $generator->create_module('page', array('course'=>$SITE->id));
444         $this->assertNotEmpty($page);
445     }
447     public function test_create_block() {
448         global $CFG;
449         if (!file_exists("$CFG->dirroot/blocks/online_users/")) {
450             $this->markTestSkipped('Can not find standard Online users block');
451         }
453         $this->resetAfterTest(true);
454         $generator = $this->getDataGenerator();
456         $page = $generator->create_block('online_users');
457         $this->assertNotEmpty($page);
458     }