Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
PU_test_dcp_group.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 
7 namespace Dcp\Pu;
8 /**
9  * @author Anakeen
10  * @package Dcp\Pu
11  */
12 require_once 'PU_testcase_dcp_commonfamily.php';
13 
15 {
16  protected static function getCommonImportFile()
17  {
18  return "PU_data_dcp_group.ods";
19  }
20  /**
21  * @dataProvider datagroupWithUserDelete
22  * @param string $login
23  * @param array $groupLoginsToCreate
24  * @param array $groupLoginsToDelete
25  * @param array $groupLoginsResult
26  */
27  public function testDeleteGroupWithUser($login, array $groupLoginsToCreate, array $groupLoginsToDelete, array $groupLoginsResult)
28  {
29  // create user
30  $user = createDoc(self::$dbaccess, "IUSER");
31  $this->assertTrue(is_object($user) , "cannot create user");
32  $err = $user->setValue("us_login", $login);
33  $password = 'secret';
34  $err.= $user->setValue("us_passwd1", $password);
35  $err.= $user->setValue("us_passwd2", $password);
36  $this->assertEmpty($err, sprintf("cannot set iuser %s", $err));
37 
38  $err = $user->store();
39  $this->assertEmpty($err, sprintf("cannot store iuser %s", $err));
40  // create group
41  $newGids = array();
42  $groups = array();
43  foreach ($groupLoginsToCreate as $gLogin) {
44  /**
45  * @var \_IGROUP $group
46  */
47  $group = createDoc(self::$dbaccess, "IGROUP");
48  $this->assertTrue(is_object($group) , "cannot create group");
49  $err = $group->setValue("us_login", $gLogin);
50  $this->assertEmpty($err, sprintf("cannot set igroup %s", $err));
51 
52  $err = $group->store();
53  $this->assertEmpty($err, sprintf("cannot store igroup %s", $err));
54  $group->insertDocument($user->initid);
55  $newGids[] = $group->getRawValue("us_whatid");
56  $groups[$gLogin] = $group;
57  }
58  $u = new \Account("", $user->getRawValue("us_whatid"));
59  $this->assertTrue($u->isAffected() , sprintf("cannot find %s account", $user->getRawValue("us_whatid")));
60  $gids = $u->getGroupsId();
61 
62  $this->assertEmpty(array_diff($newGids, $gids) , "groups are not in new user");
63  //Deleting group
64  foreach ($groupLoginsToDelete as $gLogin) {
65  $err = $groups[$gLogin]->Delete();
66  $this->assertEmpty($err, sprintf("error when delete group : %s", $err));
67  }
68 
69  $userGroups = $u->getUserParents();
70  //Checking result
71  foreach ($userGroups as $uGroup) {
72  if (($uGroup["login"] != "all") && ($uGroup["accounttype"] != 'R')) {
73  $this->assertTrue(in_array($uGroup["login"], $groupLoginsResult) , sprintf("login %s not in %s", $uGroup["login"], implode(',', $groupLoginsResult)));
74  }
75  }
76  }
77  /**
78  * @dataProvider datagroupCreate
79  * @param string $login
80  */
81  public function testDeleteGroup($login)
82  {
83  $group = $this->testCreateGroup($login);
84  $err = $group->Delete();
85  $this->assertEmpty($err, sprintf("cannot delete igroup %s", $err));
86  }
87  /**
88  * @dataProvider datagroupCreate
89  * @param string $login login for user and group
90  * @return \_IGROUP
91  */
92  public function testCreateGroup($login)
93  {
94  /**
95  * @var \_IGROUP $doc
96  */
97  $doc = createDoc(self::$dbaccess, "IGROUP");
98  $this->assertTrue(is_object($doc) , "cannot create group");
99  $err = $doc->setValue("us_login", $login);
100  $this->assertEmpty($err, sprintf("cannot set igroup %s", $err));
101 
102  $err = $doc->store();
103  $this->assertEmpty($err, sprintf("cannot store igroup %s", $err));
104 
105  $u = new \Account();
106  $this->assertTrue($u->setLoginName($login) , "system group not found");
107  $this->assertEquals($login, $u->login);
108  $this->assertEquals($doc->id, $u->fid, "mismatch document igroup reference");
109  $this->assertEquals($doc->getRawValue("us_whatid") , $u->id, "mismatch system igroup reference");
110  return $doc;
111  }
112  /**
113  * @param string $userLogin
114  * @param array $groupLogins
115  * @---depends testCreateGroup
116  * @return \Account
117  * @dataProvider dataInsertgroupCreate
118  */
119  public function testInsertInGroup($userLogin, array $groupLogins)
120  {
121  // create user
122  $user = createDoc(self::$dbaccess, "IUSER");
123  $this->assertTrue(is_object($user) , "cannot create user");
124  $err = $user->setValue("us_login", $userLogin);
125  $password = 'secret';
126  $err.= $user->setValue("us_passwd1", $password);
127  $err.= $user->setValue("us_passwd2", $password);
128  $this->assertEmpty($err, sprintf("cannot set iuser %s", $err));
129 
130  $err = $user->store();
131  $this->assertEmpty($err, sprintf("cannot store iuser %s", $err));
132  // create group
133  $newGids = array();
134  foreach ($groupLogins as $gLogin) {
135  /**
136  * @var \_IGROUP $group
137  */
138  $group = createDoc(self::$dbaccess, "IGROUP");
139  $this->assertTrue(is_object($group) , "cannot create group");
140  $err = $group->setValue("us_login", $gLogin);
141  $this->assertEmpty($err, sprintf("cannot set igroup %s", $err));
142 
143  $err = $group->store();
144  $this->assertEmpty($err, sprintf("cannot store igroup %s", $err));
145  $group->insertDocument($user->initid);
146  $newGids[] = $group->getRawValue("us_whatid");
147  }
148  $u = new \Account("", $user->getRawValue("us_whatid"));
149  $this->assertTrue($u->isAffected());
150  $gids = $u->getGroupsId();
151 
152  $this->assertEmpty(array_diff($newGids, $gids) , "groups are not in new user");
153  }
154  /**
155  * @dataProvider dataNotgroupCreate
156  * @param string $login
157  */
158  public function testNotCreateGroup($login)
159  {
160 
161  $doc = createDoc(self::$dbaccess, "IGROUP");
162  $this->assertTrue(is_object($doc) , "cannot create group");
163  $err = $doc->setValue("us_login", $login);
164  $this->assertEmpty($err, sprintf("cannot set igroup %s", $err));
165 
166  $err = $doc->store();
167  $this->assertNotEmpty($err, sprintf("must be impossible to store igroup"));
168 
169  $u = new \Account();
170  $this->assertTrue($u->setLoginName($login) , "system group not found");
171  $this->assertEquals($login, $u->login);
172  }
173  /**
174  * @param $userId int|string The user performing the insertDocument()
175  * @param $groupId int|string The group on which insertDocument() is performed
176  * @param $insertUserId int|string The user inserted in the group
177  * @dataProvider data_userInsertDocument
178  */
179  public function test_userInsertDocument($userId, $groupId, $insertUserId)
180  {
181  /**
182  * @var \Dcp\Core\UserAccount $user
183  */
184  $user = new_Doc(self::$dbaccess, $userId, true);
185  if (!$user->isAlive()) {
186  $this->markTestIncomplete(sprintf("User with id '%s' is not alive.", $userId));
187  }
188  $userWhatId = $user->getRawValue('us_whatid');
189  $userAccount = new \Account(self::$dbaccess, $userWhatId);
190  /**
191  * @var \Dcp\Core\GroupAccount $group
192  */
193  $group = new_Doc(self::$dbaccess, $groupId, true);
194  if (!$group->isAlive()) {
195  $this->markTestIncomplete(sprintf("Group with id '%s' is not alive.", $groupId));
196  }
197  $groupWhatId = $group->getRawValue('us_whatid');
198  $groupAccount = new \Account(self::$dbaccess, $groupWhatId);
199  /**
200  * @var \Dcp\Core\UserAccount $insertUser
201  */
202  $insertUser = new_Doc(self::$dbaccess, $insertUserId);
203  if (!$insertUser->isAlive()) {
204  $this->markTestIncomplete(sprintf("User with id '%s' is not alive.", $insertUserId));
205  }
206  $insertUserWhatId = $insertUser->getRawValue('us_whatid');
207  $insertUserAccount = new \Account(self::$dbaccess, $insertUserWhatId);
208  /*
209  * Setuid to $userId
210  */
211  $this->sudo($user->getRawValue('us_login'));
212  /*
213  * Insert $insertUserId into $groupId
214  */
215  $group->insertDocument($insertUserId);
216  /*
217  * Check table groups
218  */
219  $groupsIdList = $insertUserAccount->getGroupsId();
220  $this->assertTrue(in_array($groupWhatId, $groupsIdList) , sprintf("User with id '%d' has not group with id '%d' as parent.", $insertUserWhatId, $groupWhatId));
221  /*
222  * Check table fld
223  */
224  $fld = $group->getContent();
225  $found = false;
226  foreach ($fld as & $doc) {
227  if ($doc['id'] == $insertUser->id) {
228  $found = true;
229  break;
230  }
231  }
232  unset($doc);
233  $this->assertTrue($found, sprintf("Group with '%d' does not contain inserted user with id '%d'.", $group->id, $insertUser->id));
234  /*
235  * Exit sudo
236  */
237  $this->exitSudo();
238  }
239 
240  public function datagroupWithUserDelete()
241  {
242  return array(
243  array(
244  "john.doc32",
245  array(
246  "patissier"
247  ) ,
248  array(
249  "patissier"
250  ) ,
251  array()
252  ) ,
253  array(
254  "john.doc32",
255  array(
256  "patissier",
257  "menuisier",
258  "charpentier"
259  ) ,
260  array(
261  "patissier"
262  ) ,
263  array(
264  "menuisier",
265  "charpentier"
266  )
267  ) ,
268  array(
269  "john.doc32",
270  array(
271  "patissier",
272  "menuisier",
273  "charpentier"
274  ) ,
275  array(
276  "menuisier",
277  "patissier"
278  ) ,
279  array(
280  "charpentier"
281  )
282  )
283  );
284  }
285 
286  public function datagroupCreate()
287  {
288  return array(
289  array(
290  "patissier"
291  ) ,
292  array(
293  "menuisier"
294  )
295  );
296  }
297 
298  public function dataInsertgroupCreate()
299  {
300  return array(
301  array(
302  "john.doc32",
303  array(
304  "patissier"
305  )
306  ) ,
307  array(
308  "john.doc32",
309  array(
310  "menuisier",
311  "charpentier"
312  )
313  )
314  );
315  }
316 
317  public function dataNotgroupCreate()
318  {
319  return array(
320  array(
321  "gadmin"
322  ) ,
323  array(
324  "all"
325  )
326  );
327  }
328  public function data_userInsertDocument()
329  {
330  return array(
331  array(
332  'U_1',
333  'G_1',
334  'U_2'
335  )
336  );
337  }
338 }
static sudo($login)
createDoc($dbaccess, $fromid, $control=true, $defaultvalues=true, $temporary=false)
$login
Definition: dav.php:40
new_Doc($dbaccess, $id= '', $latest=false)
static getCommonImportFile()
$dbaccess
Definition: checkVault.php:17
if($file) if($subject==""&&$file) if($subject=="") $err
← centre documentaire © anakeen