| 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 tool_monitor;
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | /**
 | 
        
           |  |  | 20 |  * PHPUnit data generator test case.
 | 
        
           |  |  | 21 |  *
 | 
        
           |  |  | 22 |  * @since      Moodle 2.8
 | 
        
           |  |  | 23 |  * @package    tool_monitor
 | 
        
           |  |  | 24 |  * @category   test
 | 
        
           |  |  | 25 |  * @copyright  2014 onwards Simey Lameze <simey@moodle.com>
 | 
        
           |  |  | 26 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 27 |  */
 | 
        
           | 1441 | ariadna | 28 | final class generator_test extends \advanced_testcase {
 | 
        
           | 1 | efrain | 29 |   | 
        
           |  |  | 30 |     /**
 | 
        
           |  |  | 31 |      * Set up method.
 | 
        
           |  |  | 32 |      */
 | 
        
           |  |  | 33 |     public function setUp(): void {
 | 
        
           | 1441 | ariadna | 34 |         parent::setUp();
 | 
        
           | 1 | efrain | 35 |         // Enable monitor.
 | 
        
           |  |  | 36 |         set_config('enablemonitor', 1, 'tool_monitor');
 | 
        
           |  |  | 37 |     }
 | 
        
           |  |  | 38 |   | 
        
           |  |  | 39 |     /**
 | 
        
           |  |  | 40 |      * Test create_rule data generator.
 | 
        
           |  |  | 41 |      */
 | 
        
           | 11 | efrain | 42 |     public function test_create_rule(): void {
 | 
        
           | 1 | efrain | 43 |         $this->setAdminUser();
 | 
        
           |  |  | 44 |         $this->resetAfterTest(true);
 | 
        
           |  |  | 45 |         $course = $this->getDataGenerator()->create_course();
 | 
        
           |  |  | 46 |         $user = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 47 |   | 
        
           |  |  | 48 |         $rulegenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 |         $record = new \stdClass();
 | 
        
           |  |  | 51 |         $record->courseid = $course->id;
 | 
        
           |  |  | 52 |         $record->userid = $user->id;
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 |         $rule = $rulegenerator->create_rule($record);
 | 
        
           |  |  | 55 |         $this->assertInstanceOf('tool_monitor\rule', $rule);
 | 
        
           |  |  | 56 |         $this->assertEquals($rule->userid, $record->userid);
 | 
        
           |  |  | 57 |         $this->assertEquals($rule->courseid, $record->courseid);
 | 
        
           |  |  | 58 |     }
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 |     /**
 | 
        
           |  |  | 61 |      * Test create_subscription data generator.
 | 
        
           |  |  | 62 |      */
 | 
        
           | 11 | efrain | 63 |     public function test_create_subscription(): void {
 | 
        
           | 1 | efrain | 64 |         $this->setAdminUser();
 | 
        
           |  |  | 65 |         $this->resetAfterTest(true);
 | 
        
           |  |  | 66 |   | 
        
           |  |  | 67 |         $user = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 68 |         $course = $this->getDataGenerator()->create_course();
 | 
        
           |  |  | 69 |         $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
 | 
        
           |  |  | 70 |         $rule = $monitorgenerator->create_rule();
 | 
        
           |  |  | 71 |   | 
        
           |  |  | 72 |         $record = new \stdClass();
 | 
        
           |  |  | 73 |         $record->courseid = $course->id;
 | 
        
           |  |  | 74 |         $record->userid = $user->id;
 | 
        
           |  |  | 75 |         $record->ruleid = $rule->id;
 | 
        
           |  |  | 76 |   | 
        
           |  |  | 77 |         $subscription = $monitorgenerator->create_subscription($record);
 | 
        
           |  |  | 78 |         $this->assertEquals($record->courseid, $subscription->courseid);
 | 
        
           |  |  | 79 |         $this->assertEquals($record->ruleid, $subscription->ruleid);
 | 
        
           |  |  | 80 |         $this->assertEquals($record->userid, $subscription->userid);
 | 
        
           |  |  | 81 |         $this->assertEquals(0, $subscription->cmid);
 | 
        
           |  |  | 82 |   | 
        
           |  |  | 83 |         // Make sure rule id is always required.
 | 
        
           |  |  | 84 |         $this->expectException('coding_exception');
 | 
        
           |  |  | 85 |         unset($record->ruleid);
 | 
        
           |  |  | 86 |         $monitorgenerator->create_subscription($record);
 | 
        
           |  |  | 87 |     }
 | 
        
           |  |  | 88 |   | 
        
           |  |  | 89 |     /**
 | 
        
           |  |  | 90 |      * Test create_event data generator.
 | 
        
           |  |  | 91 |      */
 | 
        
           | 11 | efrain | 92 |     public function test_create_event_entries(): void {
 | 
        
           | 1 | efrain | 93 |         $this->setAdminUser();
 | 
        
           |  |  | 94 |         $this->resetAfterTest(true);
 | 
        
           |  |  | 95 |         $context = \context_system::instance();
 | 
        
           |  |  | 96 |   | 
        
           |  |  | 97 |         // Default data generator values.
 | 
        
           |  |  | 98 |         $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
 | 
        
           |  |  | 99 |   | 
        
           |  |  | 100 |         // First create and assertdata using default values.
 | 
        
           |  |  | 101 |         $eventdata = $monitorgenerator->create_event_entries();
 | 
        
           |  |  | 102 |         $this->assertEquals('\core\event\user_loggedin', $eventdata->eventname);
 | 
        
           |  |  | 103 |         $this->assertEquals($context->id, $eventdata->contextid);
 | 
        
           |  |  | 104 |         $this->assertEquals($context->contextlevel, $eventdata->contextlevel);
 | 
        
           |  |  | 105 |     }
 | 
        
           |  |  | 106 |   | 
        
           |  |  | 107 |     /**
 | 
        
           |  |  | 108 |      * Test create_history data generator.
 | 
        
           |  |  | 109 |      */
 | 
        
           | 11 | efrain | 110 |     public function test_create_history(): void {
 | 
        
           | 1 | efrain | 111 |         $this->setAdminUser();
 | 
        
           |  |  | 112 |         $this->resetAfterTest(true);
 | 
        
           |  |  | 113 |         $user = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 114 |         $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
 | 
        
           |  |  | 115 |         $rule = $monitorgenerator->create_rule();
 | 
        
           |  |  | 116 |   | 
        
           |  |  | 117 |         $record = new \stdClass();
 | 
        
           |  |  | 118 |         $record->userid = $user->id;
 | 
        
           |  |  | 119 |         $record->ruleid = $rule->id;
 | 
        
           |  |  | 120 |         $sid = $monitorgenerator->create_subscription($record)->id;
 | 
        
           |  |  | 121 |         $record->sid = $sid;
 | 
        
           |  |  | 122 |         $historydata = $monitorgenerator->create_history($record);
 | 
        
           |  |  | 123 |         $this->assertEquals($record->userid, $historydata->userid);
 | 
        
           |  |  | 124 |         $this->assertEquals($record->sid, $historydata->sid);
 | 
        
           |  |  | 125 |   | 
        
           |  |  | 126 |         // Test using default values.
 | 
        
           |  |  | 127 |         $record->userid = 1;
 | 
        
           |  |  | 128 |         $record->sid = 1;
 | 
        
           |  |  | 129 |         $historydata = $monitorgenerator->create_history($record);
 | 
        
           |  |  | 130 |         $this->assertEquals(1, $historydata->userid);
 | 
        
           |  |  | 131 |         $this->assertEquals(1, $historydata->sid);
 | 
        
           |  |  | 132 |     }
 | 
        
           |  |  | 133 | }
 |