39 protected function rebuildAndTestThatTheMungeDoesntChange()
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');
45 if ($beforeRows != $afterRows)
47 echo
"Before and after rebuild munge doesn't match.\n";
49 foreach ($beforeRows as $row)
51 echo join(
', ', array_values($row)) .
"\n";
54 foreach ($afterRows as $row)
56 echo join(
', ', array_values($row)) .
"\n";
61 $this->assertEquals(count($beforeRows), count($afterRows));
62 $this->assertEquals($beforeRows, $afterRows);
65 protected function nukeExistingAccounts()
70 if (count($accounts) == 0)
74 foreach ($accounts as $account)
84 protected function createAccounts($count, $testRebuildAfterCreateAccount =
false, $firstAccount = 1, $step = 1)
86 $this->assertTrue(is_bool($testRebuildAfterCreateAccount));
87 $this->assertTrue($firstAccount <= $count);
88 $this->assertTrue($step < $count);
92 $countThatBennyCanRead = 0;
93 $accountIdsThatBennyCanRead = array();
94 $startTime = microtime(
true);
95 for ($i = 0; $i < $count; $i++)
97 $bennyCanRead =
false;
98 $account = self::createRandomAccount($i);
99 $securableItemId = $i + 1;
102 $account->owner = $betty;
103 $bennyCanRead =
true;
107 $account->addPermissions($benny, Permission::READ);
108 $bennyCanRead =
true;
112 $account->addPermissions($salesStaff, Permission::READ);
113 $bennyCanRead =
true;
116 $this->assertTrue($account->save());
119 $countThatBennyCanRead++;
120 if ($countThatBennyCanRead <= 10)
122 $accountIdsThatBennyCanRead[] = $account->id;
126 if ($i >= $firstAccount - 1 &&
127 ($i - ($firstAccount - 1)) % $step == 0 &&
128 $testRebuildAfterCreateAccount)
130 $startTime = microtime(
true);
132 $endTime = microtime(
true);
133 if ($this->isDebug())
135 echo
'Rebuilt the munge in php in ' . round($endTime - $startTime, 1) .
' seconds, ' . self::getAccountMungeRowCount() .
" rows.\n";
137 $phpRows = self::getAccountMungeRows();
140 $startTime = microtime(
true);
142 $endTime = microtime(
true);
143 if ($this->isDebug())
145 echo
'Rebuilt the munge ' . (SECURITY_OPTIMIZED ?
'optimized' :
'in php') .
' in ' . round($endTime - $startTime, 1) .
' seconds, ' . self::getAccountMungeRowCount() .
" rows.\n";
147 $otherRows = self::getAccountMungeRows();
148 if ($phpRows != $otherRows)
150 echo
'PHP & optimized munges don\'t match after account ' . ($i + 1) .
"\n";
152 foreach ($phpRows as $row)
154 echo join(
', ', array_values($row)) .
"\n";
157 foreach ($otherRows as $row)
159 echo join(
', ', array_values($row)) .
"\n";
163 $this->assertEquals(count($phpRows), count($otherRows));
164 $this->assertEquals($phpRows, $otherRows);
167 $endTime = microtime(
true);
169 return array($endTime - $startTime, $countThatBennyCanRead, $accountIdsThatBennyCanRead);
172 protected static function createRandomAccount($i)
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);
192 protected static function accountMungeDoesntChangeWhenRebuilt()
198 $beforeRows = self::getAccountMungeRows();
200 $afterRows = self::getAccountMungeRows();
202 if ($beforeRows != $afterRows)
204 echo
"Before and after rebuild munge doesn't match.\n";
205 self::echoRows($beforeRows);
206 self::echoRows($afterRows);
209 return $beforeRows == $afterRows;
212 public static function getAccountMungeRows(
SecurableItem $securableItem = null)
214 if ($securableItem === null)
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');
224 $securableItemId = $securableItem->getClassId(
'SecurableItem');
225 $rows = ZurmoRedBean::getAll(
"select munge_id, count
227 where securableitem_id = $securableItemId
228 order by munge_id, count");
230 $rowsWithValues = array();
231 foreach ($rows as $row)
233 $row = array_values($row);
234 array_walk($row, array(
'self',
'stripFullStops'));
235 $rowsWithValues[] = $row;
237 return $rowsWithValues;
240 protected static function stripFullStops(&$value, $index)
247 $value = str_replace(
'.',
'', $value);
250 protected static function echoAccountMungeRows(
SecurableItem $securableItem = null)
252 self::echoRows(self::getAccountMungeRows($securableItem));
255 protected static function echoRows(array $rows)
258 foreach ($rows as $row)
260 echo join(
', ', array_values($row)) .
"\n";
265 protected static function getAccountMungeRowCount()
267 return intval(ZurmoRedBean::getCell(
'select count(*) from account_read'));
static getByUsername($username)
static forgetAll($onlyForgetPhpCache=false)
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 getCount(RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter=null, $where=null, $modelClassName=null, $selectDistinct=false)