Proyectos de Subversion Moodle

Rev

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

Rev 11 Rev 1441
Línea 345... Línea 345...
345
        $task = manager::get_next_adhoc_task($timestart);
345
        $task = manager::get_next_adhoc_task($timestart);
346
        $this->assertNull($task);
346
        $this->assertNull($task);
Línea 347... Línea 347...
347
 
347
 
348
        $this->resetAfterTest();
348
        $this->resetAfterTest();
-
 
349
    }
-
 
350
 
-
 
351
    /**
-
 
352
     * Test verifying \core\task\manager behaviour for scheduled tasks when dealing with deprecated plugin types.
-
 
353
     *
-
 
354
     * This only verifies that existing tasks will not be listed, or returned for execution via existing APIs, like:
-
 
355
     * - {@see \core\task\manager::get_all_scheduled_tasks}
-
 
356
     * - {@see \core\task\manager::get_next_scheduled_task}
-
 
357
     *
-
 
358
     * I.e. Nothing prevents task->execute() from running if called directly.
-
 
359
     *
-
 
360
     * @return void
-
 
361
     */
-
 
362
    public function test_scheduled_tasks_deprecated_plugintype(): void {
-
 
363
        $this->resetAfterTest();
-
 
364
        global $DB, $CFG;
-
 
365
 
-
 
366
        $fakepluginroot = $CFG->libdir . '/tests/fixtures/fakeplugins/fake/fullfeatured';
-
 
367
        require_once($fakepluginroot . '/classes/plugininfo/fullsubtype.php');
-
 
368
        require_once($fakepluginroot . '/classes/plugininfo/fulldeprecatedsubtype.php');
-
 
369
        require_once($fakepluginroot . '/fullsubtype/example/classes/task/scheduled_test.php');
-
 
370
        require_once($fakepluginroot . '/fulldeprecatedsubtype/test/classes/task/scheduled_test.php');
-
 
371
 
-
 
372
        // Inject stub plugininfo instances into a stub plugin manager, then inject that into the static cache via reflection.
-
 
373
        // When the manager code calls core_plugin_manager::instance(), it'll get back the stub.
-
 
374
        $stubavailableplugininfo = $this->createStub(\fake_fullfeatured\plugininfo\fullsubtype::class);
-
 
375
        $stubavailableplugininfo->method('is_deprecated')->willReturn(false);
-
 
376
        $stubavailableplugininfo->component = "fullsubtype_example";
-
 
377
        $stubdeprecatedplugininfo = $this->createStub(\fake_fullfeatured\plugininfo\fulldeprecatedsubtype::class);
-
 
378
        $stubdeprecatedplugininfo->method('is_deprecated')->willReturn(true);
-
 
379
        $stubdeprecatedplugininfo->component = "fulldeprecatedsubtype_test";
-
 
380
 
-
 
381
        $stubpluginman = $this->createStub(\core_plugin_manager::class);
-
 
382
        $stubpluginman
-
 
383
            ->method('get_plugin_info')
-
 
384
            ->will($this->returnValueMap([
-
 
385
                ['fullsubtype_example', $stubavailableplugininfo],
-
 
386
                ['fulldeprecatedsubtype_test', $stubdeprecatedplugininfo],
-
 
387
            ]));
-
 
388
 
-
 
389
        $pluginman = new \ReflectionClass(\core_plugin_manager::class);
-
 
390
        $pluginman->setStaticPropertyValue('singletoninstance', $stubpluginman);
-
 
391
 
-
 
392
        $DB->delete_records('task_scheduled');
-
 
393
 
-
 
394
        // Non-deprecated plugin type: is listed and is returned during scheduling.
-
 
395
        $scheduledtask = new \fullsubtype_example\task\scheduled_test();
-
 
396
        $DB->insert_record('task_scheduled', \core\task\manager::record_from_scheduled_task($scheduledtask));
-
 
397
        $records = $DB->get_records('task_scheduled');
-
 
398
        $this->assertCount(1, $records);
-
 
399
 
-
 
400
        $this->assertInstanceOf(
-
 
401
            \fullsubtype_example\task\scheduled_test::class,
-
 
402
            \core\task\manager::get_all_scheduled_tasks()[0]
-
 
403
        );
-
 
404
        $now = time();
-
 
405
        $task = \core\task\manager::get_next_scheduled_task($now);
-
 
406
        $this->assertInstanceOf(\fullsubtype_example\task\scheduled_test::class, $task);
-
 
407
        manager::scheduled_task_complete($task);
-
 
408
 
-
 
409
        // Deprecated plugin type: isn't listed and isn't returned during scheduling.
-
 
410
        $DB->delete_records('task_scheduled');
-
 
411
        $scheduledtask = new \fulldeprecatedsubtype_test\task\scheduled_test();
-
 
412
        $DB->insert_record('task_scheduled', \core\task\manager::record_from_scheduled_task($scheduledtask));
-
 
413
        $records = $DB->get_records('task_scheduled');
-
 
414
        $this->assertCount(1, $records);
-
 
415
 
-
 
416
        $this->assertEmpty(\core\task\manager::get_all_scheduled_tasks());
-
 
417
        $this->assertNull(\core\task\manager::get_next_scheduled_task($now));
-
 
418
 
-
 
419
        // Task can still be executed directly.
-
 
420
        $this->expectExceptionMessage('task->execute() called');
-
 
421
        $scheduledtask->execute();
-
 
422
    }
