PageRenderTime 30ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/app/protected/modules/zurmo/tests/unit/GroupUserMembershipFormUtilTest.php

https://bitbucket.org/zurmo/zurmo/
PHP | 189 lines | 141 code | 9 blank | 39 comment | 0 complexity | 11df20c9017c5f52cca4f2c77c515566 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, GPL-2.0, LGPL-3.0, LGPL-2.1, BSD-2-Clause
  1. <?php
  2. /*********************************************************************************
  3. * Zurmo is a customer relationship management program developed by
  4. * Zurmo, Inc. Copyright (C) 2015 Zurmo Inc.
  5. *
  6. * Zurmo is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License version 3 as published by the
  8. * Free Software Foundation with the addition of the following permission added
  9. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  10. * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
  11. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  12. *
  13. * Zurmo is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License along with
  19. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  20. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301 USA.
  22. *
  23. * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
  24. * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
  25. *
  26. * The interactive user interfaces in original and modified versions
  27. * of this program must display Appropriate Legal Notices, as required under
  28. * Section 5 of the GNU Affero General Public License version 3.
  29. *
  30. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  31. * these Appropriate Legal Notices must retain the display of the Zurmo
  32. * logo and Zurmo copyright notice. If the display of the logo is not reasonably
  33. * feasible for technical reasons, the Appropriate Legal Notices must display the words
  34. * "Copyright Zurmo Inc. 2015. All rights reserved".
  35. ********************************************************************************/
  36. class GroupUserMembershipFormUtilTest extends ZurmoBaseTest
  37. {
  38. public static function setUpBeforeClass()
  39. {
  40. parent::setUpBeforeClass();
  41. SecurityTestHelper::createSuperAdmin();
  42. }
  43. public function setUp()
  44. {
  45. parent::setUp();
  46. Yii::app()->user->userModel = User::getByUsername('super');
  47. }
  48. public function testMakeFormFromGroup()
  49. {
  50. $user = UserTestHelper::createBasicUser('Billy');
  51. $billId = $user->id;
  52. unset($user);
  53. $user = User::getById($billId);
  54. $this->assertEquals('billy', $user->username);
  55. $user = UserTestHelper::createBasicUser('Jimmy');
  56. $jimId = $user->id;
  57. unset($user);
  58. $user = User::getById($jimId);
  59. $this->assertEquals('jimmy', $user->username);
  60. $users = User::GetAll();
  61. $allUsers = array();
  62. foreach ($users as $user)
  63. {
  64. $allUsers[$user->id] = strval($user);
  65. }
  66. $this->assertEquals(3, count($allUsers));
  67. $a = new Group();
  68. $a->name = 'JJJ';
  69. $this->assertTrue($a->save());
  70. $this->assertEquals(0, $a->users ->count());
  71. $this->assertEquals(0, $a->groups->count());
  72. $form = GroupUserMembershipFormUtil::makeFormFromGroup($a);
  73. $this->assertEquals(array(), $form->userMembershipData);
  74. $this->assertEquals($allUsers, $form->userNonMembershipData);
  75. }
  76. /**
  77. * @depends testMakeFormFromGroup
  78. */
  79. public function testSetFormFromPostAndSetMembership()
  80. {
  81. $bill = User::getByUsername('billy');
  82. $jim = User::getByUsername('jimmy');
  83. $fakePostData = array(
  84. 'userMembershipData' => array(0 => $bill->id),
  85. 'userNonMembershipData' => array(0 => $jim->id)
  86. );
  87. $form = new GroupUserMembershipForm();
  88. $this->assertEmpty($form->userMembershipData);
  89. $this->assertEmpty($form->userNonMembershipData);
  90. $form = GroupUserMembershipFormUtil::setFormFromCastedPost($form, $fakePostData);
  91. $compare1 = array(
  92. $bill->id => strval($bill)
  93. );
  94. $this->assertEquals($compare1, $form->userMembershipData);
  95. $this->assertEquals(null, $form->userNonMembershipData);
  96. $group = Group::getByName('JJJ');
  97. $this->assertEquals('JJJ', $group->name);
  98. $saved = GroupUserMembershipFormUtil::setMembershipFromForm($form, $group);
  99. $this->assertTrue($saved);
  100. $group->forget();
  101. $group = Group::getByName('JJJ');
  102. $this->assertEquals(1, $group->users ->count());
  103. $this->assertEquals(0, $group->groups ->count());
  104. $fakePostData = array(
  105. 'userMembershipData' => array(0 => $bill->id, 1 => $jim->id),
  106. 'userNonMembershipData' => array(),
  107. );
  108. $form = new GroupUserMembershipForm();
  109. $this->assertEmpty($form->userMembershipData);
  110. $this->assertEmpty($form->userNonMembershipData);
  111. $form = GroupUserMembershipFormUtil::setFormFromCastedPost($form, $fakePostData);
  112. $compare1 = array(
  113. $bill->id => strval($bill),
  114. $jim->id => strval($jim)
  115. );
  116. $this->assertEquals($compare1, $form->userMembershipData);
  117. $group = Group::getByName('JJJ');
  118. $this->assertEquals('JJJ', $group->name);
  119. $saved = GroupUserMembershipFormUtil::setMembershipFromForm($form, $group);
  120. $this->assertTrue($saved);
  121. $group->forget();
  122. $group = Group::getByName('JJJ');
  123. $this->assertEquals(2, $group->users ->count());
  124. $this->assertEquals(0, $group->groups ->count());
  125. $fakePostData = array(
  126. 'userMembershipData' => array(),
  127. 'userNonMembershipData' => array(0 => $bill->id, 1 => $jim->id),
  128. );
  129. $form = new GroupUserMembershipForm();
  130. $this->assertEmpty($form->userMembershipData);
  131. $this->assertEmpty($form->userNonMembershipData);
  132. $form = GroupUserMembershipFormUtil::setFormFromCastedPost($form, $fakePostData);
  133. $compare1 = array();
  134. $this->assertEquals($compare1, $form->userMembershipData);
  135. $group = Group::getByName('JJJ');
  136. $this->assertEquals('JJJ', $group->name);
  137. $saved = GroupUserMembershipFormUtil::setMembershipFromForm($form, $group);
  138. $this->assertTrue($saved);
  139. $group->forget();
  140. $group = Group::getByName('JJJ');
  141. $this->assertEquals(0, $group->users ->count());
  142. $this->assertEquals(0, $group->groups ->count());
  143. }
  144. public function testValidateMembershipChange()
  145. {
  146. $bill = User::getByUsername('billy');
  147. $jim = User::getByUsername('jimmy');
  148. $fakePostData = array(
  149. 'userMembershipData' => array(0 => $bill->id),
  150. 'userNonMembershipData' => array(0 => $jim->id)
  151. );
  152. $form = new GroupUserMembershipForm();
  153. $this->assertEmpty($form->userMembershipData);
  154. $this->assertEmpty($form->userNonMembershipData);
  155. $bill->setIsSystemUser();
  156. $bill->firstName = 'Billy';
  157. $bill->lastName = 'Billium';
  158. $bill->save();
  159. $group = Group::getByName(Group::SUPER_ADMINISTRATORS_GROUP_NAME);
  160. $content = GroupUserMembershipFormUtil::validateMembershipChange($form, $group);
  161. $this->assertContains('There must be at', $content);
  162. $group->users->add($bill);
  163. $this->assertTrue($group->save());
  164. $form = GroupUserMembershipFormUtil::setFormFromCastedPost($form, $fakePostData);
  165. $content = GroupUserMembershipFormUtil::validateMembershipChange($form, $group);
  166. $this->assertNull($content);
  167. //Now add jimmy as a super user.
  168. $group->users->add($jim);
  169. $this->assertTrue($group->save());
  170. $fakePostData = array(
  171. 'userMembershipData' => array(0 => $bill->id, 1 => $jim->id)
  172. );
  173. $form = GroupUserMembershipFormUtil::setFormFromCastedPost($form, $fakePostData);
  174. //Now try to remove bill, it should pass ok validation because it won't really let you when it sets to form
  175. unset($form->userMembershipData[$bill->id]);
  176. $content = GroupUserMembershipFormUtil::validateMembershipChange($form, $group);
  177. $this->assertNull($content);
  178. }
  179. }
  180. ?>