Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | 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
namespace core;
18
 
19
use advanced_testcase;
20
use coding_exception;
21
use dml_missing_record_exception;
22
use lang_string;
23
use xmldb_table;
24
 
25
/**
26
 * Persistent testcase.
27
 *
28
 * @package    core
29
 * @copyright  2015 Frédéric Massart - FMCorz.net
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 * @covers     \core\persistent
32
 */
1441 ariadna 33
final class persistent_test extends advanced_testcase {
1 efrain 34
 
35
    public function setUp(): void {
1441 ariadna 36
        parent::setUp();
1 efrain 37
        $this->make_persistent_table();
38
        $this->make_second_persistent_table();
39
        $this->resetAfterTest();
40
    }
41
 
42
    /**
43
     * Make the table for the persistent.
44
     */
45
    protected function make_persistent_table() {
46
        global $DB;
47
        $dbman = $DB->get_manager();
48
 
49
        $table = new xmldb_table(core_testable_persistent::TABLE);
50
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
51
        $table->add_field('shortname', XMLDB_TYPE_CHAR, '100', null, null, null, null);
52
        $table->add_field('idnumber', XMLDB_TYPE_CHAR, '100', null, null, null, null);
53
        $table->add_field('description', XMLDB_TYPE_TEXT, null, null, null, null, null);
54
        $table->add_field('descriptionformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0');
55
        $table->add_field('parentid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
56
        $table->add_field('path', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
57
        $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
58
        $table->add_field('scaleid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
59
        $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
60
        $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
61
        $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
62
 
63
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
64
 
65
        if ($dbman->table_exists($table)) {
66
            $dbman->drop_table($table);
67
        }
68
 
69
        $dbman->create_table($table);
70
    }
71
 
72
    /**
73
     * Make the second table for the persistent.
74
     */
75
    protected function make_second_persistent_table() {
76
        global $DB;
77
        $dbman = $DB->get_manager();
78
 
79
        $table = new xmldb_table(core_testable_second_persistent::TABLE);
80
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
81
        $table->add_field('someint', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
82
        $table->add_field('intnull', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
83
        $table->add_field('somefloat', XMLDB_TYPE_FLOAT, '10,5', null, null, null, null);
84
        $table->add_field('sometext', XMLDB_TYPE_TEXT, null, null, null, null, null);
85
        $table->add_field('someraw', XMLDB_TYPE_CHAR, '100', null, null, null, null);
86
        $table->add_field('booltrue', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
87
        $table->add_field('boolfalse', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
88
        $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
89
        $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
90
        $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
91
 
92
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
93
 
94
        if ($dbman->table_exists($table)) {
95
            $dbman->drop_table($table);
96
        }
97
 
98
        $dbman->create_table($table);
99
    }
100
 
11 efrain 101
    public function test_properties_definition(): void {
1 efrain 102
        $expected = array(
103
            'shortname' => array(
104
                'type' => PARAM_TEXT,
105
                'default' => '',
106
                'null' => NULL_NOT_ALLOWED
107
            ),
108
            'idnumber' => array(
109
                'type' => PARAM_TEXT,
110
                'null' => NULL_NOT_ALLOWED
111
            ),
112
            'description' => array(
113
                'type' => PARAM_TEXT,
114
                'default' => '',
115
                'null' => NULL_NOT_ALLOWED
116
            ),
117
            'descriptionformat' => array(
118
                'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN),
119
                'type' => PARAM_INT,
120
                'default' => FORMAT_HTML,
121
                'null' => NULL_NOT_ALLOWED
122
            ),
123
            'parentid' => array(
124
                'type' => PARAM_INT,
125
                'default' => 0,
126
                'null' => NULL_NOT_ALLOWED
127
            ),
128
            'path' => array(
129
                'type' => PARAM_RAW,
130
                'default' => '',
131
                'null' => NULL_NOT_ALLOWED
132
            ),
133
            'sortorder' => array(
134
                'type' => PARAM_INT,
135
                'message' => new lang_string('invalidrequest', 'error'),
136
                'null' => NULL_NOT_ALLOWED
137
            ),
138
            'scaleid' => array(
139
                'default' => null,
140
                'type' => PARAM_INT,
141
                'null' => NULL_ALLOWED
142
            ),
143
            'id' => array(
144
                'default' => 0,
145
                'type' => PARAM_INT,
146
                'null' => NULL_NOT_ALLOWED
147
            ),
148
            'timecreated' => array(
149
                'default' => 0,
150
                'type' => PARAM_INT,
151
                'null' => NULL_NOT_ALLOWED
152
            ),
153
            'timemodified' => array(
154
                'default' => 0,
155
                'type' => PARAM_INT,
156
                'null' => NULL_NOT_ALLOWED
157
            ),
158
            'usermodified' => array(
159
                'default' => 0,
160
                'type' => PARAM_INT,
161
                'null' => NULL_NOT_ALLOWED
162
            ),
163
        );
164
        $this->assertEquals($expected, core_testable_persistent::properties_definition());
165
    }
166
 
167
    /**
168
     * Test filtering record properties returns only those defined by the persistent
169
     */
170
    public function test_properties_filter(): void {
171
        $result = core_testable_persistent::properties_filter((object) [
172
            'idnumber' => '123',
173
            'sortorder' => 1,
174
            'invalidparam' => 'abc',
175
        ]);
176
 
177
        // We should get back all data except invalid param.
178
        $this->assertEquals([
179
            'idnumber' => '123',
180
            'sortorder' => 1,
181
        ], $result);
182
    }
183
 
184
    /**
185
     * Test creating persistent instance by specifying record ID in constructor
186
     */
187
    public function test_constructor(): void {
188
        $persistent = (new core_testable_persistent(0, (object) [
189
            'idnumber' => '123',
190
            'sortorder' => 1,
191
        ]))->create();
192
 
193
        // Now create a new instance, passing the original instance ID in the constructor.
194
        $another = new core_testable_persistent($persistent->get('id'));
195
        $this->assertEquals($another->to_record(), $persistent->to_record());
196
    }
197
 
198
    /**
199
     * Test creating persistent instance by specifying non-existing record ID in constructor throws appropriate exception
200
     */
201
    public function test_constructor_invalid(): void {
202
        $this->expectException(dml_missing_record_exception::class);
203
        $this->expectExceptionMessage('Can\'t find data record in database table phpunit_persistent.');
204
        new core_testable_persistent(42);
205
    }
206
 
11 efrain 207
    public function test_to_record(): void {
1 efrain 208
        $p = new core_testable_persistent();
209
        $expected = (object) array(
210
            'shortname' => '',
211
            'idnumber' => null,
212
            'description' => '',
213
            'descriptionformat' => FORMAT_HTML,
214
            'parentid' => 0,
215
            'path' => '',
216
            'sortorder' => null,
217
            'id' => 0,
218
            'timecreated' => 0,
219
            'timemodified' => 0,
220
            'usermodified' => 0,
221
            'scaleid' => null,
222
        );
223
        $this->assertEquals($expected, $p->to_record());
224
    }
225
 
11 efrain 226
    public function test_from_record(): void {
1 efrain 227
        $p = new core_testable_persistent();
228
        $data = (object) array(
229
            'shortname' => 'ddd',
230
            'idnumber' => 'abc',
231
            'description' => 'xyz',
232
            'descriptionformat' => FORMAT_PLAIN,
233
            'parentid' => 999,
234
            'path' => '/a/b/c',
235
            'sortorder' => 12,
236
            'id' => 1,
237
            'timecreated' => 2,
238
            'timemodified' => 3,
239
            'usermodified' => 4,
240
            'scaleid' => null,
241
        );
242
        $p->from_record($data);
243
        $this->assertEquals($data, $p->to_record());
244
    }
245
 
11 efrain 246
    public function test_from_record_invalid_param(): void {
1 efrain 247
        $p = new core_testable_persistent();
248
        $data = (object) array(
249
            'shortname' => 'ddd',
250
            'idnumber' => 'abc',
251
            'description' => 'xyz',
252
            'descriptionformat' => FORMAT_PLAIN,
253
            'parentid' => 999,
254
            'path' => '/a/b/c',
255
            'sortorder' => 12,
256
            'id' => 1,
257
            'timecreated' => 2,
258
            'timemodified' => 3,
259
            'usermodified' => 4,
260
            'scaleid' => null,
261
            'invalidparam' => 'abc'
262
        );
263
 
264
        $p->from_record($data);
265
 
266
        // Previous call should succeed, assert we get back all data except invalid param.
267
        unset($data->invalidparam);
268
        $this->assertEquals($data, $p->to_record());
269
    }
270
 
11 efrain 271
    public function test_validate(): void {
1 efrain 272
        $data = (object) array(
273
            'idnumber' => 'abc',
274
            'sortorder' => 0
275
        );
276
        $p = new core_testable_persistent(0, $data);
277
        $this->assertFalse(isset($p->beforevalidate));
278
        $this->assertTrue($p->validate());
279
        $this->assertTrue(isset($p->beforevalidate));
280
        $this->assertTrue($p->is_valid());
281
        $this->assertEquals(array(), $p->get_errors());
282
        $p->set('descriptionformat', -100);
283
 
284
        $expected = array(
285
            'descriptionformat' => new lang_string('invaliddata', 'error'),
286
        );
287
        $this->assertEquals($expected, $p->validate());
288
        $this->assertFalse($p->is_valid());
289
        $this->assertEquals($expected, $p->get_errors());
290
    }
291
 
11 efrain 292
    public function test_validation_required(): void {
1 efrain 293
        $data = (object) array(
294
            'idnumber' => 'abc'
295
        );
296
        $p = new core_testable_persistent(0, $data);
297
        $expected = array(
298
            'sortorder' => new lang_string('requiredelement', 'form'),
299
        );
300
        $this->assertFalse($p->is_valid());
301
        $this->assertEquals($expected, $p->get_errors());
302
    }
303
 
11 efrain 304
    public function test_validation_custom(): void {
1 efrain 305
        $data = (object) array(
306
            'idnumber' => 'abc',
307
            'sortorder' => 10,
308
        );
309
        $p = new core_testable_persistent(0, $data);
310
        $expected = array(
311
            'sortorder' => new lang_string('invalidkey', 'error'),
312
        );
313
        $this->assertFalse($p->is_valid());
314
        $this->assertEquals($expected, $p->get_errors());
315
    }
316
 
11 efrain 317
    public function test_validation_custom_message(): void {
1 efrain 318
        $data = (object) array(
319
            'idnumber' => 'abc',
320
            'sortorder' => 'abc',
321
        );
322
        $p = new core_testable_persistent(0, $data);
323
        $expected = array(
324
            'sortorder' => new lang_string('invalidrequest', 'error'),
325
        );
326
        $this->assertFalse($p->is_valid());
327
        $this->assertEquals($expected, $p->get_errors());
328
    }
329
 
11 efrain 330
    public function test_validation_choices(): void {
1 efrain 331
        $data = (object) array(
332
            'idnumber' => 'abc',
333
            'sortorder' => 0,
334
            'descriptionformat' => -100
335
        );
336
        $p = new core_testable_persistent(0, $data);
337
        $expected = array(
338
            'descriptionformat' => new lang_string('invaliddata', 'error'),
339
        );
340
        $this->assertFalse($p->is_valid());
341
        $this->assertEquals($expected, $p->get_errors());
342
    }
343
 
11 efrain 344
    public function test_validation_type(): void {
1 efrain 345
        $data = (object) array(
346
            'idnumber' => 'abc',
347
            'sortorder' => 'NaN'
348
        );
349
        $p = new core_testable_persistent(0, $data);
350
        $this->assertFalse($p->is_valid());
351
        $this->assertArrayHasKey('sortorder', $p->get_errors());
352
    }
353
 
11 efrain 354
    public function test_validation_null(): void {
1 efrain 355
        $data = (object) array(
356
            'idnumber' => null,
357
            'sortorder' => 0,
358
            'scaleid' => 'bad!'
359
        );
360
        $p = new core_testable_persistent(0, $data);
361
        $this->assertFalse($p->is_valid());
362
        $this->assertArrayHasKey('idnumber', $p->get_errors());
363
        $this->assertArrayHasKey('scaleid', $p->get_errors());
364
        $p->set('idnumber', 'abc');
365
        $this->assertFalse($p->is_valid());
366
        $this->assertArrayNotHasKey('idnumber', $p->get_errors());
367
        $this->assertArrayHasKey('scaleid', $p->get_errors());
368
        $p->set('scaleid', null);
369
        $this->assertTrue($p->is_valid());
370
        $this->assertArrayNotHasKey('scaleid', $p->get_errors());
371
    }
372
 
11 efrain 373
    public function test_create(): void {
1 efrain 374
        global $DB;
375
        $p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
376
        $this->assertFalse(isset($p->beforecreate));
377
        $this->assertFalse(isset($p->aftercreate));
378
        $p->create();
379
        $record = $DB->get_record(core_testable_persistent::TABLE, array('id' => $p->get('id')), '*', MUST_EXIST);
380
        $expected = $p->to_record();
381
        $this->assertTrue(isset($p->beforecreate));
382
        $this->assertTrue(isset($p->aftercreate));
383
        $this->assertEquals($expected->sortorder, $record->sortorder);
384
        $this->assertEquals($expected->idnumber, $record->idnumber);
385
        $this->assertEquals($expected->id, $record->id);
386
        $this->assertTrue($p->is_valid()); // Should always be valid after a create.
387
    }
388
 
11 efrain 389
    public function test_update(): void {
1 efrain 390
        global $DB;
391
        $p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
392
        $p->create();
393
        $id = $p->get('id');
394
        $p->set('sortorder', 456);
395
        $p->from_record((object) array('idnumber' => 'def'));
396
        $this->assertFalse(isset($p->beforeupdate));
397
        $this->assertFalse(isset($p->afterupdate));
398
        $p->update();
399
 
400
        $expected = $p->to_record();
401
        $record = $DB->get_record(core_testable_persistent::TABLE, array('id' => $p->get('id')), '*', MUST_EXIST);
402
        $this->assertTrue(isset($p->beforeupdate));
403
        $this->assertTrue(isset($p->afterupdate));
404
        $this->assertEquals($id, $record->id);
405
        $this->assertEquals(456, $record->sortorder);
406
        $this->assertEquals('def', $record->idnumber);
407
        $this->assertTrue($p->is_valid()); // Should always be valid after an update.
408
    }
409
 
410
    /**
411
     * Test set_many prior to updating the persistent
412
     */
413
    public function test_set_many_update(): void {
414
        global $DB;
415
 
416
        $persistent = (new core_testable_persistent(0, (object) [
417
            'idnumber' => 'test',
418
            'sortorder' => 2
419
        ]))->create();
420
 
421
        // Set multiple properties, and update.
422
        $persistent->set_many([
423
            'idnumber' => 'test2',
424
            'sortorder' => 1,
425
        ])->update();
426
 
427
        // Confirm our persistent was updated.
428
        $record = $DB->get_record(core_testable_persistent::TABLE, ['id' => $persistent->get('id')], '*', MUST_EXIST);
429
        $this->assertEquals('test2', $record->idnumber);
430
        $this->assertEquals(1, $record->sortorder);
431
    }
432
 
11 efrain 433
    public function test_save(): void {
1 efrain 434
        global $DB;
435
        $p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
436
        $this->assertFalse(isset($p->beforecreate));
437
        $this->assertFalse(isset($p->aftercreate));
438
        $this->assertFalse(isset($p->beforeupdate));
439
        $this->assertFalse(isset($p->beforeupdate));
440
        $p->save();
441
        $record = $DB->get_record(core_testable_persistent::TABLE, array('id' => $p->get('id')), '*', MUST_EXIST);
442
        $expected = $p->to_record();
443
        $this->assertTrue(isset($p->beforecreate));
444
        $this->assertTrue(isset($p->aftercreate));
445
        $this->assertFalse(isset($p->beforeupdate));
446
        $this->assertFalse(isset($p->beforeupdate));
447
        $this->assertEquals($expected->sortorder, $record->sortorder);
448
        $this->assertEquals($expected->idnumber, $record->idnumber);
449
        $this->assertEquals($expected->id, $record->id);
450
        $this->assertTrue($p->is_valid()); // Should always be valid after a save/create.
451
 
452
        $p->set('idnumber', 'abcd');
453
        $p->save();
454
        $record = $DB->get_record(core_testable_persistent::TABLE, array('id' => $p->get('id')), '*', MUST_EXIST);
455
        $expected = $p->to_record();
456
        $this->assertTrue(isset($p->beforeupdate));
457
        $this->assertTrue(isset($p->beforeupdate));
458
        $this->assertEquals($expected->sortorder, $record->sortorder);
459
        $this->assertEquals($expected->idnumber, $record->idnumber);
460
        $this->assertEquals($expected->id, $record->id);
461
        $this->assertTrue($p->is_valid()); // Should always be valid after a save/update.
462
    }
463
 
464
    /**
465
     * Test set_many prior to saving the persistent
466
     */
467
    public function test_set_many_save(): void {
468
        global $DB;
469
 
470
        $persistent = (new core_testable_persistent(0, (object) [
471
            'idnumber' => 'test',
472
            'sortorder' => 2
473
        ]));
474
 
475
        // Set multiple properties, and save.
476
        $persistent->set_many([
477
            'idnumber' => 'test2',
478
            'sortorder' => 1,
479
        ])->save();
480
 
481
        // Confirm our persistent was saved.
482
        $record = $DB->get_record(core_testable_persistent::TABLE, ['id' => $persistent->get('id')], '*', MUST_EXIST);
483
        $this->assertEquals('test2', $record->idnumber);
484
        $this->assertEquals(1, $record->sortorder);
485
    }
486
 
487
    /**
488
     * Test set_many with empty array should not modify the persistent
489
     */
490
    public function test_set_many_empty(): void {
491
        global $DB;
492
 
493
        $persistent = (new core_testable_persistent(0, (object) [
494
            'idnumber' => 'test',
495
            'sortorder' => 2
496
        ]))->create();
497
 
498
        // Set empty properties, and update.
499
        $persistent->set_many([])->update();
500
 
501
        // Confirm our persistent was not updated.
502
        $record = $DB->get_record(core_testable_persistent::TABLE, ['id' => $persistent->get('id')], '*', MUST_EXIST);
503
        $this->assertEquals('test', $record->idnumber);
504
        $this->assertEquals(2, $record->sortorder);
505
    }
506
 
507
    /**
508
     * Test set with invalid property
509
     */
510
    public function test_set_invalid_property(): void {
511
        $persistent = (new core_testable_persistent(0, (object) [
512
            'idnumber' => 'test',
513
            'sortorder' => 2
514
        ]));
515
 
516
        $this->expectException(coding_exception::class);
517
        $this->expectExceptionMessage('Unexpected property \'invalid\' requested');
518
        $persistent->set('invalid', 'stuff');
519
    }
520
 
521
    /**
522
     * Test set_many with invalid property
523
     */
524
    public function test_set_many_invalid_property(): void {
525
        $persistent = (new core_testable_persistent(0, (object) [
526
            'idnumber' => 'test',
527
            'sortorder' => 2
528
        ]));
529
 
530
        $this->expectException(coding_exception::class);
531
        $this->expectExceptionMessage('Unexpected property \'invalid\' requested');
532
        $persistent->set_many(['invalid' => 'stuff']);
533
    }
534
 
11 efrain 535
    public function test_read(): void {
1 efrain 536
        $p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
537
        $p->create();
538
        unset($p->beforevalidate);
539
        unset($p->beforecreate);
540
        unset($p->aftercreate);
541
 
542
        $p2 = new core_testable_persistent($p->get('id'));
543
        $this->assertEquals($p, $p2);
544
 
545
        $p3 = new core_testable_persistent();
546
        $p3->set('id', $p->get('id'));
547
        $p3->read();
548
        $this->assertEquals($p, $p3);
549
    }
550
 
11 efrain 551
    public function test_delete(): void {
1 efrain 552
        global $DB;
553
 
554
        $p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
555
        $p->create();
556
        $this->assertNotEquals(0, $p->get('id'));
557
        $this->assertTrue($DB->record_exists_select(core_testable_persistent::TABLE, 'id = ?', array($p->get('id'))));
558
        $this->assertFalse(isset($p->beforedelete));
559
        $this->assertFalse(isset($p->afterdelete));
560
 
561
        $p->delete();
562
        $this->assertFalse($DB->record_exists_select(core_testable_persistent::TABLE, 'id = ?', array($p->get('id'))));
563
        $this->assertEquals(0, $p->get('id'));
564
        $this->assertEquals(true, $p->beforedelete);
565
        $this->assertEquals(true, $p->afterdelete);
566
    }
567
 
11 efrain 568
    public function test_has_property(): void {
1 efrain 569
        $this->assertFalse(core_testable_persistent::has_property('unknown'));
570
        $this->assertTrue(core_testable_persistent::has_property('idnumber'));
571
    }
572
 
11 efrain 573
    public function test_custom_setter_getter(): void {
1 efrain 574
        global $DB;
575
 
576
        $path = array(1, 2, 3);
577
        $json = json_encode($path);
578
 
579
        $p = new core_testable_persistent(0, (object) array('sortorder' => 0, 'idnumber' => 'abc'));
580
        $p->set('path', $path);
581
        $this->assertEquals($path, $p->get('path'));
582
        $this->assertEquals($json, $p->to_record()->path);
583
 
584
        $p->create();
585
        $record = $DB->get_record(core_testable_persistent::TABLE, array('id' => $p->get('id')), 'id, path', MUST_EXIST);
586
        $this->assertEquals($json, $record->path);
587
    }
588
 
589
    /**
590
     * Test get_record method for creating persistent instance
591
     */
592
    public function test_get_record(): void {
593
        $persistent = (new core_testable_persistent(0, (object) [
594
            'idnumber' => '123',
595
            'sortorder' => 1,
596
        ]))->create();
597
 
598
        $another = core_testable_persistent::get_record(['id' => $persistent->get('id')]);
599
 
600
        // Assert we got back a persistent instance, and it matches original.
601
        $this->assertInstanceOf(core_testable_persistent::class, $another);
602
        $this->assertEquals($another->to_record(), $persistent->to_record());
603
    }
604
 
605
    /**
606
     * Test get_record method for creating persistent instance, ignoring a non-existing record
607
     */
608
    public function test_get_record_ignore_missing(): void {
609
        $persistent = core_testable_persistent::get_record(['id' => 42]);
610
        $this->assertFalse($persistent);
611
    }
612
 
613
    /**
614
     * Test get_record method for creating persistent instance, throws appropriate exception for non-existing record
615
     */
616
    public function test_get_record_must_exist(): void {
617
        $this->expectException(dml_missing_record_exception::class);
618
        $this->expectExceptionMessage('Can\'t find data record in database table phpunit_persistent.');
619
        core_testable_persistent::get_record(['id' => 42], MUST_EXIST);
620
    }
621
 
11 efrain 622
    public function test_record_exists(): void {
1 efrain 623
        global $DB;
624
        $this->assertFalse($DB->record_exists(core_testable_persistent::TABLE, array('idnumber' => 'abc')));
625
        $p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
626
        $p->create();
627
        $id = $p->get('id');
628
        $this->assertTrue(core_testable_persistent::record_exists($id));
629
        $this->assertTrue($DB->record_exists(core_testable_persistent::TABLE, array('idnumber' => 'abc')));
630
        $p->delete();
631
        $this->assertFalse(core_testable_persistent::record_exists($id));
632
    }
633
 
11 efrain 634
    public function test_get_sql_fields(): void {
1 efrain 635
        $expected = '' .
636
            'c.id AS prefix_id, ' .
637
            'c.shortname AS prefix_shortname, ' .
638
            'c.idnumber AS prefix_idnumber, ' .
639
            'c.description AS prefix_description, ' .
640
            'c.descriptionformat AS prefix_descriptionformat, ' .
641
            'c.parentid AS prefix_parentid, ' .
642
            'c.path AS prefix_path, ' .
643
            'c.sortorder AS prefix_sortorder, ' .
644
            'c.scaleid AS prefix_scaleid, ' .
645
            'c.timecreated AS prefix_timecreated, ' .
646
            'c.timemodified AS prefix_timemodified, ' .
647
            'c.usermodified AS prefix_usermodified';
648
        $this->assertEquals($expected, core_testable_persistent::get_sql_fields('c', 'prefix_'));
649
    }
650
 
11 efrain 651
    public function test_get_sql_fields_too_long(): void {
1 efrain 652
        $this->expectException(coding_exception::class);
653
        $this->expectExceptionMessageMatches('/The alias .+ exceeds 30 characters/');
654
        core_testable_persistent::get_sql_fields('c');
655
    }
656
 
657
    public function test_get(): void {
658
        $data = [
659
            'someint' => 123,
660
            'intnull' => null,
661
            'somefloat' => 33.44,
662
            'sometext' => 'Hello',
663
            'someraw' => '/dev/hello',
664
            'booltrue' => true,
665
            'boolfalse' => false,
666
        ];
667
        $p = new core_testable_second_persistent(0, (object)$data);
668
        $p->create();
669
 
670
        $this->assertSame($data['intnull'], $p->get('intnull'));
671
        $this->assertSame($data['someint'], $p->get('someint'));
672
        $this->assertIsFloat($p->get('somefloat')); // Avoid === comparisons on floats, verify type and value separated.
673
        $this->assertEqualsWithDelta($data['somefloat'], $p->get('somefloat'), 0.00001);
674
        $this->assertSame($data['sometext'], $p->get('sometext'));
675
        $this->assertSame($data['someraw'], $p->get('someraw'));
676
        $this->assertSame($data['booltrue'], $p->get('booltrue'));
677
        $this->assertSame($data['boolfalse'], $p->get('boolfalse'));
678
 
679
        // Ensure that types are correct after reloading data from database.
680
        $p->read();
681
 
682
        $this->assertSame($data['someint'], $p->get('someint'));
683
        $this->assertSame($data['intnull'], $p->get('intnull'));
684
        $this->assertIsFloat($p->get('somefloat')); // Avoid === comparisons on floats, verify type and value separated.
685
        $this->assertEqualsWithDelta($data['somefloat'], $p->get('somefloat'), 0.00001);
686
        $this->assertSame($data['sometext'], $p->get('sometext'));
687
        $this->assertSame($data['someraw'], $p->get('someraw'));
688
        $this->assertSame($data['booltrue'], $p->get('booltrue'));
689
        $this->assertSame($data['boolfalse'], $p->get('boolfalse'));
690
    }
691
}
692
 
693
/**
694
 * Example persistent class.
695
 *
696
 * @package    core
697
 * @copyright  2015 Frédéric Massart - FMCorz.net
698
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
699
 */
700
class core_testable_persistent extends persistent {
701
 
702
    const TABLE = 'phpunit_persistent';
703
 
704
    /** @var bool before validate status. */
705
    public ?bool $beforevalidate;
706
 
707
    /** @var bool before create status. */
708
    public ?bool $beforecreate;
709
 
710
    /** @var bool before update status. */
711
    public ?bool $beforeupdate;
712
 
713
    /** @var bool before delete status. */
714
    public ?bool $beforedelete;
715
 
716
    /** @var bool after create status. */
717
    public ?bool $aftercreate;
718
 
719
    /** @var bool after update status. */
720
    public ?bool $afterupdate;
721
 
722
    /** @var bool after delete status. */
723
    public ?bool $afterdelete;
724
 
725
    protected static function define_properties() {
726
        return array(
727
            'shortname' => array(
728
                'type' => PARAM_TEXT,
729
                'default' => ''
730
            ),
731
            'idnumber' => array(
732
                'type' => PARAM_TEXT,
733
            ),
734
            'description' => array(
735
                'type' => PARAM_TEXT,
736
                'default' => ''
737
            ),
738
            'descriptionformat' => array(
739
                'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN),
740
                'type' => PARAM_INT,
741
                'default' => FORMAT_HTML
742
            ),
743
            'parentid' => array(
744
                'type' => PARAM_INT,
745
                'default' => 0
746
            ),
747
            'path' => array(
748
                'type' => PARAM_RAW,
749
                'default' => ''
750
            ),
751
            'sortorder' => array(
752
                'type' => PARAM_INT,
753
                'message' => new lang_string('invalidrequest', 'error')
754
            ),
755
            'scaleid' => array(
756
                'type' => PARAM_INT,
757
                'default' => null,
758
                'null' => NULL_ALLOWED
759
            )
760
        );
761
    }
762
 
763
    protected function before_validate() {
764
        $this->beforevalidate = true;
765
    }
766
 
767
    protected function before_create() {
768
        $this->beforecreate = true;
769
    }
770
 
771
    protected function before_update() {
772
        $this->beforeupdate = true;
773
    }
774
 
775
    protected function before_delete() {
776
        $this->beforedelete = true;
777
    }
778
 
779
    protected function after_create() {
780
        $this->aftercreate = true;
781
    }
782
 
783
    protected function after_update($result) {
784
        $this->afterupdate = true;
785
    }
786
 
787
    protected function after_delete($result) {
788
        $this->afterdelete = true;
789
    }
790
 
791
    protected function get_path() {
792
        $value = $this->raw_get('path');
793
        if (!empty($value)) {
794
            $value = json_decode($value);
795
        }
796
        return $value;
797
    }
798
 
799
    protected function set_path($value) {
800
        if (!empty($value)) {
801
            $value = json_encode($value);
802
        }
803
        $this->raw_set('path', $value);
804
    }
805
 
806
    protected function validate_sortorder($value) {
807
        if ($value == 10) {
808
            return new lang_string('invalidkey', 'error');
809
        }
810
        return true;
811
    }
812
 
813
}
814
 
815
/**
816
 * Example persistent class to test types.
817
 *
818
 * @package    core
819
 * @copyright  2021 David Matamoros <davidmc@moodle.com>
820
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
821
 */
822
class core_testable_second_persistent extends persistent {
823
 
824
    /** Table name for the persistent. */
825
    const TABLE = 'phpunit_second_persistent';
826
 
827
    /**
828
     * Return the list of properties.
829
     *
830
     * @return array
831
     */
832
    protected static function define_properties(): array {
833
        return [
834
            'someint' => [
835
                'type' => PARAM_INT,
836
            ],
837
            'intnull' => [
838
                'type' => PARAM_INT,
839
                'null' => NULL_ALLOWED,
840
                'default' => null,
841
            ],
842
            'somefloat' => [
843
                'type' => PARAM_FLOAT,
844
            ],
845
            'sometext' => [
846
                'type' => PARAM_TEXT,
847
                'default' => ''
848
            ],
849
            'someraw' => [
850
                'type' => PARAM_RAW,
851
                'default' => ''
852
            ],
853
            'booltrue' => [
854
                'type' => PARAM_BOOL,
855
            ],
856
            'boolfalse' => [
857
                'type' => PARAM_BOOL,
858
            ]
859
        ];
860
    }
861
}