Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
AccountContactAffiliationObserverTest.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 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. 2017. All rights reserved".
35  ********************************************************************************/
36 
38  {
39  public static function setUpBeforeClass()
40  {
41  parent::setUpBeforeClass();
42  SecurityTestHelper::createSuperAdmin();
44 
45  $values = array(
46  'AAA',
47  'BBB',
48  'CCC',
49  );
50  $typeFieldData = CustomFieldData::getByName('AccountContactAffiliationRoles');
51  $typeFieldData->serializedData = serialize($values);
52  if (!$typeFieldData->save())
53  {
54  throw new FailedToSaveModelException();
55  }
56  }
57 
58  public function testChangingContactOnAccountFromContactSideThatObservationTakesPlace()
59  {
60  $super = User::getByUsername('super');
61  Yii::app()->user->userModel = $super;
62  $this->assertEquals(0, count(AccountContactAffiliation::getAll()));
63  $account = AccountTestHelper::createAccountByNameForOwner('firstAccount', $super);
64  $this->assertEquals(0, count(AccountContactAffiliation::getAll()));
65  $contact2 = ContactTestHelper::createContactByNameForOwner('secondContact', $super);
66  $this->assertEquals(0, count(AccountContactAffiliation::getAll()));
67  $contact = ContactTestHelper::createContactWithAccountByNameForOwner('firstContact', $super, $account);
68  $accountContactAffiliations = AccountContactAffiliation::getAll();
69  $this->assertEquals(1, count($accountContactAffiliations));
70  $this->assertEquals(1, $accountContactAffiliations[0]->primary);
71  $this->assertTrue($accountContactAffiliations[0]->account->isSame($account));
72  $this->assertTrue($accountContactAffiliations[0]->contact->isSame($contact));
73 
74  //Now make a second account and add the first contact to it. This would switch the contact->account to account2
75  $account2 = AccountTestHelper::createAccountByNameForOwner('secondAccount', $super);
76  $account2->contacts->add($contact);
77  $this->assertTrue($account2->contacts->contains($contact));
78  //still should be the same affiliation until we save
79  $accountContactAffiliations = AccountContactAffiliation::getAll();
80  $this->assertEquals(1, count($accountContactAffiliations));
81  $this->assertEquals(1, $accountContactAffiliations[0]->primary);
82  $this->assertEmpty($accountContactAffiliations[0]->role->value);
83  $this->assertTrue($accountContactAffiliations[0]->account->isSame($account));
84  $this->assertTrue($accountContactAffiliations[0]->contact->isSame($contact));
85 
86  //Now save
87  $this->assertTrue ($account2->save());
88  $this->assertTrue ($account2->contacts->contains($contact));
89  $this->assertFalse($account->contacts->contains($contact));
90  //The old affiliation should be there but not primary anymore
91  $accountContactAffiliations = AccountContactAffiliation::getAll();
92  $this->assertEquals(2, count($accountContactAffiliations));
93  $this->assertEquals(0, $accountContactAffiliations[0]->primary);
94  $this->assertEmpty($accountContactAffiliations[0]->role->value);
95  $this->assertTrue($accountContactAffiliations[0]->account->isSame($account));
96  $this->assertTrue($accountContactAffiliations[0]->contact->isSame($contact));
97  $this->assertEquals(1, $accountContactAffiliations[1]->primary);
98  $this->assertEmpty($accountContactAffiliations[1]->role->value);
99  $this->assertTrue($accountContactAffiliations[1]->account->isSame($account2));
100  $this->assertTrue($accountContactAffiliations[1]->contact->isSame($contact));
101 
102  //Now test removing the contact from the second account
103  $account2->contacts->remove($contact);
104  $this->assertTrue($account2->save());
105  $this->assertTrue($contact->account->id < 0);
106  //Both affiliations exist, but there is no longer a primary affiliation
107  $accountContactAffiliations = AccountContactAffiliation::getAll();
108  $this->assertEquals(2, count($accountContactAffiliations));
109  $this->assertEquals(0, $accountContactAffiliations[0]->primary);
110  $this->assertTrue($accountContactAffiliations[0]->account->isSame($account));
111  $this->assertTrue($accountContactAffiliations[0]->contact->isSame($contact));
112  $this->assertEquals(0, $accountContactAffiliations[1]->primary);
113  $this->assertTrue($accountContactAffiliations[1]->account->isSame($account2));
114  $this->assertTrue($accountContactAffiliations[1]->contact->isSame($contact));
115 
116  //Contact is no longer connected to either account at this point.
117  $this->assertFalse($account->contacts->contains($contact));
118  $this->assertFalse($account2->contacts->contains($contact));
119 
120  //Now set the account2 as the primary again
121  $account2->contacts->add($contact);
122  $this->assertTrue ($account2->save());
123  //Now the account2 is the primary again
124  $accountContactAffiliations = AccountContactAffiliation::getAll();
125  $this->assertEquals(2, count($accountContactAffiliations));
126  $this->assertEquals(0, $accountContactAffiliations[0]->primary);
127  $this->assertTrue($accountContactAffiliations[0]->account->isSame($account));
128  $this->assertTrue($accountContactAffiliations[0]->contact->isSame($contact));
129  $this->assertEquals(1, $accountContactAffiliations[1]->primary);
130  $this->assertTrue($accountContactAffiliations[1]->account->isSame($account2));
131  $this->assertTrue($accountContactAffiliations[1]->contact->isSame($contact));
132 
133  //Now set the account as the primary but from the contact side
134  $contact->account = $account;
135  $this->assertTrue ($contact->save());
136  //Now account is primary again
137  $accountContactAffiliations = AccountContactAffiliation::getAll();
138  $this->assertEquals(2, count($accountContactAffiliations));
139  $this->assertEquals(1, $accountContactAffiliations[0]->primary);
140  $this->assertTrue($accountContactAffiliations[0]->account->isSame($account));
141  $this->assertTrue($accountContactAffiliations[0]->contact->isSame($contact));
142  $this->assertEquals(0, $accountContactAffiliations[1]->primary);
143  $this->assertTrue($accountContactAffiliations[1]->account->isSame($account2));
144  $this->assertTrue($accountContactAffiliations[1]->contact->isSame($contact));
145 
146  //Delete account, it should remove one of the affiliations
147  //Refresh account to properly grab related affiliations in order to delete 'owned' relations
148  $accountId = $account->id;
149  $account->forget();
150  $account = Account::getById($accountId);
151  $this->assertEquals(1, $account->contactAffiliations->count());
152  $this->assertTrue($account->delete());
153  $accountContactAffiliations = AccountContactAffiliation::getAll();
154  $this->assertEquals(1, count($accountContactAffiliations));
155 
156  //Now delete from the contact side
157  $contactId = $contact->id;
158  $contact->forget();
159  $contact = Contact::getById($contactId);
160  $this->assertEquals(1, $contact->accountAffiliations->count());
161  $this->assertTrue($contact->delete());
162  $accountContactAffiliations = AccountContactAffiliation::getAll();
163  $this->assertEquals(0, count($accountContactAffiliations));
164  }
165 
166  public function testWhenRoleIsRequired()
167  {
168  $super = User::getByUsername('super');
169  Yii::app()->user->userModel = $super;
170 
171  //Set role as required but without a default value.
172  $attributeForm = AttributesFormFactory::
173  createAttributeFormByAttributeName(new AccountContactAffiliation(), 'role');
174  $attributeForm->isRequired = true;
175  $modelAttributesAdapterClassName = $attributeForm::getModelAttributeAdapterNameForSavingAttributeFormData();
176  $adapter = new $modelAttributesAdapterClassName(new AccountContactAffiliation());
177  try
178  {
179  $adapter->setAttributeMetadataFromForm($attributeForm);
180  }
182  {
183  echo $e->getMessage();
184  $this->fail();
185  }
186  //Now create an affiliation, the role should be the first value.
187  $this->assertEquals(0, count(AccountContactAffiliation::getAll()));
188  $account = AccountTestHelper::createAccountByNameForOwner('thirdAccount', $super);
189  $contact = ContactTestHelper::createContactWithAccountByNameForOwner('thirdContact', $super, $account);
190  $accountContactAffiliations = AccountContactAffiliation::getAll();
191  $this->assertEquals(1, count($accountContactAffiliations));
192  $this->assertEquals(1, $accountContactAffiliations[0]->primary);
193  $this->assertEquals('AAA', $accountContactAffiliations[0]->role->value);
194  $this->assertTrue($accountContactAffiliations[0]->account->isSame($account));
195  $this->assertTrue($accountContactAffiliations[0]->contact->isSame($contact));
196  $this->assertTrue($contact->delete());
197  $accountContactAffiliations = AccountContactAffiliation::getAll();
198  $this->assertEquals(0, count($accountContactAffiliations));
199 
200  //Now add a default value, so the role should be the default value.
201  $attributeForm = AttributesFormFactory::
202  createAttributeFormByAttributeName(new AccountContactAffiliation(), 'role');
203  $attributeForm->defaultValueOrder = 1;
204  $modelAttributesAdapterClassName = $attributeForm::getModelAttributeAdapterNameForSavingAttributeFormData();
205  $adapter = new $modelAttributesAdapterClassName(new AccountContactAffiliation());
206  try
207  {
208  $adapter->setAttributeMetadataFromForm($attributeForm);
209  }
211  {
212  echo $e->getMessage();
213  $this->fail();
214  }
215  //Now create an account/contact and an affiliation. The role should be BBB
216  $this->assertEquals(0, count(AccountContactAffiliation::getAll()));
217  $account = AccountTestHelper::createAccountByNameForOwner('fourthAccount', $super);
218  $contact = ContactTestHelper::createContactWithAccountByNameForOwner('fourthContact', $super, $account);
219  $accountContactAffiliations = AccountContactAffiliation::getAll();
220  $this->assertEquals(1, count($accountContactAffiliations));
221  $this->assertEquals(1, $accountContactAffiliations[0]->primary);
222  $this->assertEquals('BBB', $accountContactAffiliations[0]->role->value);
223  $this->assertTrue($accountContactAffiliations[0]->account->isSame($account));
224  $this->assertTrue($accountContactAffiliations[0]->contact->isSame($contact));
225  $this->assertTrue($contact->delete());
226  $accountContactAffiliations = AccountContactAffiliation::getAll();
227  $this->assertEquals(0, count($accountContactAffiliations));
228  }
229  }
230 ?>
static getByUsername($username)
Definition: User.php:49
static loadStartingData()
static getByName($name, $shouldCache=true)
static getById($id, $modelClassName=null)
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null)
Generated on Sat Jul 4 2020 07:10:29
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.