]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/service/APIv3Helper.php
Release 6.4.0
[Github/sugarcrm.git] / tests / service / APIv3Helper.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 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 //Helper functions used by both SOAP and REST Unit Test Calls.
38
39 class APIv3Helper
40 {
41
42     function populateSeedDataForSearchTest($user_id)
43     {
44         $results = array();
45         $a1_id = create_guid();
46         $a1 = new Account();
47         $a1->id = $a1_id;
48         $a1->new_with_id = TRUE;
49         $a1->name = "UNIT TEST $a1_id";
50         $a1->assigned_user_id = $user_id;
51         $a1->save();
52         $results[] = array('id' => $a1_id, 'fieldName' => 'name', 'fieldValue' => "UNIT TEST $a1_id");
53
54         $a2_id = create_guid();
55         $a2 = new Account();
56         $a2->new_with_id = TRUE;
57         $a2->id = $a2_id;
58         $a2->name = "UNIT TEST $a2_id";
59         $a2->assigned_user_id = 'unittest';
60         $a2->save();
61         $results[] = array('id' => $a2_id, 'fieldName' => 'name', 'fieldValue' => "UNIT TEST $a2_id");
62
63         $c1_id = create_guid();
64         $c1 = new Contact();
65         $c1->id = $c1_id;
66         $c1->new_with_id = TRUE;
67         $c1->first_name = "UNIT TEST";
68         $c1->last_name = "UNIT_TEST";
69         $c1->assigned_user_id = $user_id;
70         $c1->save();
71         $results[] = array('id' => $c1_id, 'fieldName' => 'name', 'fieldValue' => $c1->first_name .' ' . $c1->last_name);
72
73         $op1_id = create_guid();
74         $op1 = new Opportunity();
75         $op1->new_with_id = TRUE;
76         $op1->id = $op1_id;
77         $op1->name = "UNIT TEST $op1_id";
78         $op1->assigned_user_id = $user_id;
79         $op1->save();
80         $results[] = array('id' => $op1_id, 'fieldName' => 'name', 'fieldValue' => "UNIT TEST $op1_id");
81
82         $op2_id = create_guid();
83         $op2 = new Opportunity();
84         $op2->new_with_id = TRUE;
85         $op2->id = $op2_id;
86         $op2->name = "UNIT TEST $op2_id";
87         $op2->assigned_user_id = 'unittest';
88         $op2->save();
89         $results[] = array('id' => $op2_id, 'fieldName' => 'name', 'fieldValue' => "UNIT TEST $op2_id");
90         $GLOBALS['db']->commit();
91         return $results;
92     }
93
94     /**
95      * Linear search function used to find a bean id in an entry list array.
96      *
97      * @param array $list
98      * @param string $bean_id
99      */
100     function findBeanIdFromEntryList($list,$bean_id,$module)
101     {
102         $found = FALSE;
103         foreach ($list as $moduleEntry)
104         {
105             if($moduleEntry['name'] == $module)
106             {
107                 foreach ($moduleEntry['records'] as $entry)
108                 {
109                     foreach ($entry as $fieldEntry)
110                     {
111                         if($fieldEntry['name'] == 'id' && $fieldEntry['value'] == $bean_id )
112                             return TRUE;
113                     }
114                 }
115             }
116         }
117
118         return $found;
119     }
120
121     /**
122      * Linear search function used to find a particular field in an entry list array.
123      *
124      * @param array $list
125      * @param string $bean_id
126      */
127     function findFieldByNameFromEntryList($list,$bean_id,$module,$fieldName)
128     {
129         $found = FALSE;
130
131         foreach ($list as $moduleEntry)
132         {
133             if($moduleEntry['name'] == $module)
134             {
135                 foreach ($moduleEntry['records'] as $entry)
136                 {
137                     $value = $this->_retrieveFieldValueByFieldName($entry, $fieldName,$bean_id);
138                     if($value !== FALSE)
139                         return $value;
140                 }
141             }
142         }
143
144         return $found;
145     }
146
147     function _retrieveFieldValueByFieldName($entry, $fieldName, $beanId)
148     {
149         $found = FALSE;
150         $fieldValue = FALSE;
151         foreach ($entry as $fieldEntry)
152         {
153             if($fieldEntry['name'] == 'id' && $fieldEntry['value'] == $beanId )
154                 $found = TRUE;
155
156             if($fieldEntry['name'] == $fieldName )
157                 $fieldValue = $fieldEntry['value'];
158         }
159
160         if($found)
161             return $fieldValue;
162         else
163             return FALSE;
164     }
165 }