-
 
423
 
-
 
424
    /**
-
 
425
     * Test verifying \core\task\manager behaviour for adhoc tasks when dealing with deprecated plugin types.
-
 
426
     *
-
 
427
     * This only verifies that new tasks cannot be queued via:
-
 
428
     * - {@see \core\task\manager::queue_adhoc_task}
-
 
429
     * - {@see \core\task\manager::get_next_adhoc_task()}
-
 
430
     *
-
 
431
     * I.e. Nothing prevents task->execute() from running if called directly.
-
 
432
     *
-
 
433
     * @return void
-
 
434
     */
-
 
435
    public function test_queue_adhoc_task_deprecated_plugintype(): void {
-
 
436
        $this->resetAfterTest();
-
 
437
        global $DB, $CFG;
-
 
438
 
-
 
439
        $fakepluginroot = $CFG->libdir . '/tests/fixtures/fakeplugins/fake/fullfeatured';
-
 
440
        require_once($fakepluginroot . '/classes/plugininfo/fullsubtype.php');
-
 
441
        require_once($fakepluginroot . '/classes/plugininfo/fulldeprecatedsubtype.php');
-
 
442
        require_once($fakepluginroot . '/classes/plugininfo/fulldeletedsubtype.php');
-
 
443
        require_once($fakepluginroot . '/fullsubtype/example/classes/task/adhoc_test.php');
-
 
444
        require_once($fakepluginroot . '/fulldeprecatedsubtype/test/classes/task/adhoc_test.php');
-
 
445
        require_once($fakepluginroot . '/fulldeletedsubtype/demo/classes/task/adhoc_test.php');
-
 
446
 
-
 
447
        // Inject stub plugininfo instances into a stub plugin manager, then inject that into the static cache via reflection.
-
 
448
        // When the manager code calls core_plugin_manager::instance(), it'll get back the stub.
-
 
449
        $stubavailableplugininfo = $this->createStub(\fake_fullfeatured\plugininfo\fullsubtype::class);
-
 
450
        $stubavailableplugininfo->method('is_deprecated')->willReturn(false);
-
 
451
        $stubavailableplugininfo->method('is_deleted')->willReturn(false);
-
 
452
        $stubavailableplugininfo->component = "fullsubtype_example";
-
 
453
        $stubdeprecatedplugininfo = $this->createStub(\fake_fullfeatured\plugininfo\fulldeprecatedsubtype::class);
-
 
454
        $stubdeprecatedplugininfo->method('is_deprecated')->willReturn(true);
-
 
455
        $stubdeprecatedplugininfo->method('is_deleted')->willReturn(false);
-
 
456
        $stubdeprecatedplugininfo->component = "fulldeprecatedsubtype_test";
-
 
457
        $stubdeletedplugininfo = $this->createStub(\fake_fullfeatured\plugininfo\fulldeletedsubtype::class);
-
 
458
        $stubdeletedplugininfo->method('is_deprecated')->willReturn(false);
-
 
459
        $stubdeletedplugininfo->method('is_deleted')->willReturn(true);
-
 
460
        $stubdeletedplugininfo->component = "fulldeletedsubtype_demo";
-
 
461
        $stubpluginman = $this->createStub(\core_plugin_manager::class);
-
 
462
        $stubpluginman->method('get_plugin_info')
-
 
463
            ->will($this->returnValueMap([
-
 
464
                ['fullsubtype_example', $stubavailableplugininfo],
-
 
465
                ['fulldeprecatedsubtype_test', $stubdeprecatedplugininfo],
-
 
466
                ['fulldeletedsubtype_demo', $stubdeletedplugininfo],
-
 
467
            ]));
-
 
468
        $pluginmanrc = new \ReflectionClass(\core_plugin_manager::class);
-
 
469
        $pluginmanrc->setStaticPropertyValue('singletoninstance', $stubpluginman);
-
 
470
 
-
 
471
        $task1 = new \fullsubtype_example\task\adhoc_test(); // Available plugin type.
-
 
472
        $task2 = new \fulldeprecatedsubtype_test\task\adhoc_test(); // Deprecated plugin type.
-
 
473
        $task3 = new \fulldeletedsubtype_demo\task\adhoc_test(); // Deleted plugin type.
-
 
474
 
-
 
475
        $DB->delete_records('task_adhoc');
-
 
476
 
-
 
477
        // Task from a non-deprecated plugin type can be queued.
-
 
478
        $this->assertIsInt(manager::queue_adhoc_task($task1));
-
 
479
        $now = time();
-
 
480
        $classname = get_class($task1);
-
 
481
        $taskfromqueue = manager::get_next_adhoc_task($now, true, $classname);
-
 
482
        $this->assertNotNull($taskfromqueue);
-
 
483
        $taskfromqueue->execute();
-
 
484
        manager::adhoc_task_complete($taskfromqueue);
-
 
485
 
-
 
486
        // Task from a deprecated plugin type cannot be queued.
-
 
487
        $this->assertTrue(\core_plugin_manager::instance()->get_plugin_info('fulldeprecatedsubtype_test')->is_deprecated());
-
 
488
        $this->assertFalse(manager::queue_adhoc_task($task2));
-
 
489
        $classname = get_class($task2);
-
 
490
        $this->assertNull(manager::get_next_adhoc_task($now, true, $classname));
-
 
491
 
-
 
492
        // Task from a deleted plugin type cannot be queued.
-
 
493
        $this->assertTrue(\core_plugin_manager::instance()->get_plugin_info('fulldeletedsubtype_demo')->is_deleted());
-
 
494
        $this->assertFalse(manager::queue_adhoc_task($task3));
-
 
495
        $classname = get_class($task3);
-
 
496
        $this->assertNull(manager::get_next_adhoc_task($now, true, $classname));
-
 
497
    }
