All Data Structures Functions Variables Pages
AccountReadPermissionsOptimizationBaseTest.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo 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 ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo 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 Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified 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 Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
38  {
39  protected function rebuildAndTestThatTheMungeDoesntChange()
40  {
41  $beforeRows = ZurmoRedBean::getAll('select munge_id, securableitem_id, count from account_read order by munge_id, securableitem_id, count');
43  $afterRows = ZurmoRedBean::getAll('select munge_id, securableitem_id, count from account_read order by munge_id, securableitem_id, count');
44 
45  if ($beforeRows != $afterRows)
46  {
47  echo "Before and after rebuild munge doesn't match.\n";
48  echo "--------\n";
49  foreach ($beforeRows as $row)
50  {
51  echo join(', ', array_values($row)) . "\n";
52  }
53  echo "--------\n";
54  foreach ($afterRows as $row)
55  {
56  echo join(', ', array_values($row)) . "\n";
57  }
58  echo "--------\n";
59  }
60 
61  $this->assertEquals(count($beforeRows), count($afterRows));
62  $this->assertEquals($beforeRows, $afterRows);
63  }
64 
65  protected function nukeExistingAccounts()
66  {
67  while (true)
68  {
69  $accounts = Account::getSubset(null, 0, 50); // Nuke 50 at a time to
70  if (count($accounts) == 0) // avoid memory issues when
71  { // we get to the big numbers.
72  break;
73  }
74  foreach ($accounts as $account)
75  {
76  $account->delete();
77  unset($account);
78  }
79  unset($accounts);
80  }
81  $this->assertEquals(0, Account::getCount());
82  }
83 
84  protected function createAccounts($count, $testRebuildAfterCreateAccount = false, $firstAccount = 1, $step = 1)
85  {
86  $this->assertTrue(is_bool($testRebuildAfterCreateAccount));
87  $this->assertTrue($firstAccount <= $count);
88  $this->assertTrue($step < $count);
89  $betty = User::getByUsername('betty');
90  $benny = User::getByUsername('benny');
91  $salesStaff = Group::getByName('Sales Staff');
92  $countThatBennyCanRead = 0;
93  $accountIdsThatBennyCanRead = array();
94  $startTime = microtime(true);
95  for ($i = 0; $i < $count; $i++)
96  {
97  $bennyCanRead = false;
98  $account = self::createRandomAccount($i);
99  $securableItemId = $i + 1;
100  if ($i % 10 == 0)
101  {
102  $account->owner = $betty;
103  $bennyCanRead = true; // Because he is betty's manager.
104  }
105  if ($i % 8 == 0)
106  {
107  $account->addPermissions($benny, Permission::READ);
108  $bennyCanRead = true; // Just coz.
109  }
110  if ($i % 12 == 0)
111  {
112  $account->addPermissions($salesStaff, Permission::READ);
113  $bennyCanRead = true; // Benny is in Sales Staff.
114  }
115 
116  $this->assertTrue($account->save());
117  if ($bennyCanRead)
118  {
119  $countThatBennyCanRead++;
120  if ($countThatBennyCanRead <= 10)
121  {
122  $accountIdsThatBennyCanRead[] = $account->id;
123  }
124  }
125 
126  if ($i >= $firstAccount - 1 &&
127  ($i - ($firstAccount - 1)) % $step == 0 &&
128  $testRebuildAfterCreateAccount)
129  {
130  $startTime = microtime(true);
132  $endTime = microtime(true);
133  if ($this->isDebug())
134  {
135  echo 'Rebuilt the munge in php in ' . round($endTime - $startTime, 1) . ' seconds, ' . self::getAccountMungeRowCount() . " rows.\n";
136  }
137  $phpRows = self::getAccountMungeRows();
138 
139  // If $securityOptimized is false in debug.php the second one will just do the php again.
140  $startTime = microtime(true);
142  $endTime = microtime(true);
143  if ($this->isDebug())
144  {
145  echo 'Rebuilt the munge ' . (SECURITY_OPTIMIZED ? 'optimized' : 'in php') . ' in ' . round($endTime - $startTime, 1) . ' seconds, ' . self::getAccountMungeRowCount() . " rows.\n";
146  }
147  $otherRows = self::getAccountMungeRows();
148  if ($phpRows != $otherRows)
149  {
150  echo 'PHP & optimized munges don\'t match after account ' . ($i + 1) . "\n";
151  echo "--------\n";
152  foreach ($phpRows as $row)
153  {
154  echo join(', ', array_values($row)) . "\n";
155  }
156  echo "--------\n";
157  foreach ($otherRows as $row)
158  {
159  echo join(', ', array_values($row)) . "\n";
160  }
161  echo "--------\n";
162  }
163  $this->assertEquals(count($phpRows), count($otherRows));
164  $this->assertEquals($phpRows, $otherRows);
165  }
166  }
167  $endTime = microtime(true);
168 
169  return array($endTime - $startTime, $countThatBennyCanRead, $accountIdsThatBennyCanRead);
170  }
171 
172  protected static function createRandomAccount($i)
173  {
174  $account = new Account();
175  $account->name = "Account#$i";
176  $account->officePhone = rand(10000000, 90000000);
177  $account->officeFax = rand(10000000, 90000000);
178  $account->employees = rand(1, 100);
179  $account->website = "http://www.account$i.com";
180  $account->annualRevenue = rand(10000, 10000000);
181  $account->description = "An account for some company called Account#$i.";
182  $account->primaryEmail->emailAddress = "info@account$i.com";
183  $account->primaryEmail->optOut = false;
184  $account->primaryEmail->isInvalid = false;
185  $account->billingAddress->street1 = "$i Some St";
186  $account->billingAddress->city = 'City';
187  $account->billingAddress->state = 'State';
188  $account->billingAddress->postalCode = rand(1000, 9999);
189  return $account;
190  }
191 
192  protected static function accountMungeDoesntChangeWhenRebuilt()
193  {
194  //Need to forget all since sometimes the related information is cached from
195  //before something occurred during a test.
198  $beforeRows = self::getAccountMungeRows();
200  $afterRows = self::getAccountMungeRows();
201 
202  if ($beforeRows != $afterRows)
203  {
204  echo "Before and after rebuild munge doesn't match.\n";
205  self::echoRows($beforeRows);
206  self::echoRows($afterRows);
207  }
208 
209  return $beforeRows == $afterRows;
210  }
211 
212  public static function getAccountMungeRows(SecurableItem $securableItem = null)
213  {
214  if ($securableItem === null)
215  {
216  $rows = ZurmoRedBean::getAll('select name, munge_id, count
217  from account_read, ownedsecurableitem, account
218  where account_read.securableitem_id = ownedsecurableitem.securableitem_id and
219  ownedsecurableitem.id = account.ownedsecurableitem_id
220  order by name, munge_id, account_read.securableitem_id, count');
221  }
222  else
223  {
224  $securableItemId = $securableItem->getClassId('SecurableItem');
225  $rows = ZurmoRedBean::getAll("select munge_id, count
226  from account_read
227  where securableitem_id = $securableItemId
228  order by munge_id, count");
229  }
230  $rowsWithValues = array();
231  foreach ($rows as $row)
232  {
233  $row = array_values($row);
234  array_walk($row, array('self', 'stripFullStops'));
235  $rowsWithValues[] = $row;
236  }
237  return $rowsWithValues;
238  }
239 
240  protected static function stripFullStops(&$value, $index)
241  {
242  // The names the accounts have . to pad them out
243  // to the minimum length they require, which is 3.
244  // This is to make them appear in the debug output
245  // as they do in the munge scenarios powerpoint
246  // slides.
247  $value = str_replace('.', '', $value);
248  }
249 
250  protected static function echoAccountMungeRows(SecurableItem $securableItem = null)
251  {
252  self::echoRows(self::getAccountMungeRows($securableItem));
253  }
254 
255  protected static function echoRows(array $rows)
256  {
257  echo "--------\n";
258  foreach ($rows as $row)
259  {
260  echo join(', ', array_values($row)) . "\n";
261  }
262  echo "--------\n";
263  }
264 
265  protected static function getAccountMungeRowCount()
266  {
267  return intval(ZurmoRedBean::getCell('select count(*) from account_read'));
268  }
269  }
270 ?>
static getByUsername($username)
Definition: User.php:49
static forgetAll($onlyForgetPhpCache=false)
static forgetAll()
static rebuild($overwriteExistingTables=true, $forcePhp=false, $messageStreamer=null)
static getSubset(RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter=null, $offset=null, $count=null, $where=null, $orderBy=null, $modelClassName=null, $selectDistinct=false)
static getByName($name)
Definition: Group.php:57
static getCount(RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter=null, $where=null, $modelClassName=null, $selectDistinct=false)
Generated on Sat Jun 6 2020 07:10:29