Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 11
Línea 95... Línea 95...
95
        }
95
        }
Línea 96... Línea 96...
96
 
96
 
97
        $dbman->create_table($table);
97
        $dbman->create_table($table);
Línea 98... Línea 98...
98
    }
98
    }
99
 
99
 
100
    public function test_properties_definition() {
100
    public function test_properties_definition(): void {
101
        $expected = array(
101
        $expected = array(
102
            'shortname' => array(
102
            'shortname' => array(
103
                'type' => PARAM_TEXT,
103
                'type' => PARAM_TEXT,
Línea 201... Línea 201...
201
        $this->expectException(dml_missing_record_exception::class);
201
        $this->expectException(dml_missing_record_exception::class);
202
        $this->expectExceptionMessage('Can\'t find data record in database table phpunit_persistent.');
202
        $this->expectExceptionMessage('Can\'t find data record in database table phpunit_persistent.');
203
        new core_testable_persistent(42);
203
        new core_testable_persistent(42);
204
    }
204
    }
Línea 205... Línea 205...
205
 
205
 
206
    public function test_to_record() {
206
    public function test_to_record(): void {
207
        $p = new core_testable_persistent();
207
        $p = new core_testable_persistent();
208
        $expected = (object) array(
208
        $expected = (object) array(
209
            'shortname' => '',
209
            'shortname' => '',
210
            'idnumber' => null,
210
            'idnumber' => null,
Línea 220... Línea 220...
220
            'scaleid' => null,
220
            'scaleid' => null,
221
        );
221
        );
222
        $this->assertEquals($expected, $p->to_record());
222
        $this->assertEquals($expected, $p->to_record());
223
    }
223
    }
Línea 224... Línea 224...
224
 
224
 
225
    public function test_from_record() {
225
    public function test_from_record(): void {
226
        $p = new core_testable_persistent();
226
        $p = new core_testable_persistent();
227
        $data = (object) array(
227
        $data = (object) array(
228
            'shortname' => 'ddd',
228
            'shortname' => 'ddd',
229
            'idnumber' => 'abc',
229
            'idnumber' => 'abc',
Línea 240... Línea 240...
240
        );
240
        );
241
        $p->from_record($data);
241
        $p->from_record($data);
242
        $this->assertEquals($data, $p->to_record());
242
        $this->assertEquals($data, $p->to_record());
243
    }
243
    }
Línea 244... Línea 244...
244
 
244
 
245
    public function test_from_record_invalid_param() {
245
    public function test_from_record_invalid_param(): void {
246
        $p = new core_testable_persistent();
246
        $p = new core_testable_persistent();
247
        $data = (object) array(
247
        $data = (object) array(
248
            'shortname' => 'ddd',
248
            'shortname' => 'ddd',
249
            'idnumber' => 'abc',
249
            'idnumber' => 'abc',
Línea 265... Línea 265...
265
        // Previous call should succeed, assert we get back all data except invalid param.
265
        // Previous call should succeed, assert we get back all data except invalid param.
266
        unset($data->invalidparam);
266
        unset($data->invalidparam);
267
        $this->assertEquals($data, $p->to_record());
267
        $this->assertEquals($data, $p->to_record());
268
    }
268
    }
Línea 269... Línea 269...
269
 
269
 
270
    public function test_validate() {
270
    public function test_validate(): void {
271
        $data = (object) array(
271
        $data = (object) array(
272
            'idnumber' => 'abc',
272
            'idnumber' => 'abc',
273
            'sortorder' => 0
273
            'sortorder' => 0
274
        );
274
        );
Línea 286... Línea 286...
286
        $this->assertEquals($expected, $p->validate());
286
        $this->assertEquals($expected, $p->validate());
287
        $this->assertFalse($p->is_valid());
287
        $this->assertFalse($p->is_valid());
288
        $this->assertEquals($expected, $p->get_errors());
288
        $this->assertEquals($expected, $p->get_errors());
289
    }
289
    }
Línea 290... Línea 290...
290
 
290
 
291
    public function test_validation_required() {
291
    public function test_validation_required(): void {
292
        $data = (object) array(
292
        $data = (object) array(
293
            'idnumber' => 'abc'
293
            'idnumber' => 'abc'
294
        );
294
        );
295
        $p = new core_testable_persistent(0, $data);
295
        $p = new core_testable_persistent(0, $data);
Línea 298... Línea 298...
298
        );
298
        );
299
        $this->assertFalse($p->is_valid());
299
        $this->assertFalse($p->is_valid());
300
        $this->assertEquals($expected, $p->get_errors());
300
        $this->assertEquals($expected, $p->get_errors());
301
    }
301
    }
Línea 302... Línea 302...
302
 
302
 
303
    public function test_validation_custom() {
303
    public function test_validation_custom(): void {
304
        $data = (object) array(
304
        $data = (object) array(
305
            'idnumber' => 'abc',
305
            'idnumber' => 'abc',
306
            'sortorder' => 10,
306
            'sortorder' => 10,
307
        );
307
        );
Línea 311... Línea 311...
311
        );
311
        );
312
        $this->assertFalse($p->is_valid());
312
        $this->assertFalse($p->is_valid());
313
        $this->assertEquals($expected, $p->get_errors());
313
        $this->assertEquals($expected, $p->get_errors());
314
    }
314
    }
Línea 315... Línea 315...
315
 
315
 
316
    public function test_validation_custom_message() {
316
    public function test_validation_custom_message(): void {
317
        $data = (object) array(
317
        $data = (object) array(
318
            'idnumber' => 'abc',
318
            'idnumber' => 'abc',
319
            'sortorder' => 'abc',
319
            'sortorder' => 'abc',
320
        );
320
        );
Línea 324... Línea 324...
324
        );
324
        );
325
        $this->assertFalse($p->is_valid());
325
        $this->assertFalse($p->is_valid());
326
        $this->assertEquals($expected, $p->get_errors());
326
        $this->assertEquals($expected, $p->get_errors());
327
    }
327
    }
