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/>.
16
17/**
18 * PHPUnit Util tests
19 *
20 * @package    core
21 * @category   phpunit
22 * @copyright  2015 Andrew Nicols <andrew@nicols.co.uk>
23 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25defined('MOODLE_INTERNAL') || die();
26
27/**
28 * Test util extra features.
29 *
30 * @package    core
31 * @category   phpunit
32 * @copyright  2015 Andrew Nicols <andrew@nicols.co.uk>
33 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35class core_phpunit_util_testcase extends advanced_testcase {
36    /**
37     * @dataProvider set_table_modified_by_sql_provider
38     */
39    public function test_set_table_modified_by_sql($sql, $expectations) {
40        phpunit_util::reset_updated_table_list();
41        phpunit_util::set_table_modified_by_sql($sql);
42        foreach ($expectations as $table => $present) {
43            $this->assertEquals($present, !empty(phpunit_util::$tableupdated[$table]));
44        }
45    }
46
47    public function set_table_modified_by_sql_provider() {
48        global $DB;
49        $prefix = $DB->get_prefix();
50
51        return array(
52            'Basic update' => array(
53                'sql'           => "UPDATE {$prefix}user SET username = username || '_test'",
54                'expectations'  => array(
55                    'user'      => true,
56                    'course'    => false,
57                ),
58            ),
59            'Basic update with a fieldname sharing the same prefix' => array(
60                'sql'           => "UPDATE {$prefix}user SET {$prefix}username = username || '_test'",
61                'expectations'  => array(
62                    'user'      => true,
63                    'course'    => false,
64                ),
65            ),
66            'Basic update with a table which contains the prefix' => array(
67                'sql'           => "UPDATE {$prefix}user{$prefix} SET username = username || '_test'",
68                'expectations'  => array(
69                    "user{$prefix}" => true,
70                    'course'        => false,
71                ),
72            ),
73            'Update table with a numeric name' => array(
74                'sql'           => "UPDATE {$prefix}example42 SET username = username || '_test'",
75                'expectations'  => array(
76                    'example42' => true,
77                    'user'      => false,
78                    'course'    => false,
79                ),
80            ),
81            'Drop basic table' => array(
82                'sql'           => "DROP TABLE {$prefix}user",
83                'expectations'  => array(
84                    'user'      => true,
85                    'course'    => false,
86                ),
87            ),
88            'Drop table with a numeric name' => array(
89                'sql'           => "DROP TABLE {$prefix}example42",
90                'expectations'  => array(
91                    'example42' => true,
92                    'user'      => false,
93                    'course'    => false,
94                ),
95            ),
96            'Insert in table' => array(
97                'sql'           => "INSERT INTO {$prefix}user (username,password) VALUES ('moodle', 'test')",
98                'expectations'  => array(
99                    'user'      => true,
100                    'course'    => false,
101                ),
102            ),
103        );
104    }
105}
106