MDL-36547 add test for section number when generating modules
[moodle.git] / lib / phpunit / tests / generator_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 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 data generator
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_generator_testcase extends advanced_testcase {
38     public function test_create() {
39         global $DB;
41         $this->resetAfterTest(true);
42         $generator = $this->getDataGenerator();
44         $count = $DB->count_records('user');
45         $user = $generator->create_user();
46         $this->assertEquals($count+1, $DB->count_records('user'));
47         $this->assertSame($user->username, clean_param($user->username, PARAM_USERNAME));
48         $this->assertSame($user->email, clean_param($user->email, PARAM_EMAIL));
49         $user = $generator->create_user(array('firstname'=>'Žluťoučký', 'lastname'=>'Koníček'));
50         $this->assertSame($user->username, clean_param($user->username, PARAM_USERNAME));
51         $this->assertSame($user->email, clean_param($user->email, PARAM_EMAIL));
53         $count = $DB->count_records('course_categories');
54         $category = $generator->create_category();
55         $this->assertEquals($count+1, $DB->count_records('course_categories'));
56         $this->assertRegExp('/^Course category \d/', $category->name);
57         $this->assertSame('', $category->idnumber);
58         $this->assertRegExp('/^Test course category \d/', $category->description);
59         $this->assertSame(FORMAT_MOODLE, $category->descriptionformat);
61         $count = $DB->count_records('course');
62         $course = $generator->create_course();
63         $this->assertEquals($count+1, $DB->count_records('course'));
64         $this->assertRegExp('/^Test course \d/', $course->fullname);
65         $this->assertRegExp('/^tc_\d/', $course->shortname);
66         $this->assertSame('', $course->idnumber);
67         $this->assertSame('topics', $course->format);
68         $this->assertEquals(0, $course->newsitems);
69         $this->assertEquals(5, $course->numsections);
70         $this->assertRegExp('/^Test course \d/', $course->summary);
71         $this->assertSame(FORMAT_MOODLE, $course->summaryformat);
73         $section = $generator->create_course_section(array('course'=>$course->id, 'section'=>3));
74         $this->assertEquals($course->id, $section->course);
76         $scale = $generator->create_scale();
77         $this->assertNotEmpty($scale);
78     }
80     public function test_create_module() {
81         global $CFG, $SITE;
82         if (!file_exists("$CFG->dirroot/mod/page/")) {
83             $this->markTestSkipped('Can not find standard Page module');
84         }
86         $this->resetAfterTest(true);
87         $generator = $this->getDataGenerator();
89         $page = $generator->create_module('page', array('course'=>$SITE->id));
90         $this->assertNotEmpty($page);
91         $cm = get_coursemodule_from_instance('page', $page->id, $SITE->id, true);
92         $this->assertEquals(0, $cm->sectionnum);
94         $page = $generator->create_module('page', array('course'=>$SITE->id), array('section'=>3));
95         $this->assertNotEmpty($page);
96         $cm = get_coursemodule_from_instance('page', $page->id, $SITE->id, true);
97         $this->assertEquals(3, $cm->sectionnum);
98     }
100     public function test_create_block() {
101         global $CFG;
102         if (!file_exists("$CFG->dirroot/blocks/online_users/")) {
103             $this->markTestSkipped('Can not find standard Online users block');
104         }
106         $this->resetAfterTest(true);
107         $generator = $this->getDataGenerator();
109         $page = $generator->create_block('online_users');
110         $this->assertNotEmpty($page);
111     }