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 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 advanced_testcase extra features.
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_advanced_testcase extends advanced_testcase {
39 public function test_set_user() {
42 $this->assertEquals(0, $USER->id);
43 $this->assertSame($_SESSION['USER'], $USER);
45 $user = $DB->get_record('user', array('id'=>2));
46 $this->setUser($user);
47 $this->assertEquals(2, $USER->id);
48 $this->assertEquals(2, $_SESSION['USER']->id);
49 $this->assertSame($_SESSION['USER'], $USER);
52 $this->assertEquals(3, $USER->id);
53 $this->assertEquals(3, $_SESSION['USER']->id);
54 $this->assertSame($_SESSION['USER'], $USER);
56 session_set_user($user);
57 $this->assertEquals(2, $USER->id);
58 $this->assertEquals(2, $_SESSION['USER']->id);
59 $this->assertSame($_SESSION['USER'], $USER);
61 $USER = $DB->get_record('user', array('id'=>1));
62 $this->assertEquals(1, $USER->id);
63 $this->assertEquals(1, $_SESSION['USER']->id);
64 $this->assertSame($_SESSION['USER'], $USER);
67 $this->assertEquals(0, $USER->id);
68 $this->assertSame($_SESSION['USER'], $USER);
71 public function test_database_reset() {
74 $this->resetAfterTest(true);
76 $this->preventResetByRollback();
78 $this->assertEquals(1, $DB->count_records('course')); // only frontpage in new site
80 // this is weird table - id is NOT a sequence here
81 $this->assertEquals(0, $DB->count_records('context_temp'));
82 $DB->import_record('context_temp', array('id'=>5, 'path'=>'/1/2', 'depth'=>2));
83 $record = $DB->get_record('context_temp', array());
84 $this->assertEquals(5, $record->id);
86 $this->assertEquals(0, $DB->count_records('user_preferences'));
87 $originaldisplayid = $DB->insert_record('user_preferences', array('userid'=>2, 'name'=> 'phpunittest', 'value'=>'x'));
88 $this->assertEquals(1, $originaldisplayid);
90 $course = $this->getDataGenerator()->create_course();
91 $this->assertEquals(2, $course->id);
93 $this->assertEquals(2, $DB->count_records('user'));
94 $DB->delete_records('user', array('id'=>1));
95 $this->assertEquals(1, $DB->count_records('user'));
99 $this->resetAllData();
101 $this->assertEquals(1, $DB->count_records('course')); // only frontpage in new site
102 $this->assertEquals(0, $DB->count_records('context_temp')); // only frontpage in new site
103 $course = $this->getDataGenerator()->create_course();
104 $this->assertEquals(2, $course->id);
106 $displayid = $DB->insert_record('user_preferences', array('userid'=>2, 'name'=> 'phpunittest', 'value'=>'x'));
107 $this->assertEquals($originaldisplayid, $displayid);
109 $this->assertEquals(2, $DB->count_records('user'));
110 $DB->delete_records('user', array('id'=>2));
111 $user = $this->getDataGenerator()->create_user();
112 $this->assertEquals(3, $user->id);
116 $this->resetAllData();
118 $course = $this->getDataGenerator()->create_course();
119 $this->assertEquals(2, $course->id);
121 $this->assertEquals(2, $DB->count_records('user'));
122 $DB->delete_records('user', array('id'=>2));
126 $this->resetAllData();
128 $course = $this->getDataGenerator()->create_course();
129 $this->assertEquals(2, $course->id);
131 $this->assertEquals(2, $DB->count_records('user'));
134 public function test_change_detection() {
135 global $DB, $CFG, $COURSE, $SITE, $USER;
137 $this->preventResetByRollback();
138 phpunit_util::reset_all_data(true);
141 $this->assertEquals(1, $DB->get_field('user', 'confirmed', array('id'=>2)));
142 $DB->set_field('user', 'confirmed', 0, array('id'=>2));
144 phpunit_util::reset_all_data(true);
145 } catch (Exception $e) {
146 $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $e);
148 $this->assertEquals(1, $DB->get_field('user', 'confirmed', array('id'=>2)));
153 $CFG->rolesactive = 0;
155 phpunit_util::reset_all_data(true);
156 } catch (Exception $e) {
157 $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $e);
158 $this->assertContains('xx', $e->getMessage());
159 $this->assertContains('admin', $e->getMessage());
160 $this->assertContains('rolesactive', $e->getMessage());
162 $this->assertFalse(isset($CFG->xx));
163 $this->assertTrue(isset($CFG->admin));
164 $this->assertEquals(1, $CFG->rolesactive);
167 $_SERVER['xx'] = 'yy';
168 phpunit_util::reset_all_data(true);
169 $this->assertFalse(isset($_SERVER['xx']));
173 $COURSE = new stdClass();
176 phpunit_util::reset_all_data(true);
177 } catch (Exception $e) {
178 $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $e);
179 $this->assertEquals(1, $SITE->id);
180 $this->assertSame($SITE, $COURSE);
181 $this->assertSame($SITE, $COURSE);
187 phpunit_util::reset_all_data(true);
188 } catch (Exception $e) {
189 $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $e);
190 $this->assertEquals(0, $USER->id);
194 public function test_getDataGenerator() {
195 $generator = $this->getDataGenerator();
196 $this->assertInstanceOf('phpunit_data_generator', $generator);
199 public function test_database_mock1() {
203 $DB->get_record('pokus', array());
204 $this->fail('Exception expected when accessing non existent table');
205 } catch (dml_exception $e) {
206 $this->assertTrue(true);
208 $DB = $this->getMock(get_class($DB));
209 $this->assertNull($DB->get_record('pokus', array()));
210 // test continues after reset
213 public function test_database_mock2() {
216 // now the database should be back to normal
217 $this->assertFalse($DB->get_record('user', array('id'=>9999)));
220 public function test_load_dataset() {
223 $this->resetAfterTest(true);
225 $this->assertFalse($DB->record_exists('user', array('id'=>5)));
226 $this->assertFalse($DB->record_exists('user', array('id'=>7)));
227 $dataset = $this->createXMLDataSet(__DIR__.'/fixtures/sample_dataset.xml');
228 $this->loadDataSet($dataset);
229 $this->assertTrue($DB->record_exists('user', array('id'=>5)));
230 $this->assertTrue($DB->record_exists('user', array('id'=>7)));
231 $user5 = $DB->get_record('user', array('id'=>5));
232 $user7 = $DB->get_record('user', array('id'=>7));
233 $this->assertEquals('john.doe', $user5->username);
234 $this->assertEquals('jane.doe', $user7->username);
236 $dataset = $this->createCsvDataSet(array('user'=>__DIR__.'/fixtures/sample_dataset.csv'));
237 $this->loadDataSet($dataset);
238 $this->assertEquals(8, $DB->get_field('user', 'id', array('username'=>'pepa.novak')));
239 $this->assertEquals(9, $DB->get_field('user', 'id', array('username'=>'bozka.novakova')));
243 array('username', 'email'),
244 array('top.secret', 'top@example.com'),
245 array('low.secret', 'low@example.com'),
248 $dataset = $this->createArrayDataSet($data);
249 $this->loadDataSet($dataset);
250 $this->assertTrue($DB->record_exists('user', array('email'=>'top@example.com')));
251 $this->assertTrue($DB->record_exists('user', array('email'=>'low@example.com')));
255 array('username'=>'noidea', 'email'=>'noidea@example.com'),
256 array('username'=>'onemore', 'email'=>'onemore@example.com'),
259 $dataset = $this->createArrayDataSet($data);
260 $this->loadDataSet($dataset);
261 $this->assertTrue($DB->record_exists('user', array('username'=>'noidea')));
262 $this->assertTrue($DB->record_exists('user', array('username'=>'onemore')));