@@ -138,4 +138,68 @@ class RestfulAuthenticationTestCase extends RestfulCurlBaseTestCase {
138138
139139 $this->assertEqual($user->uid, 0, 'Global user is the correct one after an API call that switches the user.');
140140 }
141+
142+ /**
143+ * Test recording of access time.
144+ */
145+ function testAccessTime() {
146+ global $user;
147+
148+ $user1 = $this->drupalCreateUser();
149+ $user2 = $this->drupalCreateUser();
150+
151+ $handler = restful_get_restful_handler('main', 1, 5);
152+
153+ // Case 1. Ensure that access time is recorded for cookie auth.
154+ $user = $user1;
155+
156+ $user1_access_time_before = db_query('SELECT access FROM {users} WHERE uid = :d', array(':d' => $user1->uid))->fetchObject();
157+
158+ // Perform request authentication.
159+ $handler->getAccount();
160+
161+ $user1_access_time = db_query('SELECT access FROM {users} WHERE uid = :d', array(':d' => $user1->uid))->fetchObject();
162+ $this->assertEqual($user1_access_time->access, REQUEST_TIME, 'Cookie authenticated user access time is updated.');
163+
164+ $this->assertNotEqual($user1_access_time_before->access, $user1_access_time->access, 'Access time before and after request are equal.');
165+
166+ // Case 2. Ensure that access time is recorded for basic auth.
167+ $user = $user2;
168+
169+ $_SERVER['PHP_AUTH_USER'] = $user2->name;
170+ $_SERVER['PHP_AUTH_PW'] = $user2->pass_raw;
171+ $_SERVER['REDIRECT_HTTP_AUTHORIZATION'] = NULL;
172+ $handler = restful_get_restful_handler('articles');
173+
174+ // Perform request authentication.
175+ $handler->getAccount();
176+
177+ $user2_access_time = db_query('SELECT access FROM {users} WHERE uid = :d', array(':d' => $user2->uid))->fetchObject();
178+ $this->assertEqual($user2_access_time->access, REQUEST_TIME, 'Basic authenticated user access time is updated.');
179+
180+ // Case 3. Ensure that the timestamp gets updated.
181+ $user = $user1;
182+
183+ // Get a timestamp that is in the past.
184+ $the_past = REQUEST_TIME - variable_get('session_write_interval');
185+
186+ // To begin, we'll set the timestamp for user1 back a little bit.
187+ $num_updated = db_update('users')
188+ ->fields(array('access' => $the_past))
189+ ->condition('uid', $user1->uid)
190+ ->execute();
191+
192+ $user1_pre_access_time = db_query('SELECT access FROM {users} WHERE uid = :d', array(':d' => $user1->uid))->fetchObject();
193+ $this->assertEqual($user1_pre_access_time->access, $the_past, 'Set user1 access time to a time in the past.');
194+
195+ // Perform an authenticated request.
196+ $this->drupalGet('/api/v1.5/main', array(), array(
197+ 'Authorization' => 'Basic ' . base64_encode($user1->name . ':' . $user1->pass_raw))
198+ );
199+
200+ $user1_post_access_time = db_query('SELECT access FROM {users} WHERE uid = :d', array(':d' => $user1->uid))->fetchObject();
201+
202+ $this->assertEqual($user1_post_access_time->access, REQUEST_TIME, 'Basic authenticated user access time is updated.');
203+ }
204+
141205}
0 commit comments