Línea 328... Línea 328...
328
 
328
 
329
    public function test_validation_choices() {
329
    public function test_validation_choices(): void {
330
        $data = (object) array(
330
        $data = (object) array(
331
            'idnumber' => 'abc',
331
            'idnumber' => 'abc',
332
            'sortorder' => 0,
332
            'sortorder' => 0,
333
            'descriptionformat' => -100
333
            'descriptionformat' => -100
Línea 338... Línea 338...
338
        );
338
        );
339
        $this->assertFalse($p->is_valid());
339
        $this->assertFalse($p->is_valid());
340
        $this->assertEquals($expected, $p->get_errors());
340
        $this->assertEquals($expected, $p->get_errors());
341
    }
341
    }
Línea 342... Línea 342...
342
 
342
 
343
    public function test_validation_type() {
343
    public function test_validation_type(): void {
344
        $data = (object) array(
344
        $data = (object) array(
345
            'idnumber' => 'abc',
345
            'idnumber' => 'abc',
346
            'sortorder' => 'NaN'
346
            'sortorder' => 'NaN'
347
        );
347
        );
348
        $p = new core_testable_persistent(0, $data);
348
        $p = new core_testable_persistent(0, $data);
349
        $this->assertFalse($p->is_valid());
349
        $this->assertFalse($p->is_valid());
350
        $this->assertArrayHasKey('sortorder', $p->get_errors());
350
        $this->assertArrayHasKey('sortorder', $p->get_errors());
Línea 351... Línea 351...
351
    }
351
    }
352
 
352
 
