Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * Unit test for query building.
19
 *
20
 * @package    block_dash
21
 * @copyright  2020 bdecent gmbh <https://bdecent.de>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace block_dash;
26
 
27
use block_dash\local\dash_framework\query_builder\builder;
28
use block_dash\local\dash_framework\query_builder\exception\invalid_operator_exception;
29
use block_dash\local\dash_framework\query_builder\exception\invalid_where_clause_exception;
30
use block_dash\local\dash_framework\query_builder\where;
31
 
32
/**
33
 * Unit test for query building.
34
 *
35
 * @group block_dash
36
 * @group bdecent
37
 * @group query_builder_test
38
 */
39
class framework_query_builder_test extends \advanced_testcase {
40
 
41
    /**
42
     * Test for where() to ensure that the where conditions are correctly applied.
43
     *
44
     * @covers ::where
45
     * @return void
46
     */
47
    public function test_where() {
48
        $this->resetAfterTest();
49
 
50
        $category = $this->getDataGenerator()->create_category();
51
 
52
        $course1 = $this->getDataGenerator()->create_course([
53
            'shortname' => 'test1',
54
            'fullname' => 'Testing course 1',
55
            'category' => $category->id,
56
        ]);
57
        $course2 = $this->getDataGenerator()->create_course([
58
            'shortname' => 'test2',
59
            'fullname' => 'Testing course 2',
60
        ]);
61
        $course3 = $this->getDataGenerator()->create_course([
62
            'shortname' => 'test3',
63
            'fullname' => 'Testing course 3',
64
            'category' => $category->id,
65
        ]);
66
 
67
        // Test OPERATOR_EQUAL.
68
        $builder = new builder();
69
        $builder
70
            ->select('c.fullname', 'c_fullname')
71
            ->from('course', 'c')
72
            ->where('c.id', [$course1->id]);
73
        $result = array_values($builder->query());
74
        $this->assertCount(1, $result);
75
        $this->assertEquals('Testing course 1', $result[0]->c_fullname);
76
 
77
        // Test OPERATOR_IN.
78
        $builder = new builder();
79
        $builder
80
            ->select('c.fullname', 'c_fullname')
81
            ->select('c.category', 'c_category')
82
            ->from('course', 'c')
83
            ->where('c.id', [$course1->id, $course3->id], where::OPERATOR_IN);
84
        $result = array_values($builder->query());
85
        $this->assertCount(2, $result);
86
        $this->assertEquals($category->id, $result[0]->c_category);
87
        $this->assertEquals($category->id, $result[1]->c_category);
88
 
89
        $this->expectException(invalid_where_clause_exception::class);
90
        $builder->where('c.id', []);
91
        $builder->query();
92
    }
93
 
94
    /**
95
     * Test for where_in_query() to ensure that the where query.
96
     *
97
     * @covers ::where_in_query
98
     * @return void
99
     */
100
    public function test_where_in_query() {
101
        $this->resetAfterTest();
102
 
103
        $course1 = $this->getDataGenerator()->create_course([
104
            'shortname' => 'test1',
105
            'fullname' => 'Testing course 1',
106
            'startdate' => 946684800,
107
        ]);
108
        $course2 = $this->getDataGenerator()->create_course([
109
            'shortname' => 'test2',
110
            'fullname' => 'Testing course 2',
111
            'startdate' => 946684800,
112
        ]);
113
        $course3 = $this->getDataGenerator()->create_course([
114
            'shortname' => 'test3',
115
            'fullname' => 'Testing course 3',
116
            'startdate' => time(),
117
        ]);
118
 
119
        $builder = new builder();
120
        $builder
121
            ->select('c.fullname', 'c_fullname')
122
            ->from('course', 'c')
123
            ->where_in_query('c.id', 'SELECT c2.id FROM {course} c2
124
                WHERE c2.startdate > 0 AND c2.startdate <= :y2k', ['y2k' => 946684800]);
125
 
126
        $result = array_values($builder->query());
127
        $this->assertCount(2, $result);
128
        $this->assertEquals('Testing course 1', $result[0]->c_fullname);
129
        $this->assertEquals('Testing course 2', $result[1]->c_fullname);
130
 
131
        $this->expectException(invalid_operator_exception::class);
132
        $builder = new builder();
133
        $builder->select('c.id', 'c_id')->from('course', 'c')->where('c.id', [1], 'missing');
134
        $builder->query();
135
    }
136
 
137
    /**
138
     * Test for limits() to ensure that the dtatatables limits function.
139
     *
140
     * @covers ::limits
141
     * @return void
142
     */
143
    public function test_limits() {
144
        $this->resetAfterTest();
145
 
146
        $users = [];
147
        for ($i = 0; $i < 10; $i++) {
148
            $users[] = $this->getDataGenerator()->create_user(['middlename' => 'John']);
149
        }
150
 
151
        $builder = new builder();
152
        $builder
153
            ->select('u.id', 'u_id')
154
            ->from('user', 'u')
155
            ->limitfrom(5)
156
            ->limitnum(2)
157
            ->where('u.middlename', ['John']);
158
 
159
        $results = array_values($builder->query());
160
        $this->assertCount(2, $results);
161
        $this->assertEquals($users[5]->id, $results[0]->u_id);
162
        $this->assertEquals($users[6]->id, $results[1]->u_id);
163
    }
164
 
165
    /**
166
     * Test for orderby() to confirm the order by of datasource works.
167
     *
168
     * @covers ::orderby
169
     * @return void
170
     */
171
    public function test_orderby() {
172
        $this->resetAfterTest();
173
 
174
        $courses = [];
175
        for ($i = 0; $i < 10; $i++) {
176
            $courses[] = $this->getDataGenerator()->create_course();
177
        }
178
 
179
        $builder = new builder();
180
        $builder
181
            ->select('c.id', 'c_id')
182
            ->from('course', 'c')
183
            ->where('c.format', ['site'], where::OPERATOR_NOT_EQUAL);
184
        $builder->orderby('c.id', 'DESC');
185
        $results = array_values($builder->query());
186
 
187
        $this->assertEquals($courses[9]->id, $results[0]->c_id);
188
 
189
        $builder->orderby('c.id', 'ASC');
190
        $results = array_values($builder->query());
191
 
192
        $this->assertEquals($courses[0]->id, $results[0]->c_id);
193
 
194
        $this->expectException(\coding_exception::class);
195
        $builder->orderby('c.id', 'wrong');
196
    }
197
 
198
    /**
199
     * Test for joins() to ensure that the table joins works.
200
     *
201
     * @covers ::joins
202
     * @return void
203
     */
204
    public function test_joins() {
205
        $this->resetAfterTest();
206
 
207
        $category = $this->getDataGenerator()->create_category();
208
 
209
        $courses = [];
210
        for ($i = 0; $i < 10; $i++) {
211
            $courses[] = $this->getDataGenerator()->create_course(['category' => $category->id]);
212
        }
213
 
214
        $builder = new builder();
215
        $builder
216
            ->select('c.id', 'c_id')
217
            ->select('cc.name', 'cc_name')
218
            ->from('course', 'c')
219
            ->join('course_categories', 'cc', 'id', 'c.category')
220
            ->join_condition('cc', 'cc.parent = 0');
221
 
222
        $results = array_values($builder->query());
223
        $this->assertEquals($category->name, $results[0]->cc_name);
224
 
225
        $this->expectException(\coding_exception::class);
226
        $builder->join_condition('missing', '');
227
    }
228
}