]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/SugarTestRelationshipUtilities.php
Release 6.5.5
[Github/sugarcrm.git] / tests / SugarTestRelationshipUtilities.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
5  * 
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.
12  * 
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
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 SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
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.
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 "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  ********************************************************************************/
36
37
38 require_once 'modules/ModuleBuilder/parsers/relationships/DeployedRelationships.php' ;
39
40 class SugarTestRelationshipUtilities
41 {
42     private static $_relsAdded = array();
43
44     protected static $_relRequiredKeys = array(
45         'relationship_type',
46         'lhs_module',
47         'rhs_module',
48     );
49
50     /**
51      * Create a relationship
52      *
53      * Params should be passed in as this:
54      *
55      * array(
56      *       'relationship_type' => 'one-to-many',
57      *       'lhs_module' => 'Accounts',
58      *       'rhs_module' => 'Accounts',
59      *   )
60      *
61      * @static
62      * @param array $relationship_def
63      * @return ActivitiesRelationship|bool|ManyToManyRelationship|ManyToOneRelationship|OneToManyRelationship|OneToOneRelationship
64      */
65     public static function createRelationship(array $relationship_def)
66     {
67
68         if(!self::checkRequiredFields($relationship_def)) return false;
69
70         $relationships = new DeployedRelationships ($relationship_def['lhs_module']);
71
72         if(!isset($relationship_def['view_module'])) {
73             $relationship_def['view_module'] = $relationship_def['lhs_module'];
74         }
75
76         $REQUEST_Backup = $_REQUEST;
77
78         $_REQUEST = $relationship_def;
79
80         $relationship = $relationships->addFromPost();
81         $relationships->save();
82         $relationships->build();
83         LanguageManager::clearLanguageCache($relationship_def['lhs_module']);
84
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();
90
91         $_REQUEST = $REQUEST_Backup;
92         unset($REQUEST_Backup);
93
94
95         self::$_relsAdded[] = $relationship->getDefinition();
96
97         return $relationship;
98     }
99
100     /**
101      * Remove all created relationships
102      *
103      * @static
104      */
105     public static function removeAllCreatedRelationships()
106     {
107         foreach(self::$_relsAdded as $rel) {
108
109             $relationships = new DeployedRelationships($rel['lhs_module']);
110
111             $relationships->delete($rel['relationship_name']);
112
113             $relationships->save();
114             $relationships->build();
115             LanguageManager::clearLanguageCache($rel['lhs_module']);
116             require_once("data/Relationships/RelationshipFactory.php");
117             SugarRelationshipFactory::deleteCache();
118
119             SugarRelationshipFactory::rebuildCache();
120         }
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']);
124         }
125     }
126
127     /**
128      * Make sure we have at least the required keys
129      *
130      * @static
131      * @param array $relationship_def
132      * @return bool
133      */
134     protected static function checkRequiredFields(array $relationship_def)
135     {
136         foreach(self::$_relRequiredKeys as $key) {
137             if(!array_key_exists($key, $relationship_def)) {
138                 return false;
139             }
140         }
141
142         return true;
143     }
144
145 }
146 ?>