Proyectos de Subversion Moodle

Rev

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

Rev 1 Rev 11
Línea 30... Línea 30...
30
 */
30
 */
31
class base_test extends \advanced_testcase {
31
class base_test extends \advanced_testcase {
Línea 32... Línea 32...
32
 
32
 
Línea 33... Línea 33...
33
    const DEBUGGING_MSG = 'Events API using $handlers array has been deprecated in favour of Events 2 API, please use it instead.';
33
    const DEBUGGING_MSG = 'Events API using $handlers array has been deprecated in favour of Events 2 API, please use it instead.';
34
 
34
 
Línea 35... Línea 35...
35
    public function test_event_properties() {
35
    public function test_event_properties(): void {
36
        global $USER;
36
        global $USER;
Línea 85... Línea 85...
85
 
85
 
86
        $event3 = \core_tests\event\unittest_executed::create(array('contextid'=>$system->id, 'objectid'=>5, 'anonymous'=>true, 'other'=>array('sample'=>null, 'xx'=>10)));
86
        $event3 = \core_tests\event\unittest_executed::create(array('contextid'=>$system->id, 'objectid'=>5, 'anonymous'=>true, 'other'=>array('sample'=>null, 'xx'=>10)));
87
        $this->assertSame(1, $event3->anonymous);
87
        $this->assertSame(1, $event3->anonymous);
Línea 88... Línea 88...
88
    }
88
    }
89
 
89
 
90
    public function test_event_properties_guessing() {
90
    public function test_event_properties_guessing(): void {
Línea 91... Línea 91...
91
        global $USER;
91
        global $USER;
92
        $this->resetAfterTest();
92
        $this->resetAfterTest();
Línea 118... Línea 118...
118
        $this->assertSame(0, $event->courseid);
118
        $this->assertSame(0, $event->courseid);
119
        $this->assertSame($USER->id, $event->userid);
119
        $this->assertSame($USER->id, $event->userid);
120
        $this->assertSame($user->id, $event->relateduserid);
120
        $this->assertSame($user->id, $event->relateduserid);
121
    }
121
    }
Línea 122... Línea 122...
122
 
122
 
123
    public function test_observers_parsing() {
123
    public function test_observers_parsing(): void {
Línea 124... Línea 124...
124
        global $CFG;
124
        global $CFG;
125
 
125
 
126
        $observers = array(
126
        $observers = array(
Línea 285... Línea 285...
285
        $result = \core\event\manager::phpunit_replace_observers($observers);
285
        $result = \core\event\manager::phpunit_replace_observers($observers);
286
        $this->assertCount(0, $result);
286
        $this->assertCount(0, $result);
287
        $this->assertDebuggingCalled();
287
        $this->assertDebuggingCalled();
288
    }
288
    }
Línea 289... Línea 289...
289
 
289
 
290
    public function test_normal_dispatching() {
290
    public function test_normal_dispatching(): void {
291
        $observers = array(
291
        $observers = array(
292
            array(
292
            array(
293
                'eventname'   => '\core_tests\event\unittest_executed',
293
                'eventname'   => '\core_tests\event\unittest_executed',
294
                'callback'    => '\core_tests\event\unittest_observer::observe_one',
294
                'callback'    => '\core_tests\event\unittest_observer::observe_one',
Línea 321... Línea 321...
321
        $this->assertSame(
321
        $this->assertSame(
322
            array('observe_all-nesting-1', 'observe_one-1', 'observe_all-666', 'observe_one-666', 'observe_all-2', 'observe_one-2'),
322
            array('observe_all-nesting-1', 'observe_one-1', 'observe_all-666', 'observe_one-666', 'observe_all-2', 'observe_one-2'),
323
            \core_tests\event\unittest_observer::$info);
323
            \core_tests\event\unittest_observer::$info);
324
    }
324
    }
Línea 325... Línea 325...
325
 
325
 
326
    public function test_event_sink() {
326
    public function test_event_sink(): void {
327
        $sink = $this->redirectEvents();
327
        $sink = $this->redirectEvents();
328
        $event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
328
        $event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
329
        $event1->trigger();
329
        $event1->trigger();
330
        $this->assertSame(1, $sink->count());
330
        $this->assertSame(1, $sink->count());
Línea 354... Línea 354...
354
        $this->assertSame(1, $sink->count());
354
        $this->assertSame(1, $sink->count());
355
        $retult = $sink->get_events();
355
        $retult = $sink->get_events();
356
        $this->assertSame($event3, $retult[0]);
356
        $this->assertSame($event3, $retult[0]);
357
    }
357
    }
Línea 358... Línea 358...
358
 
358
 
359
    public function test_ignore_exceptions() {
359
    public function test_ignore_exceptions(): void {
Línea 360... Línea 360...
360
        $observers = array(
360
        $observers = array(
361
 
361
 
362
            array(
362
            array(
Línea 385... Línea 385...
385
        $this->assertSame(
385
        $this->assertSame(
386
            array('broken_observer-1', 'observe_one-1', 'broken_observer-2', 'observe_one-2'),
386
            array('broken_observer-1', 'observe_one-1', 'broken_observer-2', 'observe_one-2'),
387
            \core_tests\event\unittest_observer::$info);
387
            \core_tests\event\unittest_observer::$info);
388
    }
388
    }
Línea 389... Línea 389...
389
 
389
 
390
    public function test_external_buffer() {
390
    public function test_external_buffer(): void {
Línea 391... Línea 391...
391
        global $DB;
391
        global $DB;
Línea 392... Línea 392...
392
 
392
 
Línea 459... Línea 459...
459
        $this->assertSame(
459
        $this->assertSame(
460
            array('external_observer-1', 'observe_one-1', 'observe_one-2'),
460
            array('external_observer-1', 'observe_one-1', 'observe_one-2'),
461
            \core_tests\event\unittest_observer::$info);
461
            \core_tests\event\unittest_observer::$info);
462
    }
462
    }
Línea 463... Línea 463...
463
 
463
 
464
    public function test_rollback() {
464
    public function test_rollback(): void {
Línea 465... Línea 465...
465
        global $DB;
465
        global $DB;
466
 
466
 
Línea 512... Línea 512...
512
 
512
 
513
        \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)))->trigger();
513
        \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)))->trigger();
514
        $this->assertCount(1, \core_tests\event\unittest_observer::$event);
514
        $this->assertCount(1, \core_tests\event\unittest_observer::$event);
Línea 515... Línea 515...
515
    }
515
    }
516
 
516
 
Línea 517... Línea 517...
517
    public function test_forced_rollback() {
517
    public function test_forced_rollback(): void {
518
        global $DB;
518
        global $DB;
Línea 554... Línea 554...
554
 
554
 
555
        \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)))->trigger();
555
        \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)))->trigger();
556
        $this->assertCount(1, \core_tests\event\unittest_observer::$event);
556
        $this->assertCount(1, \core_tests\event\unittest_observer::$event);
Línea 557... Línea 557...
557
    }
557
    }
