Merge lp:~canonical-isd-hackers/drupal-teams/unit-tests into lp:drupal-teams/6.x

Proposed by Stuart Metcalfe
Status: Merged
Merged at revision: not available
Proposed branch: lp:~canonical-isd-hackers/drupal-teams/unit-tests
Merge into: lp:drupal-teams/6.x
Diff against target: None lines
To merge this branch: bzr merge lp:~canonical-isd-hackers/drupal-teams/unit-tests
Reviewer Review Type Date Requested Status
Łukasz Czyżykowski (community) Approve
Review via email: mp+6473@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Stuart Metcalfe (stuartmetcalfe) wrote :

Big drop of retrospective unit tests. Coverage now at ~65%. No changes to functional code

Revision history for this message
Łukasz Czyżykowski (lukasz-czyzykowski) wrote :

It looks like everything is OK. Code coverage for drupal-teams is at 66.67%

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'openid_teams.module'
2--- openid_teams.module 2009-05-11 20:26:38 +0000
3+++ openid_teams.module 2009-05-12 15:19:41 +0000
4@@ -403,7 +403,7 @@
5 function openid_teams_trusted_new_form_submit($form, &$form_state) {
6 if ($form_state['values']['form_id'] == 'openid_teams_trusted_new_form') {
7 db_query('INSERT INTO {openid_teams_trusted} (server) '.
8- "VALUES ('%s')");
9+ "VALUES ('%s')", $form_state['values']['new_server']);
10 $sid = db_last_insert_id('openid_teams_trusted', 'sid');
11 drupal_set_message(t('Trusted server added'));
12 $form_state['redirect'] = 'admin/settings/openid_teams/trusted';
13
14=== added directory 'tests'
15=== added file 'tests/module.test'
16--- tests/module.test 1970-01-01 00:00:00 +0000
17+++ tests/module.test 2009-05-12 11:08:39 +0000
18@@ -0,0 +1,210 @@
19+<?php
20+
21+class DrupalTeamsTestCase extends DrupalWebTestCase {
22+ private $all_tids = array();
23+ private $all_rids = array(1, 2, 1, 2);
24+ private $all_sids = array();
25+ private $all_teams = array('team1', 'team2', 'team2', 'team3');
26+ private $all_servers = array(
27+ null,
28+ 'http://localhost/test/',
29+ 'http://localhost/test/',
30+ 'http://*.example.com/'
31+ );
32+ private $rids_matched_by_servers = array(
33+ array(
34+ 'server' => 'http://localhost/test/',
35+ 'assigned_team' => 'team2',
36+ 'fixture_rids' => array(1, 2),
37+ 'fixture_tids_index' => array(1, 2),
38+ ),
39+ array(
40+ 'server' => 'http://*.example.com/',
41+ 'assigned_team' => 'team3',
42+ 'fixture_rids' => array(2),
43+ 'fixture_tids_index' => array(2),
44+ ),
45+ );
46+
47+ function getInfo() {
48+ return array(
49+ 'name' => t('Teams'),
50+ 'description' => t('Teams/openid unit tests'),
51+ 'group' => t('OpenID Teams'),
52+ );
53+ }
54+
55+ function testAddsFieldToUserAdminSettingsForm() {
56+ $form = $form_state = array();
57+ $form_id = 'user_admin_settings';
58+ openid_teams_form_alter($form, $form_state, $form_id);
59+ $this->assertTrue(is_array($form['openid']['openid_teams_limit_to_known']));
60+ }
61+
62+ function testIgnoresNonUserAdminSettingsForm() {
63+ $form = $form_state = array();
64+ $form_id = 'some_other_form';
65+ openid_teams_form_alter($form, $form_state, $form_id);
66+ $this->assertTrue(sizeof(array_keys($form)) == 0);
67+ }
68+
69+ function testHandlesUserAdminSettingsSubmit() {
70+ variable_del('openid_teams_limit_to_known');
71+ $form = array();
72+ $form_values = array(
73+ 'values' => array(
74+ 'form_id' => 'user_admin_settings',
75+ 'openid_teams_limit_to_known' => true,
76+ ),
77+ );
78+ openid_teams_form_submit($form, $form_values);
79+ $this->assertTrue(variable_get('openid_teams_limit_to_known', false));
80+ }
81+
82+ function testHandlesUserAdminSettingsSubmitNotSet() {
83+ variable_del('openid_teams_limit_to_known');
84+ $form = array();
85+ $form_values = array(
86+ 'values' => array(
87+ 'form_id' => 'user_admin_settings',
88+ ),
89+ );
90+ openid_teams_form_submit($form, $form_values);
91+ $this->assertFalse(variable_get('openid_teams_limit_to_known', true));
92+ }
93+
94+ function testGetCurrentTeams() {
95+ $result = sort(get_current_teams());
96+ $fixture = sort(array_unique($this->all_teams));
97+ $this->assertEqual($result, $fixture);
98+ }
99+
100+ function testGetRolesTeamsMap() {
101+ $result = get_roles_teams_map();
102+ $fixture = array_combine($this->all_tids, $this->all_rids);
103+ $this->assertEqual($result, $fixture);
104+ }
105+
106+ function testGetTeamsRolesMap() {
107+ $result = get_teams_roles_map();
108+ $fixture = array();
109+ for ($i = 0; $i < sizeof($this->all_teams); $i++) {
110+ $team = $this->all_teams[$i];
111+ $rid = $this->all_rids[$i];
112+ if (!array_key_exists($team, $fixture)) {
113+ $fixture[$team] = array();
114+ }
115+ $fixture[$team][] = $rid;
116+ }
117+ $this->assertEqual($result, $fixture);
118+ }
119+
120+ function testGetTeamRoleIds() {
121+ foreach ($this->rids_matched_by_servers as $test_case) {
122+ $team = $test_case['assigned_team'];
123+ $server = $test_case['server'];
124+ $result = sort(get_team_role_ids($team, $server));
125+ $fixture = sort($test_case['fixture_rids']);
126+ $this->assertEqual($result, $fixture);
127+ }
128+ }
129+
130+ function testGetApprovedTeamMappingsWithArray() {
131+ foreach ($this->rids_matched_by_servers as $test_case) {
132+ $teams = array('no-team1', $test_case['assigned_team'], 'no-team2');
133+ $server = $test_case['server'];
134+ $result = sort(get_approved_team_mappings($teams, $server));
135+ $fixture = sort($this->getTidsWithIndexes($test_case['fixture_tids_index']));
136+ $this->assertEqual($result, $fixture);
137+ }
138+ }
139+
140+ function testGetApprovedTeamMappingsWithString() {
141+ foreach ($this->rids_matched_by_servers as $test_case) {
142+ $teams = "no-team1,no-team2,{$test_case['assigned_team']}";
143+ $server = $test_case['server'];
144+ $result = get_approved_team_mappings($teams, $server);
145+ $fixture = sort($this->getTidsWithIndexes($test_case['fixture_tids_index']));
146+ $this->assertEqual($result, $fixture);
147+ }
148+ }
149+
150+ function testGetApprovedTeamMappingsWithStringSingleTeam() {
151+ foreach ($this->rids_matched_by_servers as $test_case) {
152+ $teams = $test_case['assigned_team'];
153+ $server = $test_case['server'];
154+ $result = get_approved_team_mappings($teams, $server);
155+ $fixture = sort($this->getTidsWithIndexes($test_case['fixture_tids_index']));
156+ $this->assertEqual($result, $fixture);
157+ }
158+ }
159+
160+ private function getTidsWithIndexes($all_indexes) {
161+ $tids = array();
162+ foreach ($all_indexes as $index) {
163+ $tids[] = $this->all_tids[$index];
164+ }
165+ return $tids;
166+ }
167+
168+ function testOpenidTeamsGetTrustedListWithoutAllServers() {
169+ $result = openid_teams_get_trusted_list();
170+ $fixture = $this->getUniqueTrustedServersCount();
171+ $this->assertEqual(sizeof($result), $fixture, sprintf('Expected %d, got %d', $fixture, sizeof($result)));
172+ }
173+
174+ function testOpenidTeamsGetTrustedListWithAllServers() {
175+ $result = openid_teams_get_trusted_list(TRUE);
176+ $fixture = $this->getUniqueTrustedServersCount()+1;
177+ $this->assertEqual(sizeof($result), $fixture, sprintf('Expected %d, got %d', $fixture, sizeof($result)));
178+ }
179+
180+ function getUniqueTrustedServersCount() {
181+ $non_null_servers = array();
182+ foreach ($this->all_servers as $server) {
183+ if ($server) {
184+ $non_null_servers[] = $server;
185+ }
186+ }
187+ return sizeof(array_unique($non_null_servers));
188+ }
189+
190+ function setUp() {
191+ parent::setUp('openid', 'openid_launchpad', 'openid_teams');
192+ $this->setupTrustedServersData();
193+ $this->setupTeamsRolesData();
194+ }
195+
196+ function setupTrustedServersData() {
197+ $servers_added = array();
198+ $sid = 1;
199+ for ($i = 0; $i < sizeof($this->all_servers); $i++) {
200+ $server = $this->all_servers[$i];
201+ if ($server != null) {
202+ if (!in_array($server, $servers_added)) {
203+ db_query("INSERT INTO {openid_teams_trusted} (sid, server) VALUES (%d, '%s')", $sid++, $server);
204+ $servers_added[] = $server;
205+ }
206+ $this->all_sids[] = $sid;
207+ }
208+ }
209+ }
210+
211+ function setupTeamsRolesData() {
212+ for ($i = 0; $i < sizeof($this->all_teams); $i++) {
213+ $tid = $i+1;
214+ $this->all_tids[$i] = $tid;
215+ $rid = $this->all_rids[$i];
216+ $sid = $this->all_sids[$i];
217+ $team = $this->all_teams[$i];
218+ db_query('INSERT INTO {openid_teams_roles} (tid, rid, sid, team) '.
219+ "VALUES (%d, %d, %d, '%s')", $tid, $rid, $sid, $team);
220+ }
221+ }
222+
223+ function tearDown() {
224+ parent::tearDown();
225+ db_query('DELETE FROM {openid_teams_roles}');
226+ variable_del('openid_teams_limit_to_known');
227+ }
228+}
229
230=== added directory 'tests/openid_test'
231=== added file 'tests/openid_test.info'
232--- tests/openid_test.info 1970-01-01 00:00:00 +0000
233+++ tests/openid_test.info 2009-05-12 11:08:39 +0000
234@@ -0,0 +1,8 @@
235+; $Id$
236+name = "OpenID Test"
237+description = "Support module for OpenID Teams tests"
238+package = Testing
239+version = VERSION
240+core = 6.x
241+files[] = openid_test.module
242+hidden = TRUE
243\ No newline at end of file
244
245=== added file 'tests/openid_test.module'
246--- tests/openid_test.module 1970-01-01 00:00:00 +0000
247+++ tests/openid_test.module 2009-05-12 11:08:39 +0000
248@@ -0,0 +1,21 @@
249+<?php
250+
251+function openid_test_menu() {
252+ return array(
253+ 'teams/test/login' => array(
254+ 'title' => 'Mock login',
255+ 'page callback' => 'openid_test_login_with_teams',
256+ 'access arguments' => array('access content'),
257+ ),
258+ );
259+}
260+
261+function openid_test_login_with_teams($teams) {
262+ $op = 'user_register';
263+ $response = array(
264+ 'openid.op_endpoint' => 'http://localhost/',
265+ 'openid.lp.is_member' => $teams,
266+ );
267+ openid_teams_openid_response($op, $response);
268+ return "Requested teams {$teams}";
269+}
270
271=== added file 'tests/openid_test/admin.test'
272--- tests/openid_test/admin.test 1970-01-01 00:00:00 +0000
273+++ tests/openid_test/admin.test 2009-05-12 15:17:29 +0000
274@@ -0,0 +1,118 @@
275+<?php
276+
277+class DrupalTeamsAdminTestCase extends DrupalWebTestCase {
278+ function getInfo() {
279+ return array(
280+ 'name' => t('Teams admin'),
281+ 'description' => t('Teams/openid admin functions unit tests'),
282+ 'group' => t('OpenID Teams'),
283+ );
284+ }
285+
286+ function setUp() {
287+ parent::setUp('openid', 'openid_teams');
288+ }
289+
290+ public function testOpenidTeamsTrustedNewFormReturnsNonEmptyArray() {
291+ $form = openid_teams_trusted_new_form();
292+ $result = is_array($form) && !empty($form);
293+ $this->assertTrue($result, 'New trusted server form returns a non-empty array');
294+ }
295+
296+ public function testOpenidTeamsTrustedNewFormSubmitSavesData() {
297+ db_query('DELETE FROM {openid_teams_trusted}');
298+ $fixture = 'http://localhost/';
299+ $form = openid_teams_trusted_new_form();
300+ $form_state = array(
301+ 'values' => array(
302+ 'form_id' => 'openid_teams_trusted_new_form',
303+ 'new_server' => $fixture,
304+ ),
305+ );
306+ openid_teams_trusted_new_form_submit($form, $form_state);
307+ $this->assertEqual($this->countTableRows('trusted'), 1, 'Trusted server added to database');
308+ $query = db_query('SELECT * FROM {openid_teams_trusted}');
309+ $result = db_fetch_object($query);
310+ $this->assertEqual($result->server, $fixture, "Confirm server value. Expected {$fixture}. ".
311+ "Got {$result->server}");
312+ }
313+
314+ public function testOpenidTeamsAdminTrustedDeleteSubmitDeletesRow() {
315+ db_query('DELETE FROM {openid_teams_trusted}');
316+ db_query("INSERT INTO {openid_teams_trusted} (server) VALUES ('test1')");
317+ db_query("INSERT INTO {openid_teams_trusted} (server) VALUES ('test2')");
318+ $sid = db_last_insert_id('openid_teams_trusted', 'sid');
319+ if ($this->countTableRows('trusted') != 2) {
320+ throw new Exception('Test data not added. Cannot continue.');
321+ }
322+ $form = array();
323+ $form_state = array(
324+ 'values' => array(
325+ 'sid' => $sid,
326+ ),
327+ );
328+ openid_teams_admin_trusted_delete_submit($form, $form_state);
329+ $result = $this->countTableRows('trusted');
330+ $this->assertEqual($result, 1, "Trusted server removed. Expected 1 rows. Got {$result}");
331+ }
332+
333+ public function testOpenidTeamsFormAlterChangesForm() {
334+ $form = $fixture = $form_state = array();
335+ openid_teams_form_alter($form, $form_state, 'user_admin_settings');
336+ $this->assertNotEqual($fixture, $form);
337+ }
338+
339+ public function testOpenidTeamsFormSubmitChangesVariableWhenSet() {
340+ variable_set('openid_teams_limit_to_known', false);
341+ $form = array();
342+ $form_state = array(
343+ 'values' => array(
344+ 'form_id' => 'user_admin_settings',
345+ 'openid_teams_limit_to_known' => '1',
346+ ),
347+ );
348+ openid_teams_form_submit($form, &$form_state);
349+ $result = variable_get('openid_teams_limit_to_known', false);
350+ $this->assertTrue($result, 'OpenID teams limit variable set');
351+ }
352+
353+ public function testOpenidTeamsFormSubmitChangesVariableWhenNotSet() {
354+ variable_set('openid_teams_limit_to_known', true);
355+ $form = array();
356+ $form_state = array(
357+ 'values' => array(
358+ 'form_id' => 'user_admin_settings',
359+ ),
360+ );
361+ openid_teams_form_submit($form, &$form_state);
362+ $result = variable_get('openid_teams_limit_to_known', true);
363+ $this->assertFalse($result, 'OpenID teams limit variable set');
364+ }
365+
366+ public function testOpenidTeamsAdminFormReturnsNonEmptyArray() {
367+ db_query("INSERT INTO {openid_teams_roles} (rid, sid, team) VALUES (2, -1, 'team1')");
368+ db_query("INSERT INTO {openid_teams_roles} (rid, sid, team) VALUES (2, -1, 'team2')");
369+ $form = openid_teams_admin_form();
370+ $result = is_array($form) && !empty($form);
371+ $this->assertTrue($result, 'Main admin form returns a non-empty array');
372+ }
373+
374+ public function testOpenidTeamsMenuReturnsNonEmptyArray() {
375+ $form = openid_teams_menu();
376+ $result = is_array($form) && !empty($form);
377+ $this->assertTrue($result, 'Menu hook returns a non-empty array');
378+ }
379+
380+ public function testOpenidTeamsTrustedPageReturnsNonEmptyString() {
381+ $content = openid_teams_trusted_page();
382+ $result = is_string($content) && !empty($content);
383+ $this->assertTrue($result, 'Trusted server management page returns content');
384+ }
385+
386+ private function countTableRows($tablename) {
387+ $sql = sprintf('SELECT count(*) AS count FROM {openid_teams_%s}', $tablename);
388+ $query = db_query($sql);
389+ $result = db_fetch_object($query);
390+ return $result->count;
391+ }
392+}
393
394=== added file 'tests/request.test'
395--- tests/request.test 1970-01-01 00:00:00 +0000
396+++ tests/request.test 2009-05-12 11:08:39 +0000
397@@ -0,0 +1,39 @@
398+<?php
399+
400+class DrupalTeamsRequestTestCase extends DrupalWebTestCase {
401+ function getInfo() {
402+ return array(
403+ 'name' => t('Teams request'),
404+ 'description' => t('Teams/openid request unit tests'),
405+ 'group' => t('OpenID Teams'),
406+ );
407+ }
408+
409+ function setUp() {
410+ parent::setUp('openid_teams');
411+ }
412+
413+ function tearDown() {
414+ parent::tearDown();
415+ }
416+
417+ public function testExtensionReturned() {
418+ $ext = module_invoke_all('openid', 'extension');
419+ $result = array_search('lp', $ext);
420+ $this->assertNotIdentical($result, false, 'lp extension returned');
421+ }
422+
423+ public function testTeamsAddedToRequest() {
424+ $all_teams = array('team1', 'team2', 'team3');
425+ $tid = 1;
426+ foreach ($all_teams as $team) {
427+ $rid = $this->_drupalCreateRole(array('access content'));
428+ db_query('INSERT INTO {openid_teams_roles} (tid, rid, sid, team) '.
429+ "VALUES (%d, %d, -1, '%s')", $tid++, $rid, $team);
430+ }
431+ $request = module_invoke_all('openid', 'request');
432+ $result = $request['openid.lp.query_membership'];
433+ $fixture = join(',', $all_teams);
434+ $this->assertEqual($result, $fixture, 'Teams added to request');
435+ }
436+}
437
438=== added file 'tests/response.test'
439--- tests/response.test 1970-01-01 00:00:00 +0000
440+++ tests/response.test 2009-05-12 11:08:39 +0000
441@@ -0,0 +1,56 @@
442+<?php
443+
444+class DrupalTeamsResponseTestCase extends DrupalWebTestCase {
445+ function getInfo() {
446+ return array(
447+ 'name' => t('Teams response'),
448+ 'description' => t('Teams/openid response unit tests'),
449+ 'group' => t('OpenID Teams'),
450+ );
451+ }
452+
453+ function setUp() {
454+ parent::setUp('openid_test', 'openid_teams');
455+ }
456+
457+ function tearDown() {
458+ $this->drupalGet('logout');
459+ variable_del('openid_teams_limit_to_known');
460+ parent::tearDown();
461+ }
462+
463+ public function testOpenidResponseLogsOutUserNotInTeamWithVariableSet() {
464+ $this->runResponseTestForVariableStateAndTeam(true, 'team2');
465+ $this->assertResponse(403, 'User has been logged out by mock registration call (not in team, '.
466+ 'variable set)');
467+ }
468+
469+ public function testOpenidResponseAcceptsUserInTeamWithVariableSet() {
470+ $this->runResponseTestForVariableStateAndTeam(true, 'team1');
471+ $this->assertResponse(200, 'User is still logged in after mock registration call (in team, '.
472+ 'variable set)');
473+ }
474+
475+ public function testOpenidResponseAcceptsUserNotInTeamWithVariableNotSet() {
476+ $this->runResponseTestForVariableStateAndTeam(false, 'team2');
477+ $this->assertResponse(200, 'User is still logged in after mock registration call (not in '.
478+ 'team, variable not set');
479+ }
480+
481+ public function testOpenidResponseAcceptsUserInTeamWithVariableNotSet() {
482+ $this->runResponseTestForVariableStateAndTeam(false, 'team1');
483+ $this->assertResponse(200, 'User is still logged in after mock registration call (in team, '.
484+ 'variable not set)');
485+ }
486+
487+ private function runResponseTestForVariableStateAndTeam($limit_to_known, $team) {
488+ db_query("INSERT INTO {openid_teams_roles} (tid, rid, sid, team) VALUES (1, 1, -1, 'team1')");
489+ $this->drupalGet('logout');
490+ $user = $this->drupalCreateUser(array('access content'));
491+ $this->drupalLogin($user);
492+ variable_set('openid_teams_limit_to_known', $limit_to_known);
493+ $content = $this->drupalGet("teams/test/login/{$team}");
494+ $this->drupalGet("user/{$user->uid}");
495+ db_query('DELETE FROM {openid_teams_roles}');
496+ }
497+}
498
499=== added file 'tests/roles.test'
500--- tests/roles.test 1970-01-01 00:00:00 +0000
501+++ tests/roles.test 2009-05-12 15:36:47 +0000
502@@ -0,0 +1,55 @@
503+<?php
504+
505+class DrupalTeamsRolesTestCase extends DrupalWebTestCase {
506+ function getInfo() {
507+ return array(
508+ 'name' => t('Teams role assignment'),
509+ 'description' => t('Teams/openid role assignment unit tests'),
510+ 'group' => t('OpenID Teams'),
511+ );
512+ }
513+
514+ function setUp() {
515+ parent::setUp('openid_teams');
516+ }
517+
518+ function tearDown() {
519+ parent::tearDown();
520+ }
521+
522+ function testClearTeamRoles() {
523+ $user = $this->drupalCreateUser(array('access content'));
524+ $roles_before = $this->getRoleIdsForUser($user);
525+ $roles_assigned = array();
526+ for ($assigned = 0; $assigned < 4; $assigned++) {
527+ $roles_assigned[] = $this->addRoleForTeamAndUser($assigned, $user);
528+ }
529+ $roles_now = $this->getRoleIdsForUser($user);
530+ if (sizeof($roles_now) - sizeof($roles_before) != $assigned) {
531+ throw new Exception('Roles incorrectly assigned. Not running test');
532+ } else {
533+ variable_set("openid_assigned_roles_{$user->uid}", join(',', $roles_assigned));
534+ clear_team_roles($user);
535+ variable_del("openid_assigned_roles_{$user->uid}");
536+ $roles_after = $this->getRoleIdsForUser($user);
537+ $this->assertEqual($roles_before, $roles_after, 'Team roles removed from database');
538+ }
539+ }
540+
541+ private function getRoleIdsForUser($user) {
542+ $query = db_query('SELECT rid FROM {users_roles} WHERE uid=%d ORDER BY rid', $user->uid);
543+ $all_rids = array();
544+ while ($row = db_fetch_object($query)) {
545+ $all_rids[] = $row->rid;
546+ }
547+ return $all_rids;
548+ }
549+
550+ private function addRoleForTeamAndUser($team_id, $user) {
551+ $rid = $this->_drupalCreateRole(array('access content'));
552+ db_query('INSERT INTO {openid_teams_roles} (rid, sid, team) '.
553+ "VALUES (%d, -1, 'team%d')", $rid, $team_id);
554+ db_query("INSERT INTO {users_roles} (uid, rid) VALUES (%d, '%s')", $user->uid, $rid);
555+ return $rid;
556+ }
557+}

Subscribers

People subscribed via source and target branches

to all changes: