2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
6 * This program 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 SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13 * This program 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
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
23 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26 * The interactive user interfaces in modified source and object code 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.
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 "Powered by
32 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33 * technical reasons, the Appropriate Legal Notices must display the words
34 * "Powered by SugarCRM".
35 ********************************************************************************/
38 require_once 'modules/ModuleBuilder/parsers/relationships/DeployedRelationships.php' ;
40 class SugarTestRelationshipUtilities
42 private static $_relsAdded = array();
44 protected static $_relRequiredKeys = array(
51 * Create a relationship
53 * Params should be passed in as this:
56 * 'relationship_type' => 'one-to-many',
57 * 'lhs_module' => 'Accounts',
58 * 'rhs_module' => 'Accounts',
62 * @param array $relationship_def
63 * @return ActivitiesRelationship|bool|ManyToManyRelationship|ManyToOneRelationship|OneToManyRelationship|OneToOneRelationship
65 public static function createRelationship(array $relationship_def)
68 if(!self::checkRequiredFields($relationship_def)) return false;
70 $relationships = new DeployedRelationships ($relationship_def['lhs_module']);
72 if(!isset($relationship_def['view_module'])) {
73 $relationship_def['view_module'] = $relationship_def['lhs_module'];
76 $REQUEST_Backup = $_REQUEST;
78 $_REQUEST = $relationship_def;
80 $relationship = $relationships->addFromPost();
81 $relationships->save();
82 $relationships->build();
83 LanguageManager::clearLanguageCache($relationship_def['lhs_module']);
85 SugarRelationshipFactory::rebuildCache();
86 // rebuild the dictionary to make sure that it has the new relationship in it
87 SugarTestHelper::setUp('dictionary');
88 // reset the link fields since we added one
89 VardefManager::$linkFields = array();
91 $_REQUEST = $REQUEST_Backup;
92 unset($REQUEST_Backup);
95 self::$_relsAdded[] = $relationship->getDefinition();
101 * Remove all created relationships
105 public static function removeAllCreatedRelationships()
107 foreach(self::$_relsAdded as $rel) {
109 $relationships = new DeployedRelationships($rel['lhs_module']);
111 $relationships->delete($rel['relationship_name']);
113 $relationships->save();
114 $relationships->build();
115 LanguageManager::clearLanguageCache($rel['lhs_module']);
116 require_once("data/Relationships/RelationshipFactory.php");
117 SugarRelationshipFactory::deleteCache();
119 SugarRelationshipFactory::rebuildCache();
121 // since we are creating a relationship we need to unset this global var
122 if(isset($GLOBALS['reload_vardefs'])) {
123 unset($GLOBALS['reload_vardefs']);
128 * Make sure we have at least the required keys
131 * @param array $relationship_def
134 protected static function checkRequiredFields(array $relationship_def)
136 foreach(self::$_relRequiredKeys as $key) {
137 if(!array_key_exists($key, $relationship_def)) {