558
 
558
 
Línea 559... Línea 559...
559
    public function test_deprecated() {
559
    public function test_deprecated(): void {
Línea 560... Línea 560...
560
        global $DB;
560
        global $DB;
Línea 571... Línea 571...
571
        $this->assertDebuggingCalled('level property is deprecated, use edulevel property instead');
571
        $this->assertDebuggingCalled('level property is deprecated, use edulevel property instead');
Línea 572... Línea 572...
572
 
572
 
573
        $this->assertSame($event::LEVEL_TEACHING, $event->edulevel);
573
        $this->assertSame($event::LEVEL_TEACHING, $event->edulevel);
Línea 574... Línea 574...
574
    }
574
    }
575
 
575
 
Línea 576... Línea 576...
576
    public function test_legacy() {
576
    public function test_legacy(): void {
Línea 577... Línea 577...
577
        global $DB, $CFG;
577
        global $DB, $CFG;
Línea 621... Línea 621...
621
 
621
 
622
        $logs = $DB->get_records('log', array(), 'id ASC');
622
        $logs = $DB->get_records('log', array(), 'id ASC');
623
        $this->assertCount(0, $logs);
623
        $this->assertCount(0, $logs);
Línea 624... Línea 624...
624
    }
624
    }
625
 
625
 
626
    public function test_restore_event() {
626
    public function test_restore_event(): void {
Línea 627... Línea 627...
627
        $event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
627
        $event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
628
        $data1 = $event1->get_data();
628
        $data1 = $event1->get_data();
Línea 655... Línea 655...
655
        $this->assertInstanceOf(\core_tests\event\unittest_executed::class, $event5);
655
        $this->assertInstanceOf(\core_tests\event\unittest_executed::class, $event5);
656
        $this->assertDebuggingCalled();
656
        $this->assertDebuggingCalled();
Línea 657... Línea 657...
657
 
657
 
Línea 658... Línea 658...
658
    }
658
    }
659
 
659
 
Línea 660... Línea 660...
660
    public function test_trigger_problems() {
660
    public function test_trigger_problems(): void {
661
        $this->resetAfterTest(true);
661
        $this->resetAfterTest(true);
662
 
662
 
Línea 688... Línea 688...
688
        } catch (\moodle_exception $e) {
688
        } catch (\moodle_exception $e) {
689
            $this->assertInstanceOf(\coding_exception::class, $e);
689
            $this->assertInstanceOf(\coding_exception::class, $e);
690
        }
690
        }
691
    }
691
    }
Línea 692... Línea 692...
692
 
692
 