-
 
498
 
-
 
499
    /**
-
 
500
     * Test verifying \core\task\manager can still return and run adhoc tasks queued prior to plugin deprecation.
-
 
501
     *
-
 
502
     *  This only verifies that existing tasks can be fetched and run via:
-
 
503
     *  - {@see \core\task\manager::get_next_adhoc_task()}
-
 
504
     *
-
 
505
     * @return void
-
 
506
     */
-
 
507
    public function test_run_existing_adhoc_task_deprecated_plugintype(): void {
-
 
508
        $this->resetAfterTest();
-
 
509
        global $DB, $CFG;
-
 
510
 
-
 
511
        $fakepluginroot = $CFG->libdir . '/tests/fixtures/fakeplugins/fake/fullfeatured';
-
 
512
        require_once($fakepluginroot . '/classes/plugininfo/fullsubtype.php');
-
 
513
        require_once($fakepluginroot . '/fullsubtype/example/classes/task/adhoc_test.php');
-
 
514
 
-
 
515
        // Inject stub plugininfo instances into a stub plugin manager, then inject that into the static cache via reflection.
-
 
516
        // When the task code calls core_plugin_manager::instance(), it'll get back the stub.
-
 
517
        $stubavailableplugininfo = $this->createStub(\fake_fullfeatured\plugininfo\fullsubtype::class);
-
 
518
        $stubavailableplugininfo->method('is_deprecated')->willReturn(false);
-
 
519
        $stubavailableplugininfo->component = "fullsubtype_example";
-
 
520
        $stubpluginman = $this->createStub(\core_plugin_manager::class);
-
 
521
        $stubpluginman
-
 
522
            ->method('get_plugin_info')
-
 
523
            ->will($this->returnValueMap([
-
 
524
                ['fullsubtype_example', $stubavailableplugininfo],
-
 
525
            ]));
-
 
526
        $pluginmanrc = new \ReflectionClass(\core_plugin_manager::class);
-
 
527
        $pluginmanrc->setStaticPropertyValue('singletoninstance', $stubpluginman);
-
 
528
 
-
 
529
        $task1 = new \fullsubtype_example\task\adhoc_test(); // An available plugin.
-
 
530
 
-
 
531
        $DB->delete_records('task_adhoc');
-
 
532
 
-
 
533
        // Queue the task for the available plugin.
-
 
534
        $this->assertIsInt(manager::queue_adhoc_task($task1));
-
 
535
        $this->assertEquals(1, $DB->count_records('task_adhoc'));
-
 
536
 
-
 
537
        // Now, deprecate the plugin type by redefining the stubs and reinjecting into the stub plugin manager.
-
 
538
        $stubdeprecatedplugininfo = $this->createStub(\fake_fullfeatured\plugininfo\fullsubtype::class);
-
 
539
        $stubdeprecatedplugininfo->method('is_deprecated')->willReturn(true);
-
 
540
        $stubdeprecatedplugininfo->component = "fullsubtype_example";
-
 
541
        $stubpluginman = $this->createStub(\core_plugin_manager::class);
-
 
542
        $stubpluginman
-
 
543
            ->method('get_plugin_info')
-
 
544
            ->will($this->returnValueMap([
-
 
545
                ['fullsubtype_example', $stubdeprecatedplugininfo],
-
 
546
            ]));
-
 
547
        $pluginmanrc->setStaticPropertyValue('singletoninstance', $stubpluginman);
-
 
548
 
-
 
549
        // Assert prior-queued tasks can be fetched and run.
-
 
550
        $this->assertTrue(\core_plugin_manager::instance()->get_plugin_info('fullsubtype_example')->is_deprecated());
-
 
551
        $classname = get_class($task1);
-
 
552
        $now = time();
-
 
553
        $taskfromqueue = manager::get_next_adhoc_task($now, true, $classname);
-
 
554
        $this->assertNotNull($taskfromqueue);
-
 
555
        $taskfromqueue->execute();
-
 
556
        manager::adhoc_task_complete($taskfromqueue);
349
    }
557
    }