@@ -27,6 +27,111 @@ public function setUp()
27
27
parent ::setUp ();
28
28
}
29
29
30
+ /**
31
+ * helper function to test simple invalid cases:
32
+ * will test all invalid users sending in all required valid params
33
+ * will also test all combinations of invalid params with a valid user
34
+ * for each required param
35
+ * @param type $method
36
+ * @param type $validUser
37
+ * @param type $invalidUsers
38
+ * @param type $requiredParams
39
+ */
40
+ protected function exerciseInvalidCases ($ method , $ validUser , $ invalidUsers , $ requiredParams )
41
+ {
42
+ // test all invalid users with valid params
43
+ foreach ($ invalidUsers as $ invalidUser )
44
+ {
45
+ $ this ->resetAll ();
46
+ if ($ invalidUser != null )
47
+ {
48
+ $ this ->params ['token ' ] = $ this ->_loginAsUser ($ invalidUser );
49
+ }
50
+ $ this ->params ['method ' ] = $ method ;
51
+ foreach ($ requiredParams as $ requiredParam )
52
+ {
53
+ $ this ->params [$ requiredParam ['name ' ]] = $ requiredParam ['valid ' ];
54
+ }
55
+ $ resp = $ this ->_callJsonApi ();
56
+ $ this ->_assertStatusFail ($ resp , MIDAS_INVALID_POLICY );
57
+ }
58
+
59
+ // test valid user with all combinations of missing/invalid/valid params
60
+ // will not test a case of valid user and all valid params
61
+
62
+ $ numParams = sizeof ($ requiredParams );
63
+ // create an int array that is initially all 0
64
+ $ requiredParamStates = array_fill (0 , $ numParams , 0 );
65
+ $ allTwosSum = 2 * $ numParams ;
66
+
67
+ while (array_sum ($ requiredParamStates ) < $ allTwosSum )
68
+ {
69
+ $ this ->resetAll ();
70
+ $ this ->params ['token ' ] = $ this ->_loginAsUser ($ validUser );
71
+ $ this ->params ['method ' ] = $ method ;
72
+ $ skipTestCase = false ;
73
+ foreach ($ requiredParams as $ ind => $ requiredParam )
74
+ {
75
+ // find the state corresponding to this param
76
+ $ state = $ requiredParamStates [$ ind ];
77
+ // 0s mean the param is missing (not sent)
78
+ if ($ state == 1 )
79
+ {
80
+ // 1s mean an invalid form of the param is sent
81
+ if (!array_key_exists ('invalid ' , $ requiredParam ))
82
+ {
83
+ // some params may not have an invalid form
84
+ // skip this test case as it would repeat the case of the missing param
85
+ $ skipTestCase = true ;
86
+ break ;
87
+ }
88
+ $ this ->params [$ requiredParam ['name ' ]] = $ requiredParam ['invalid ' ];
89
+ }
90
+ elseif ($ state == 2 )
91
+ {
92
+ // 2s mean a valid form of the param is sent
93
+ $ this ->params [$ requiredParam ['name ' ]] = $ requiredParam ['valid ' ];
94
+ }
95
+ elseif ($ state < 0 || $ state > 2 )
96
+ {
97
+ throw new Exception ("left most param state is invalid value: " .$ state );
98
+ }
99
+ }
100
+ if (!$ skipTestCase )
101
+ {
102
+ $ resp = $ this ->_callJsonApi ();
103
+ $ this ->_assertStatusFail ($ resp , MIDAS_INVALID_PARAMETER );
104
+ }
105
+
106
+ // now increment the parameter states
107
+ // add 1 to the right most value
108
+ $ incrementIndex = $ numParams - 1 ;
109
+ $ rightMost = $ requiredParamStates [$ incrementIndex ];
110
+ $ rightMost += 1 ;
111
+ $ requiredParamStates [$ incrementIndex ] = $ rightMost ;
112
+ while ($ rightMost == 3 )
113
+ {
114
+ // if the right most goes to 3, set it to 0
115
+ // and repeat the process one index to the left, stop moving
116
+ // to the left when the last increment doesn't go to 3,
117
+ // i.e. there are no more carry bits
118
+ $ rightMost = 0 ;
119
+ $ requiredParamStates [$ incrementIndex ] = $ rightMost ;
120
+ if ($ incrementIndex > 0 )
121
+ {
122
+ $ incrementIndex -= 1 ;
123
+ $ rightMost = $ requiredParamStates [$ incrementIndex ];
124
+ $ rightMost += 1 ;
125
+ $ requiredParamStates [$ incrementIndex ] = $ rightMost ;
126
+ }
127
+ else
128
+ {
129
+ throw new Exception ("left most param state is 3 " );
130
+ }
131
+ }
132
+ }
133
+ }
134
+
30
135
/** Test adding and removing a user from a group */
31
136
public function testGroupUserAddRemove ()
32
137
{
@@ -40,71 +145,23 @@ public function testGroupUserAddRemove()
40
145
$ commMember = $ userModel ->load ('4 ' );
41
146
$ commModerator = $ userModel ->load ('5 ' );
42
147
$ commAdmin = $ userModel ->load ('6 ' );
43
- $ nonModerators = array ($ commMember );
44
- $ nonAdmins = array ($ commMember , $ commModerator );
45
- $ moderators = array ($ commModerator , $ commAdmin );
46
148
47
149
$ validGroupId = '3004 ' ;
48
150
$ invalidGroupId = '-10 ' ;
49
151
$ validUserId = '2 ' ;
50
152
$ invalidUserId = '-10 ' ;
51
153
52
- // test all the failure cases
154
+ // add in an anonymous user to non admins
155
+ $ invalidUsers = array ($ commMember , $ commModerator , false );
156
+
157
+ // test all the invalid cases
53
158
foreach ($ methods as $ method )
54
159
{
55
- // Try anonymously first
56
- $ this ->resetAll ();
57
- $ this ->params ['method ' ] = $ method ;
58
- $ this ->params ['group_id ' ] = $ validGroupId ;
59
- $ this ->params ['user_id ' ] = $ validUserId ;
60
- $ resp = $ this ->_callJsonApi ();
61
- $ this ->_assertStatusFail ($ resp , MIDAS_INVALID_POLICY );
160
+ $ requiredParams = array (
161
+ array ('name ' => 'group_id ' , 'valid ' => $ validGroupId , 'invalid ' => $ invalidGroupId ),
162
+ array ('name ' => 'user_id ' , 'valid ' => $ validUserId , 'invalid ' => $ invalidUserId ));
62
163
63
- // missing group_id
64
- $ this ->resetAll ();
65
- $ this ->params ['token ' ] = $ this ->_loginAsUser ($ commAdmin );
66
- $ this ->params ['method ' ] = $ method ;
67
- $ this ->params ['user_id ' ] = $ validUserId ;
68
- $ resp = $ this ->_callJsonApi ();
69
- $ this ->_assertStatusFail ($ resp , MIDAS_INVALID_PARAMETER );
70
-
71
- // missing user_id
72
- $ this ->resetAll ();
73
- $ this ->params ['token ' ] = $ this ->_loginAsUser ($ commAdmin );
74
- $ this ->params ['method ' ] = $ method ;
75
- $ this ->params ['group_id ' ] = $ validGroupId ;
76
- $ resp = $ this ->_callJsonApi ();
77
- $ this ->_assertStatusFail ($ resp , MIDAS_INVALID_PARAMETER );
78
-
79
- // an invalid group
80
- $ this ->resetAll ();
81
- $ this ->params ['token ' ] = $ this ->_loginAsUser ($ commAdmin );
82
- $ this ->params ['method ' ] = $ method ;
83
- $ this ->params ['group_id ' ] = $ invalidGroupId ;
84
- $ this ->params ['user_id ' ] = $ validUserId ;
85
- $ resp = $ this ->_callJsonApi ();
86
- $ this ->_assertStatusFail ($ resp , MIDAS_INVALID_PARAMETER );
87
-
88
- // an invalid user
89
- $ this ->resetAll ();
90
- $ this ->params ['token ' ] = $ this ->_loginAsUser ($ commAdmin );
91
- $ this ->params ['method ' ] = $ method ;
92
- $ this ->params ['group_id ' ] = $ validGroupId ;
93
- $ this ->params ['user_id ' ] = $ invalidUserId ;
94
- $ resp = $ this ->_callJsonApi ();
95
- $ this ->_assertStatusFail ($ resp , MIDAS_INVALID_PARAMETER );
96
-
97
- // as a non admin
98
- foreach ($ nonAdmins as $ nonAdmin )
99
- {
100
- $ this ->resetAll ();
101
- $ this ->params ['token ' ] = $ this ->_loginAsUser ($ nonAdmin );
102
- $ this ->params ['method ' ] = $ method ;
103
- $ this ->params ['group_id ' ] = $ validGroupId ;
104
- $ this ->params ['user_id ' ] = $ validUserId ;
105
- $ resp = $ this ->_callJsonApi ();
106
- $ this ->_assertStatusFail ($ resp , MIDAS_INVALID_POLICY );
107
- }
164
+ $ this ->exerciseInvalidCases ($ method , $ commAdmin , $ invalidUsers , $ requiredParams );
108
165
}
109
166
110
167
// ensure the user isn't already in the group
@@ -137,5 +194,79 @@ public function testGroupUserAddRemove()
137
194
$ this ->assertFalse ($ groupModel ->userInGroup ($ changedUser , $ group ), "This user is not expected to be in the group " );
138
195
}
139
196
197
+ /** Test adding and removing a group */
198
+ public function testGroupAddRemove ()
199
+ {
200
+ $ validCommunityId = 2001 ;
201
+ $ invalidCommunityId = -10 ;
202
+
203
+ $ communityModel = MidasLoader::loadModel ('Community ' );
204
+ $ comm2001 = $ communityModel ->load ('2001 ' );
205
+ $ userModel = MidasLoader::loadModel ('User ' );
206
+ $ commMember = $ userModel ->load ('4 ' );
207
+ $ commModerator = $ userModel ->load ('5 ' );
208
+ $ commAdmin = $ userModel ->load ('6 ' );
209
+
210
+ // add in an anonymous user to non admins
211
+ $ invalidUsers = array ($ commMember , $ commModerator , false );
212
+
213
+ // group add
214
+
215
+ $ addMethod = "midas.group.add " ;
216
+ $ newGroupName = 'new group ' ;
217
+ $ addMethodRequiredParams = array (
218
+ array ('name ' => 'community_id ' , 'valid ' => $ validCommunityId , 'invalid ' => $ invalidCommunityId ),
219
+ array ('name ' => 'name ' , 'valid ' => $ newGroupName )); // no invalid name
220
+
221
+ $ this ->exerciseInvalidCases ($ addMethod , $ commAdmin , $ invalidUsers , $ addMethodRequiredParams );
222
+
223
+ $ groupModel = MidasLoader::loadModel ('Group ' );
224
+ $ existingGroups = $ groupModel ->findByCommunity ($ comm2001 );
140
225
226
+ // add a group via the api call
227
+
228
+ $ addedGroupName = 'ApiCallGroupMethodsTest ' ;
229
+ $ this ->resetAll ();
230
+ $ this ->params ['token ' ] = $ this ->_loginAsUser ($ commAdmin );
231
+ $ this ->params ['method ' ] = $ addMethod ;
232
+ $ this ->params ['community_id ' ] = $ validCommunityId ;
233
+ $ this ->params ['name ' ] = $ addedGroupName ;
234
+ $ resp = $ this ->_callJsonApi ();
235
+ $ this ->_assertStatusOk ($ resp );
236
+
237
+ $ addedGroupId = $ resp ->data ->group_id ;
238
+ // check that the group didn't already exist for the community
239
+ foreach ($ existingGroups as $ existingGroup )
240
+ {
241
+ $ this ->assertNotEquals ($ addedGroupId , $ existingGroup ->getGroupId (), 'added group has the same id as an existing group ' );
242
+ }
243
+ $ addedGroup = $ groupModel ->load ($ addedGroupId );
244
+ // check that the added group has the correct values
245
+ $ this ->assertEquals ($ addedGroup ->getCommunityId (), $ validCommunityId , 'added group has incorrect community id ' );
246
+ $ this ->assertEquals ($ addedGroup ->getName (), $ addedGroupName , 'added group has incorrect community id ' );
247
+
248
+ // group remove
249
+
250
+ $ invalidGroupId = -10 ;
251
+ $ removeMethod = "midas.group.remove " ;
252
+ $ removeMethodRequiredParams = array (
253
+ array ('name ' => 'group_id ' , 'valid ' => $ addedGroupId , 'invalid ' => $ invalidGroupId ));
254
+
255
+ $ this ->exerciseInvalidCases ($ removeMethod , $ commAdmin , $ invalidUsers , $ removeMethodRequiredParams );
256
+
257
+ // remove the group via the api call
258
+
259
+ $ this ->resetAll ();
260
+ $ this ->params ['token ' ] = $ this ->_loginAsUser ($ commAdmin );
261
+ $ this ->params ['method ' ] = $ removeMethod ;
262
+ $ this ->params ['group_id ' ] = $ addedGroupId ;
263
+ $ resp = $ this ->_callJsonApi ();
264
+ $ this ->_assertStatusOk ($ resp );
265
+ $ success = $ resp ->data ->success ;
266
+ $ this ->assertEquals ($ success , 'true ' , 'success value should have been true ' );
267
+
268
+ // ensure that the group doesn't exist
269
+ $ addedGroup = $ groupModel ->load ($ addedGroupId );
270
+ $ this ->assertFalse ($ addedGroup , "group should have been removed but remains " );
271
+ }
141
272
}
0 commit comments