Merge branch 'MDL-48921-27-2nd' of git://github.com/FMCorz/moodle into MOODLE_27_STABLE
[moodle.git] / auth / db / tests / events_test.php
blob46e4408edb1233504e3d4236c20df0c46b9513ae
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.
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/>.
17 /**
18 * Events tests.
20 * @package auth_db
21 * @category test
22 * @copyright 2014 Mark Nelson <markn@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
30 require_once($CFG->dirroot . '/auth/db/tests/db_test.php');
32 class auth_db_events_testcase extends advanced_testcase {
34 /**
35 * Test set up.
37 public function setUp() {
38 $this->resetAfterTest(true);
41 /**
42 * Tests that the locations in the auth_db API that create a user trigger the user_created event.
44 public function test_user_created() {
45 global $DB;
47 $this->preventResetByRollback();
49 // Initialise the database.
50 $authdbtestcase = new auth_db_testcase();
51 $authdbtestcase->init_auth_database();
53 $auth = get_auth_plugin('db');
54 $auth->db_init();
56 // Add a user to the auth_db_users table - we will then call sync_users to
57 // deal with the record here. In this case it will create the user.
58 $user = new stdClass();
59 $user->name = 'mark';
60 $user->pass = 'password123';
61 $user->email = 'what@legend.com';
62 $user->id = $DB->insert_record('auth_db_users', $user);
64 // Run sync_users and capture the user_created event.
65 $sink = $this->redirectEvents();
66 $trace = new null_progress_trace();
67 $auth->sync_users($trace, false);
68 $events = $sink->get_events();
69 $sink->close();
71 // Check that there is only one event.
72 $this->assertEquals(1, count($events));
74 // Get the event.
75 $event = array_pop($events);
77 // Test that the user created event was triggered - no need to test the other
78 // details of the event as that is done extensively in other unit tests.
79 $this->assertInstanceOf('\core\event\user_created', $event);
82 /**
83 * Tests that the locations in the auth_db API that update a user trigger the user_updated event.
85 public function test_user_updated() {
86 global $CFG, $DB;
88 $this->preventResetByRollback();
90 // Initialise the database.
91 $authdbtestcase = new auth_db_testcase();
92 $authdbtestcase->init_auth_database();
94 $auth = get_auth_plugin('db');
95 $auth->db_init();
97 // Add a suspended user.
98 $user = array();
99 $user['username'] = 'mark';
100 $user['suspended'] = '1';
101 $user['mnethostid'] = $CFG->mnet_localhost_id;
102 $user['auth'] = 'db';
103 $this->getDataGenerator()->create_user($user);
105 // Add a user to the auth_db_users table - we will then call sync_users to
106 // deal with the record here. In this case it will un-suspend the user.
107 $user = new stdClass();
108 $user->name = 'mark';
109 $user->pass = 'password123';
110 $user->email = 'what@legend.com';
111 $user->id = $DB->insert_record('auth_db_users', $user);
113 // Set the config to remove the suspension on the user.
114 set_config('removeuser', AUTH_REMOVEUSER_SUSPEND, 'auth/db');
115 $auth->config->removeuser = AUTH_REMOVEUSER_SUSPEND;
117 // Run sync_users and capture the user_updated event.
118 $sink = $this->redirectEvents();
119 $trace = new null_progress_trace();
120 $auth->sync_users($trace, false);
121 $events = $sink->get_events();
122 $sink->close();
124 // Check that there is only one event.
125 $this->assertEquals(1, count($events));
127 // Get the event.
128 $event = array_pop($events);
130 // Test that the user updated event was triggered - no need to test the other
131 // details of the event as that is done extensively in other unit tests.
132 $this->assertInstanceOf('\core\event\user_updated', $event);
134 // Run sync_users and capture the user_updated event.
135 $sink = $this->redirectEvents();
136 $auth->update_user_record('mark');
137 $events = $sink->get_events();
138 $sink->close();
140 // Check that there is only one event.
141 $this->assertEquals(1, count($events));
143 // Get the event.
144 $event = array_pop($events);
146 // Test that the user updated event was triggered - no need to test the other
147 // details of the event as that is done extensively in other unit tests.
148 $this->assertInstanceOf('\core\event\user_updated', $event);