/** * @param CEvent $event */ public function processFromContactSide(CEvent $event) { $model = $event->sender; if (isset($model->originalAttributeValues['account'])) { if ($model->originalAttributeValues['account'][1] > 0) { //lookup to see if there is a 'primary' affiliation for old acc/con pairing and unmark as primary $accountContactAffiliations = AccountContactAffiliation::getPrimaryByAccountIdAndContactId((int) $model->originalAttributeValues['account'][1], (int) $model->id); //shouldn't be more than one, but if there is unset all of them foreach ($accountContactAffiliations as $accountContactAffiliation) { $accountContactAffiliation->primary = false; $accountContactAffiliation->save(); } } //lookup and see if there is an affiliation for the new acc/con pairing if ($model->account->id > 0) { $accountContactAffiliations = AccountContactAffiliation::getByAccountAndContact($model->account, $model); //Shouldn't be more than one, but if there is, just mark the first primary. if (count($accountContactAffiliations) > 0) { //If so - mark primary. $accountContactAffiliations[0]->primary = true; $accountContactAffiliations[0]->save(); } else { //If not, create and mark primary. $accountContactAffiliation = new AccountContactAffiliation(); $accountContactAffiliation->primary = true; $accountContactAffiliation->contact = $model; $accountContactAffiliation->account = $model->account; if ($accountContactAffiliation->isAttributeRequired('role') && $accountContactAffiliation->role->value == null) { $accountContactAffiliation->role->value = $this->resolveRoleValue($accountContactAffiliation); } $accountContactAffiliation->save(); } } } }
public static function getPrimaryByAccountIdAndContactId($accountId, $contactId) { assert('is_int($accountId)'); assert('is_int($contactId)'); $searchAttributeData = array(); $searchAttributeData['clauses'] = array(1 => array('attributeName' => 'account', 'operatorType' => 'equals', 'value' => $accountId), 2 => array('attributeName' => 'contact', 'operatorType' => 'equals', 'value' => $contactId)); $searchAttributeData['structure'] = '(1 and 2)'; $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('AccountContactAffiliation'); $where = RedBeanModelDataProvider::makeWhere('AccountContactAffiliation', $searchAttributeData, $joinTablesAdapter); $tableName = AccountContactAffiliation::getTableName(); $q = DatabaseCompatibilityUtil::getQuote(); $where .= " and {$q}{$tableName}{$q}.{$q}primary{$q} = '1'"; return self::getSubset($joinTablesAdapter, null, null, $where); }
public function testSimpleUserImportWhereAllRowsSucceed() { Yii::app()->user->userModel = User::getByUsername('super'); $account = AccountTestHelper::createAccountByNameForOwner('Account', Yii::app()->user->userModel); $contact = ContactTestHelper::createContactByNameForOwner('Contact', Yii::app()->user->userModel); $accountContactAffiliations = AccountContactAffiliation::getAll(); $this->assertEquals(0, count($accountContactAffiliations)); $import = new Import(); $serializedData['importRulesType'] = 'AccountContactAffiliations'; $serializedData['firstRowIsHeaderRow'] = true; $import->serializedData = serialize($serializedData); $this->assertTrue($import->save()); ImportTestHelper::createTempTableByFileNameAndTableName('importTest.csv', $import->getTempTableName(), true, Yii::getPathOfAlias('application.modules.accountContactAffiliations.tests.unit.files')); //update the ids of the account column to match the parent account. ZurmoRedBean::exec("update " . $import->getTempTableName() . " set column_2 = " . $account->id . " where id != 1 limit 4"); ZurmoRedBean::exec("update " . $import->getTempTableName() . " set column_1 = " . $contact->id . " where id != 1 limit 4"); $this->assertEquals(4, ImportDatabaseUtil::getCount($import->getTempTableName())); // includes header rows. $mappingData = array('column_0' => ImportMappingUtil::makeDropDownColumnMappingData('role'), 'column_1' => ImportMappingUtil::makeHasOneColumnMappingData('contact'), 'column_2' => ImportMappingUtil::makeHasOneColumnMappingData('account')); $importRules = ImportRulesUtil::makeImportRulesByType('AccountContactAffiliations'); $page = 0; $config = array('pagination' => array('pageSize' => 50)); //This way all rows are processed. $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config); $dataProvider->getPagination()->setCurrentPage($page); $importResultsUtil = new ImportResultsUtil($import); $messageLogger = new ImportMessageLogger(); ImportUtil::importByDataProvider($dataProvider, $importRules, $mappingData, $importResultsUtil, new ExplicitReadWriteModelPermissions(), $messageLogger); $importResultsUtil->processStatusAndMessagesForEachRow(); //Confirm that 3 models where created. $accountContactAffiliations = AccountContactAffiliation::getAll(); $this->assertEquals(3, count($accountContactAffiliations)); $this->assertEquals('0', $accountContactAffiliations[0]->primary); $this->assertTrue($accountContactAffiliations[0]->account->isSame($account)); $this->assertTrue($accountContactAffiliations[0]->contact->isSame($contact)); $this->assertEquals('', $accountContactAffiliations[0]->role->value); $this->assertEquals('0', $accountContactAffiliations[1]->primary); $this->assertTrue($accountContactAffiliations[1]->account->isSame($account)); $this->assertTrue($accountContactAffiliations[1]->contact->isSame($contact)); $this->assertEquals('Support', $accountContactAffiliations[1]->role->value); $this->assertEquals('0', $accountContactAffiliations[2]->primary); $this->assertTrue($accountContactAffiliations[2]->account->isSame($account)); $this->assertTrue($accountContactAffiliations[2]->contact->isSame($contact)); $this->assertEquals('Technical', $accountContactAffiliations[2]->role->value); //Confirm 3 rows were processed as 'created'. $this->assertEquals(3, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = " . ImportRowDataResultsUtil::CREATED)); //Confirm that 0 rows were processed as 'updated'. $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = " . ImportRowDataResultsUtil::UPDATED)); }
public function actionDelete($id) { $accountContactAffiliation = AccountContactAffiliation::getById(intval($id)); $accountContactAffiliation->delete(); //Do not redirect since there is no index view to go to. Also delete is called from related portlets only }
public function testSuperUserDeleteAction() { $super = $this->logoutCurrentUserLoginNewUserAndGetByUsername('super'); $accountContactAffiliations = AccountContactAffiliation::getAll(); $this->assertEquals(2, count($accountContactAffiliations)); //Delete an AccountContactAffiliation model. $this->setGetArray(array('id' => $accountContactAffiliations[0]->id)); $this->resetPostArray(); $this->runControllerWithNoExceptionsAndGetContent('accountContactAffiliations/default/delete', true); $accountContactAffiliations = AccountContactAffiliation::getAll(); $this->assertEquals(1, count($accountContactAffiliations)); }
public function testGetAffiliatedAccountsAndContacts() { $super = User::getByUsername('super'); Yii::app()->user->userModel = $super; $authenticationData = $this->login(); $headers = array('Accept: application/json', 'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'], 'ZURMO_TOKEN: ' . $authenticationData['token'], 'ZURMO_API_REQUEST_TYPE: REST'); $account = AccountTestHelper::createAccountByNameForOwner('firstAffAccount', Yii::app()->user->userModel); $account2 = AccountTestHelper::createAccountByNameForOwner('secondAffAccount', Yii::app()->user->userModel); $account3 = AccountTestHelper::createAccountByNameForOwner('thirdAffAccount', Yii::app()->user->userModel); $contact = ContactTestHelper::createContactByNameForOwner('firstAffContact', Yii::app()->user->userModel); $contact2 = ContactTestHelper::createContactByNameForOwner('secondAffContact', Yii::app()->user->userModel); $contact3 = ContactTestHelper::createContactByNameForOwner('thirdAffContact', Yii::app()->user->userModel); $accountContactAffiliation = new AccountContactAffiliation(); $accountContactAffiliation->account = $account; $accountContactAffiliation->contact = $contact; $this->assertTrue($accountContactAffiliation->save()); sleep(1); $accountContactAffiliation2 = new AccountContactAffiliation(); $accountContactAffiliation2->account = $account; $accountContactAffiliation2->contact = $contact2; $this->assertTrue($accountContactAffiliation2->save()); sleep(1); $accountContactAffiliation = new AccountContactAffiliation(); $accountContactAffiliation->account = $account2; $accountContactAffiliation->contact = $contact2; $this->assertTrue($accountContactAffiliation->save()); sleep(1); $accountContactAffiliation = new AccountContactAffiliation(); $accountContactAffiliation->account = $account3; $accountContactAffiliation->contact = $contact3; $this->assertTrue($accountContactAffiliation->save()); // Get all contacts affiliated with account $data = array('search' => array('modelClassName' => 'AccountContactAffiliation', 'searchAttributeData' => array('clauses' => array(1 => array('attributeName' => 'account', 'relatedAttributeName' => 'id', 'operatorType' => 'equals', 'value' => $account->id)), 'structure' => '1')), 'pagination' => array('page' => 1, 'pageSize' => 5), 'sort' => 'id asc'); $response = $this->createApiCallWithRelativeUrl('search/filter/', 'POST', $headers, array('data' => $data)); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); $this->assertEquals(2, $response['data']['totalCount']); $this->assertEquals(2, count($response['data']['items'])); $this->assertEquals($account->id, $response['data']['items'][0]['account']['id']); $this->assertEquals($contact->id, $response['data']['items'][0]['contact']['id']); $this->assertEquals($account->id, $response['data']['items'][1]['account']['id']); $this->assertEquals($contact2->id, $response['data']['items'][1]['contact']['id']); // Get all accounts affiliated with account $data = array('search' => array('modelClassName' => 'AccountContactAffiliation', 'searchAttributeData' => array('clauses' => array(1 => array('attributeName' => 'contact', 'relatedAttributeName' => 'id', 'operatorType' => 'equals', 'value' => $contact2->id)), 'structure' => '1')), 'pagination' => array('page' => 1, 'pageSize' => 5), 'sort' => 'id asc'); $response = $this->createApiCallWithRelativeUrl('search/filter/', 'POST', $headers, array('data' => $data)); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); $this->assertEquals(2, $response['data']['totalCount']); $this->assertEquals(2, count($response['data']['items'])); $this->assertEquals($account->id, $response['data']['items'][0]['account']['id']); $this->assertEquals($contact2->id, $response['data']['items'][0]['contact']['id']); $this->assertEquals($account2->id, $response['data']['items'][1]['account']['id']); $this->assertEquals($contact2->id, $response['data']['items'][1]['contact']['id']); }
/** * @depends testGetByAccountAndContact */ public function testGetPrimaryByAccountIdAndContactId() { $accountContactAffiliations = AccountContactAffiliation::getAll(); $this->assertEquals(1, count($accountContactAffiliations)); $accountContactAffiliations = AccountContactAffiliation::getPrimaryByAccountIdAndContactId($accountContactAffiliations[0]->account->id, $accountContactAffiliations[0]->contact->id); $this->assertEquals(0, count($accountContactAffiliations)); $accountContactAffiliations = AccountContactAffiliation::getAll(); $accountContactAffiliations[0]->primary = true; $this->assertTrue($accountContactAffiliations[0]->save()); $accountContactAffiliations = AccountContactAffiliation::getPrimaryByAccountIdAndContactId($accountContactAffiliations[0]->account->id, $accountContactAffiliations[0]->contact->id); $this->assertEquals(1, count($accountContactAffiliations)); }
public function testWhenRoleIsRequired() { $super = User::getByUsername('super'); Yii::app()->user->userModel = $super; //Set role as required but without a default value. $attributeForm = AttributesFormFactory::createAttributeFormByAttributeName(new AccountContactAffiliation(), 'role'); $attributeForm->isRequired = true; $modelAttributesAdapterClassName = $attributeForm::getModelAttributeAdapterNameForSavingAttributeFormData(); $adapter = new $modelAttributesAdapterClassName(new AccountContactAffiliation()); try { $adapter->setAttributeMetadataFromForm($attributeForm); } catch (FailedDatabaseSchemaChangeException $e) { echo $e->getMessage(); $this->fail(); } //Now create an affiliation, the role should be the first value. $this->assertEquals(0, count(AccountContactAffiliation::getAll())); $account = AccountTestHelper::createAccountByNameForOwner('thirdAccount', $super); $contact = ContactTestHelper::createContactWithAccountByNameForOwner('thirdContact', $super, $account); $accountContactAffiliations = AccountContactAffiliation::getAll(); $this->assertEquals(1, count($accountContactAffiliations)); $this->assertEquals(1, $accountContactAffiliations[0]->primary); $this->assertEquals('AAA', $accountContactAffiliations[0]->role->value); $this->assertTrue($accountContactAffiliations[0]->account->isSame($account)); $this->assertTrue($accountContactAffiliations[0]->contact->isSame($contact)); $this->assertTrue($contact->delete()); $accountContactAffiliations = AccountContactAffiliation::getAll(); $this->assertEquals(0, count($accountContactAffiliations)); //Now add a default value, so the role should be the default value. $attributeForm = AttributesFormFactory::createAttributeFormByAttributeName(new AccountContactAffiliation(), 'role'); $attributeForm->defaultValueOrder = 1; $modelAttributesAdapterClassName = $attributeForm::getModelAttributeAdapterNameForSavingAttributeFormData(); $adapter = new $modelAttributesAdapterClassName(new AccountContactAffiliation()); try { $adapter->setAttributeMetadataFromForm($attributeForm); } catch (FailedDatabaseSchemaChangeException $e) { echo $e->getMessage(); $this->fail(); } //Now create an account/contact and an affiliation. The role should be BBB $this->assertEquals(0, count(AccountContactAffiliation::getAll())); $account = AccountTestHelper::createAccountByNameForOwner('fourthAccount', $super); $contact = ContactTestHelper::createContactWithAccountByNameForOwner('fourthContact', $super, $account); $accountContactAffiliations = AccountContactAffiliation::getAll(); $this->assertEquals(1, count($accountContactAffiliations)); $this->assertEquals(1, $accountContactAffiliations[0]->primary); $this->assertEquals('BBB', $accountContactAffiliations[0]->role->value); $this->assertTrue($accountContactAffiliations[0]->account->isSame($account)); $this->assertTrue($accountContactAffiliations[0]->contact->isSame($contact)); $this->assertTrue($contact->delete()); $accountContactAffiliations = AccountContactAffiliation::getAll(); $this->assertEquals(0, count($accountContactAffiliations)); }
/** * @depends testSuperUserCustomFieldsWalkthroughForAccountContactAffiliationsModule */ public function testLayoutsLoadOkAfterCustomFieldsPlacedForAccountContactAffiliationsModule() { $super = $this->logoutCurrentUserLoginNewUserAndGetByUsername('super'); //Load create, edit views. $this->runControllerWithNoExceptionsAndGetContent('accountContactAffiliations/default/create'); $accountContactAffiliation = AccountContactAffiliation::getAll(); $this->assertEquals(0, count($accountContactAffiliation)); //Create a new AccountContactAffiliation from a related account. $this->setGetArray(array('relationAttributeName' => 'account', 'relationModelId' => self::$account->id, 'relationModuleId' => 'accounts', 'redirectUrl' => 'someRedirect')); $this->setPostArray(array('AccountContactAffiliation' => array('role' => array('value' => 'TechnicalX'), 'contact' => array('id' => self::$contact->id)))); $this->runControllerWithRedirectExceptionAndGetContent('accountContactAffiliations/default/createFromRelation'); $accountContactAffiliation = AccountContactAffiliation::getAll(); $this->assertEquals(1, count($accountContactAffiliation)); $this->setGetArray(array('id' => $accountContactAffiliation[0]->id)); $this->runControllerWithRedirectExceptionAndGetContent('accountContactAffiliations/default/edit'); }