]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/Dashlets/Bug52173Test.php
Release 6.5.12
[Github/sugarcrm.git] / tests / include / Dashlets / Bug52173Test.php
1 <?php
2 /*********************************************************************************
3  * By installing or using this file, you are confirming on behalf of the entity
4  * subscribed to the SugarCRM Inc. product ("Company") that Company is bound by
5  * the SugarCRM Inc. Master Subscription Agreement ("MSA"), which is viewable at:
6  * http://www.sugarcrm.com/master-subscription-agreement
7  *
8  * If Company is not bound by the MSA, then by installing or using this file
9  * you are agreeing unconditionally that Company will be bound by the MSA and
10  * certifying that you have authority to bind Company accordingly.
11  *
12  * Copyright (C) 2004-2013 SugarCRM Inc.  All rights reserved.
13  ********************************************************************************/
14
15 require_once('include/generic/LayoutManager.php');
16 require_once('include/generic/SugarWidgets/SugarWidgetFieldrelate.php');
17
18 /**
19  * Bug #52173
20  *
21  * Dashlets | Adding relationships (Accounts, Contacts, custom modules) to dashlet filters do not work
22  * @ticket 52173
23  */
24
25 class Bug52173Test extends Sugar_PHPUnit_Framework_TestCase
26 {
27     /** @var Account */
28     protected $account = null;
29
30     /** @var Contact */
31     protected $contact1 = null;
32
33     /** @var Contact */
34     protected $contact2 = null;
35
36     /** @var SugarWidgetFieldrelate */
37     protected $sugarWidget = null;
38
39     /** @var DynamicField */
40     protected $df = null;
41
42     /** @var RepairAndClear */
43     protected $rc = null;
44
45     /** @var TemplateRelatedTextField */
46     protected $relateField = null;
47
48     /** @var string name */
49     protected $field_name_c = 'Bug58931_relateField';
50
51     /** @var LayoutManager */
52     protected $layoutManager = null;
53
54     public function setup()
55     {
56         SugarTestHelper::setUp('beanList');
57         SugarTestHelper::setUp('beanFiles');
58         SugarTestHelper::setUp('current_user', array(true, 1));
59         SugarTestHelper::setUp('app_list_strings');
60         SugarTestHelper::setUp('mod_strings', array('ModuleBuilder'));
61         parent::setUp();
62
63         $this->createCustomField();
64         $this->getSugarWidgetFieldRelate();
65
66         $this->account = SugarTestAccountUtilities::createAccount();
67         $this->contact1 = SugarTestContactUtilities::createContact();
68         $this->contact2 = SugarTestContactUtilities::createContact();
69     }
70
71     public function tearDown()
72     {
73         $this->relateField->delete($this->df);
74         $this->rc->repairAndClearAll(array("rebuildExtensions", "clearVardefs"), array("Contact"),  false, false);
75
76         SugarTestAccountUtilities::removeAllCreatedAccounts();
77         SugarTestContactUtilities::removeAllCreatedContacts();
78         SugarTestHelper::tearDown();
79     }
80
81     /**
82      * Create the custom field with type 'relate'
83      */
84     protected function createCustomField()
85     {
86         $field = get_widget('relate');
87         $field->id = 'Contacts'. $this->field_name_c;
88         $field->name = $this->field_name_c;
89         $field->type = 'relate';
90         $field->label = 'LBL_' . strtoupper($this->field_name_c);
91         $field->ext2 = 'Accounts';
92         $field->view_module = 'Contacts';
93         $this->relateField = $field;
94
95         $this->bean =BeanFactory::getBean('Contacts');
96         $this->df = new DynamicField($this->bean->module_name);
97         $this->df->setup($this->bean);
98         $field->save($this->df);
99
100         $this->rc = new RepairAndClear();
101         $this->rc->repairAndClearAll(array("rebuildExtensions", "clearVardefs"), array('Contact'),  false, false);
102     }
103
104     /**
105      * Create SugarWidget for relate field
106      */
107     public function getSugarWidgetFieldRelate()
108     {
109         $layoutManager = new LayoutManager();
110         $layoutManager->setAttribute('context', 'Report');
111         $db = new stdClass();
112         $db->db = $GLOBALS['db'];
113         $db->report_def_str = '';
114         $layoutManager->setAttributePtr('reporter', $db);
115         $this->sugarWidget = new SugarWidgetFieldrelate($layoutManager);
116     }
117
118     /*
119     * Check correct execution of the query for Dashlets if filter contains default bean's relate field
120     * @return void
121     */
122     public function testDefaultRelateField()
123     {
124         $this->contact2->account_id = $this->account->id;
125         $this->contact2->save();
126         $layoutDef = array( 'name'        => 'account_name',
127                             'id_name'     => 'account_id',
128                             'type'        => 'relate',
129                             'link'        => 'accounts_contacts',
130                             'table'       => 'contacts',
131                             'table_alias' => 'contacts',
132                             'module'      => 'Contacts',
133                             'input_name0' => array( 0 => $this->account->id ),
134         );
135         $out = $this->sugarWidget->queryFilterone_of($layoutDef);
136         $this->assertContains($this->contact2->id, $out, 'The request for existing relate field was made incorrectly');
137     }
138
139     /*
140     * Check correct execution of the query for Dashlets
141     * if filter contains default bean's relate field with same LHS and RHS modules
142     * @return void
143     */
144     public function testDefaultRelateFieldForSameLHSAndRHSModules()
145     {
146         $this->contact2->reports_to_id = $this->contact1->id;
147         $this->contact2->save();
148         $layoutDef = array( 'name'        => 'report_to_name',
149                             'id_name'     => 'reports_to_id',
150                             'type'        => 'relate',
151                             'link'        => 'contact_direct_reports',
152                             'table'       => 'contacts',
153                             'table_alias' => 'contacts',
154                             'module'      => 'Contacts',
155                             'input_name0' => array( 0 => $this->contact2->reports_to_id ),
156         );
157         $out = $this->sugarWidget->queryFilterone_of($layoutDef);
158         $this->assertContains($this->contact2->id, $out, 'The request for existing relate field which has same LHS and RHS modules was made incorrectly');
159     }
160
161     /*
162     * Check correct execution of the query for Dashlets if filter contains custom relate field
163     * @return void
164     */
165     public function testCustomRelateFieldInDashlet()
166     {
167         $id = $this->relateField->ext3;
168         $this->contact2->$id = $this->account->id;
169         $this->contact2->save();
170         $layoutDef = array( 'name'          => $this->relateField->name,
171                             'id_name'       => $this->relateField->ext3,
172                             'type'          => 'relate',
173                             'ext2'          => 'Accounts',
174                             'custom_module' => 'Contacts',
175                             'table'         => 'contacts_cstm',
176                             'table_alias'   => 'contacts',
177                             'module'        => 'Accounts',
178                             'input_name0'   => array( 0 => $this->account->id ),
179         );
180         $out = $this->sugarWidget->queryFilterone_of($layoutDef);
181         $this->assertContains($this->contact2->id, $out, 'The request for custom relate field was made incorrectly');
182     }
183 }