353
    public function test_validation_null() {
353
    public function test_validation_null(): void {
354
        $data = (object) array(
354
        $data = (object) array(
355
            'idnumber' => null,
355
            'idnumber' => null,
356
            'sortorder' => 0,
356
            'sortorder' => 0,
Línea 367... Línea 367...
367
        $p->set('scaleid', null);
367
        $p->set('scaleid', null);
368
        $this->assertTrue($p->is_valid());
368
        $this->assertTrue($p->is_valid());
369
        $this->assertArrayNotHasKey('scaleid', $p->get_errors());
369
        $this->assertArrayNotHasKey('scaleid', $p->get_errors());
370
    }
370
    }
Línea 371... Línea 371...
371
 
371
 
372
    public function test_create() {
372
    public function test_create(): void {
373
        global $DB;
373
        global $DB;
374
        $p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
374
        $p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
375
        $this->assertFalse(isset($p->beforecreate));
375
        $this->assertFalse(isset($p->beforecreate));
376
        $this->assertFalse(isset($p->aftercreate));
376
        $this->assertFalse(isset($p->aftercreate));
Línea 383... Línea 383...
383
        $this->assertEquals($expected->idnumber, $record->idnumber);
383
        $this->assertEquals($expected->idnumber, $record->idnumber);
384
        $this->assertEquals($expected->id, $record->id);
384
        $this->assertEquals($expected->id, $record->id);
385
        $this->assertTrue($p->is_valid()); // Should always be valid after a create.
385
        $this->assertTrue($p->is_valid()); // Should always be valid after a create.
386
    }
386
    }
Línea 387... Línea 387...
387
 
387
 
388
    public function test_update() {
388
    public function test_update(): void {
389
        global $DB;
389
        global $DB;
390
        $p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
390
        $p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
391
        $p->create();
391
        $p->create();
392
        $id = $p->get('id');
392
        $id = $p->get('id');
Línea 427... Línea 427...
427
        $record = $DB->get_record(core_testable_persistent::TABLE, ['id' => $persistent->get('id')], '*', MUST_EXIST);
427
        $record = $DB->get_record(core_testable_persistent::TABLE, ['id' => $persistent->get('id')], '*', MUST_EXIST);
428
        $this->assertEquals('test2', $record->idnumber);
428
        $this->assertEquals('test2', $record->idnumber);
429
        $this->assertEquals(1, $record->sortorder);
429
        $this->assertEquals(1, $record->sortorder);
430
    }
430
    }
Línea 431... Línea 431...
431
 
431
 
432
    public function test_save() {
432
    public function test_save(): void {
433
        global $DB;
433
        global $DB;
434
        $p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
434
        $p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
435
        $this->assertFalse(isset($p->beforecreate));
435
        $this->assertFalse(isset($p->beforecreate));
436
        $this->assertFalse(isset($p->aftercreate));
436
        $this->assertFalse(isset($p->aftercreate));
Línea 529... Línea 529...
529
        $this->expectException(coding_exception::class);
529
        $this->expectException(coding_exception::class);
530
        $this->expectExceptionMessage('Unexpected property \'invalid\' requested');
530
        $this->expectExceptionMessage('Unexpected property \'invalid\' requested');
531
        $persistent->set_many(['invalid' => 'stuff']);
531
        $persistent->set_many(['invalid' => 'stuff']);
532
    }
532
    }
Línea 533... Línea 533...
533
 
533
 
534
    public function test_read() {
534
    public function test_read(): void {
535
        $p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
535
        $p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
536
        $p->create();
536
        $p->create();
537
        unset($p->beforevalidate);
537
        unset($p->beforevalidate);
538
        unset($p->beforecreate);
538
        unset($p->beforecreate);
Línea 545... Línea 545...
545
        $p3->set('id', $p->get('id'));
545
        $p3->set('id', $p->get('id'));
546
        $p3->read();
546
        $p3->read();
547
        $this->assertEquals($p, $p3);
547
        $this->assertEquals($p, $p3);
548
    }
548
    }
Línea 549... Línea 549...
549
 
549
 
550
    public function test_delete() {
550
    public function test_delete(): void {
Línea 551... Línea 551...
551
        global $DB;
551
        global $DB;
552
 
552
 
553
        $p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
553
        $p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
Línea 562... Línea 562...
562
        $this->assertEquals(0, $p->get('id'));
562
        $this->assertEquals(0, $p->get('id'));
563
        $this->assertEquals(true, $p->beforedelete);
563
        $this->assertEquals(true, $p->beforedelete);
564
        $this->assertEquals(true, $p->afterdelete);
564
        $this->assertEquals(true, $p->afterdelete);
565
    }
565
    }
Línea 566... Línea 566...
566
 
566
 
567
    public function test_has_property() {
567
    public function test_has_property(): void {
568
        $this->assertFalse(core_testable_persistent::has_property('unknown'));
568
        $this->assertFalse(core_testable_persistent::has_property('unknown'));
569
        $this->assertTrue(core_testable_persistent::has_property('idnumber'));
569
        $this->assertTrue(core_testable_persistent::has_property('idnumber'));
Línea 570... Línea 570...
570
    }
570
    }
571
 
571
 
Línea 572... Línea 572...
572
    public function test_custom_setter_getter() {
572
    public function test_custom_setter_getter(): void {
573
        global $DB;
573
        global $DB;
Línea 616... Línea 616...
616
        $this->expectException(dml_missing_record_exception::class);
616
        $this->expectException(dml_missing_record_exception::class);
617
        $this->expectExceptionMessage('Can\'t find data record in database table phpunit_persistent.');
617
        $this->expectExceptionMessage('Can\'t find data record in database table phpunit_persistent.');
618
        core_testable_persistent::get_record(['id' => 42], MUST_EXIST);
618
        core_testable_persistent::get_record(['id' => 42], MUST_EXIST);
619
    }
619
    }
Línea 620... Línea 620...
620
 
620
 
621
    public function test_record_exists() {
621
    public function test_record_exists(): void {
622
        global $DB;
622
        global $DB;
623
        $this->assertFalse($DB->record_exists(core_testable_persistent::TABLE, array('idnumber' => 'abc')));
623
        $this->assertFalse($DB->record_exists(core_testable_persistent::TABLE, array('idnumber' => 'abc')));
624
        $p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
624
        $p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
625
        $p->create();
625
        $p->create();
Línea 628... Línea 628...
628
        $this->assertTrue($DB->record_exists(core_testable_persistent::TABLE, array('idnumber' => 'abc')));
628
        $this->assertTrue($DB->record_exists(core_testable_persistent::TABLE, array('idnumber' => 'abc')));
629
        $p->delete();
629
        $p->delete();
630
        $this->assertFalse(core_testable_persistent::record_exists($id));
630
        $this->assertFalse(core_testable_persistent::record_exists($id));
631
    }
631
    }
Línea 632... Línea 632...
632
 
632
 
633
    public function test_get_sql_fields() {
633
    public function test_get_sql_fields(): void {
634
        $expected = '' .
634
        $expected = '' .
635
            'c.id AS prefix_id, ' .
635
            'c.id AS prefix_id, ' .
636
            'c.shortname AS prefix_shortname, ' .
636
            'c.shortname AS prefix_shortname, ' .
637
            'c.idnumber AS prefix_idnumber, ' .
637
            'c.idnumber AS prefix_idnumber, ' .
Línea 645... Línea 645...
645
            'c.timemodified AS prefix_timemodified, ' .
645
            'c.timemodified AS prefix_timemodified, ' .
646
            'c.usermodified AS prefix_usermodified';
646
            'c.usermodified AS prefix_usermodified';
647
        $this->assertEquals($expected, core_testable_persistent::get_sql_fields('c', 'prefix_'));
647
        $this->assertEquals($expected, core_testable_persistent::get_sql_fields('c', 'prefix_'));
648
    }
648
    }
Línea 649... Línea 649...
649
 
649
 
650
    public function test_get_sql_fields_too_long() {
650
    public function test_get_sql_fields_too_long(): void {
651
        $this->expectException(coding_exception::class);
651
        $this->expectException(coding_exception::class);
652
        $this->expectExceptionMessageMatches('/The alias .+ exceeds 30 characters/');
652
        $this->expectExceptionMessageMatches('/The alias .+ exceeds 30 characters/');
653
        core_testable_persistent::get_sql_fields('c');
653
        core_testable_persistent::get_sql_fields('c');