693
    public function test_bad_events() {
693
    public function test_bad_events(): void {
Línea 694... Línea 694...
694
        $this->resetAfterTest(true);
694
        $this->resetAfterTest(true);
695
 
695
 
696
        try {
696
        try {
Línea 747... Línea 747...
747
        $event = \core_tests\event\bad_event8::create(array('context'=>\context_system::instance()));
747
        $event = \core_tests\event\bad_event8::create(array('context'=>\context_system::instance()));
748
        $event->trigger();
748
        $event->trigger();
749
        $this->assertDebuggingCalled('Event property objectid must be set when objecttable is defined');
749
        $this->assertDebuggingCalled('Event property objectid must be set when objecttable is defined');
750
    }
750
    }
Línea 751... Línea 751...
751
 
751
 
752
    public function test_problematic_events() {
752
    public function test_problematic_events(): void {
Línea 753... Línea 753...
753
        $this->resetAfterTest(true);
753
        $this->resetAfterTest(true);
754
 
754
 
755
        $event1 = \core_tests\event\problematic_event1::create(array('context'=>\context_system::instance()));
755
        $event1 = \core_tests\event\problematic_event1::create(array('context'=>\context_system::instance()));
Línea 798... Línea 798...
798
        $this->assertDebuggingNotCalled();
798
        $this->assertDebuggingNotCalled();
799
        $event = \core_tests\event\problematic_event3::create(array());
799
        $event = \core_tests\event\problematic_event3::create(array());
800
        $this->assertDebuggingCalled();
800
        $this->assertDebuggingCalled();
801
    }
801
    }
Línea 802... Línea 802...
802
 
802
 
803
    public function test_record_snapshots() {
803
    public function test_record_snapshots(): void {
Línea 804... Línea 804...
804
        global $DB;
804
        global $DB;
Línea 805... Línea 805...
805
 
805
 
Línea 835... Línea 835...
835
        } catch (\moodle_exception $e) {
835
        } catch (\moodle_exception $e) {
836
            $this->assertInstanceOf(\coding_exception::class, $e);
836
            $this->assertInstanceOf(\coding_exception::class, $e);
837
        }
837
        }
838
    }
838
    }
Línea 839... Línea 839...
839
 
839
 
840
    public function test_get_name() {
840
    public function test_get_name(): void {
841
        $event = \core_tests\event\noname_event::create(array('other' => array('sample' => 1, 'xx' => 10)));
841
        $event = \core_tests\event\noname_event::create(array('other' => array('sample' => 1, 'xx' => 10)));
842
        $this->assertEquals("core_tests: noname event", $event->get_name());
842
        $this->assertEquals("core_tests: noname event", $event->get_name());
Línea 843... Línea 843...
843
    }
843
    }
844
 
844
 
Línea 845... Línea 845...
845
    public function test_iteration() {
845
    public function test_iteration(): void {
846
        $event = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
846
        $event = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
847
 
847
 
Línea 854... Línea 854...
854
    }
854
    }
Línea 855... Línea 855...
855
 
855
 
856
    /**
856
    /**
857
     * Test that all observer information is returned correctly.
857
     * Test that all observer information is returned correctly.
858
     */
858
     */
859
    public function test_get_all_observers() {
859
    public function test_get_all_observers(): void {
860
        // Retrieve all observers.
860
        // Retrieve all observers.
Línea 861... Línea 861...
861
        $observers = \core\event\manager::get_all_observers();
861
        $observers = \core\event\manager::get_all_observers();
862
 
862
 
Línea 882... Línea 882...
882
 
882
 
883
    /**
883
    /**
884
     * Test formatting of the get_explanation method.
884
     * Test formatting of the get_explanation method.
885
     * This formats the information from an events class docblock.
885
     * This formats the information from an events class docblock.
886
     */
886
     */
887
    public function test_get_explanation() {
887
    public function test_get_explanation(): void {
Línea 888... Línea 888...
888
        $explanation = \core_tests\event\full_docblock::get_explanation();
888
        $explanation = \core_tests\event\full_docblock::get_explanation();
889
 
889
 
890
        $expected = "This is an explanation of the event.
890
        $expected = "This is an explanation of the event.
Línea 910... Línea 910...
910
 
910
 
911
    /**
911
    /**
912
     * Test that general information about an event is returned
912
     * Test that general information about an event is returned
913
     * by the get_static_info() method.
913
     * by the get_static_info() method.
914
     */
914
     */
915
    public function test_get_static_info() {
915
    public function test_get_static_info(): void {
Línea 916... Línea 916...
916
        $staticinfo = \core_tests\event\static_info_viewing::get_static_info();
916
        $staticinfo = \core_tests\event\static_info_viewing::get_static_info();
917
 
917
 
918
        $expected = array(
918
        $expected = array(
Línea 932... Línea 932...
932
     *
932
     *
933
     * What we are testing is if we can subscribe to a parent event class, instead of only
933
     * What we are testing is if we can subscribe to a parent event class, instead of only
934
     * the base event class or the final, implemented event class.  This enables us to subscribe
934
     * the base event class or the final, implemented event class.  This enables us to subscribe
935
     * to things like all course module view events, all comment created events, etc.
935
     * to things like all course module view events, all comment created events, etc.
936
     */
936
     */
937
    public function test_observe_parent_event() {
937
    public function test_observe_parent_event(): void {
938
        $this->resetAfterTest();
938
        $this->resetAfterTest();
Línea 939... Línea 939...
939
 
939
 
940
        // Ensure this has been reset prior to using it.
940
        // Ensure this has been reset prior to using it.