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
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.
12 * Copyright (C) 2004-2013 SugarCRM Inc. All rights reserved.
13 ********************************************************************************/
15 require_once('include/generic/LayoutManager.php');
16 require_once('include/generic/SugarWidgets/SugarWidgetFieldrelate.php');
21 * Dashlets | Adding relationships (Accounts, Contacts, custom modules) to dashlet filters do not work
25 class Bug52173Test extends Sugar_PHPUnit_Framework_TestCase
28 protected $account = null;
31 protected $contact1 = null;
34 protected $contact2 = null;
36 /** @var SugarWidgetFieldrelate */
37 protected $sugarWidget = null;
39 /** @var DynamicField */
42 /** @var RepairAndClear */
45 /** @var TemplateRelatedTextField */
46 protected $relateField = null;
48 /** @var string name */
49 protected $field_name_c = 'Bug58931_relateField';
51 /** @var LayoutManager */
52 protected $layoutManager = null;
54 public function setup()
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'));
63 $this->createCustomField();
64 $this->getSugarWidgetFieldRelate();
66 $this->account = SugarTestAccountUtilities::createAccount();
67 $this->contact1 = SugarTestContactUtilities::createContact();
68 $this->contact2 = SugarTestContactUtilities::createContact();
71 public function tearDown()
73 $this->relateField->delete($this->df);
74 $this->rc->repairAndClearAll(array("rebuildExtensions", "clearVardefs"), array("Contact"), false, false);
76 SugarTestAccountUtilities::removeAllCreatedAccounts();
77 SugarTestContactUtilities::removeAllCreatedContacts();
78 SugarTestHelper::tearDown();
82 * Create the custom field with type 'relate'
84 protected function createCustomField()
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;
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);
100 $this->rc = new RepairAndClear();
101 $this->rc->repairAndClearAll(array("rebuildExtensions", "clearVardefs"), array('Contact'), false, false);
105 * Create SugarWidget for relate field
107 public function getSugarWidgetFieldRelate()
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);
119 * Check correct execution of the query for Dashlets if filter contains default bean's relate field
122 public function testDefaultRelateField()
124 $this->contact2->account_id = $this->account->id;
125 $this->contact2->save();
126 $layoutDef = array( 'name' => 'account_name',
127 'id_name' => 'account_id',
129 'link' => 'accounts_contacts',
130 'table' => 'contacts',
131 'table_alias' => 'contacts',
132 'module' => 'Contacts',
133 'input_name0' => array( 0 => $this->account->id ),
135 $out = $this->sugarWidget->queryFilterone_of($layoutDef);
136 $this->assertContains($this->contact2->id, $out, 'The request for existing relate field was made incorrectly');
140 * Check correct execution of the query for Dashlets
141 * if filter contains default bean's relate field with same LHS and RHS modules
144 public function testDefaultRelateFieldForSameLHSAndRHSModules()
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',
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 ),
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');
162 * Check correct execution of the query for Dashlets if filter contains custom relate field
165 public function testCustomRelateFieldInDashlet()
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,
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 ),
180 $out = $this->sugarWidget->queryFilterone_of($layoutDef);
181 $this->assertContains($this->contact2->id, $out, 'The request for custom relate field was made incorrectly');