/** * Test types. * Test how RedBeanPHP OODB and OODBBean handle type and type casts. * * Rules: * * 1. before storing a bean all types are preserved except booleans (they are converted to STRINGS '0' or '1') * 2. after store-reload all bean property values are STRINGS or NULL * (or ARRAYS but that's only from a user perspective because these are lazy loaded) * 3. the ID returned by store() is an INTEGER (if possible; on 32 bit systems overflowing values will be cast to STRINGS!) * * After loading: * ALL VALUES EXCEPT NULL -> STRING * NULL -> NULL * * @note Why not simply return bean->id in store()? Because not every driver returns the same type: * databases without insert_id support require a separate query or a suffix returning STRINGS, not INTEGERS. * * @note Why not preserve types? I.e. I store integer, why do I get back a string? * Answer: types are handled different across database platforms, would cause overhead to inspect every value for type, * also PHP is a dynamically typed language so types should not matter that much. Another reason: due to the nature * of RB columns in the database might change (INT -> STRING) this would cause return types to change as well which would * cause 'cascading errors', i.e. a column gets widened and suddenly your code would break. * * @note Unfortunately the 32/64-bit issue cannot be tested fully. Return-strategy store() is probably the safest * solution. * * @return void */ public function testTypes() { testpack('Beans can only contain STRING and NULL after reload'); R::nuke(); $bean = R::dispense('bean'); $bean->number = 123; $bean->float = 12.3; $bean->bool = false; $bean->bool2 = true; $bean->text = 'abc'; $bean->null = null; $bean->datetime = new DateTime('NOW', new DateTimeZone('Europe/Amsterdam')); $id = R::store($bean); asrt(is_int($id), TRUE); asrt(is_float($bean->float), TRUE); asrt(is_integer($bean->number), TRUE); asrt(is_string($bean->bool), TRUE); asrt(is_string($bean->bool2), TRUE); asrt(is_string($bean->datetime), TRUE); asrt(is_string($bean->text), TRUE); asrt(is_null($bean->null), TRUE); $bean = R::load('bean', $id); asrt(is_string($bean->id), TRUE); asrt(is_string($bean->float), TRUE); asrt(is_string($bean->number), TRUE); asrt(is_string($bean->bool), TRUE); asrt(is_string($bean->bool2), TRUE); asrt(is_string($bean->datetime), TRUE); asrt(is_string($bean->text), TRUE); asrt(is_null($bean->null), TRUE); asrt($bean->bool, '0'); asrt($bean->bool2, '1'); }
public function testCreate() { \R::nuke(); $this->user->create($this->email, $this->username, $this->password, $this->password); $id = $this->user->unbox()->id; $this->assertEquals(1, $id); }
/** * Test extended associations. * * @return void */ public function testExtAssoc() { $toolbox = R::$toolbox; $adapter = $toolbox->getDatabaseAdapter(); $writer = $toolbox->getWriter(); $redbean = $toolbox->getRedBean(); $pdo = $adapter->getDatabase(); R::nuke(); $webpage = $redbean->dispense("webpage"); $webpage->title = "page with ads"; $ad = $redbean->dispense("ad"); $ad->title = "buy this!"; $top = $redbean->dispense("placement"); $top->position = "top"; $bottom = $redbean->dispense("placement"); $bottom->position = "bottom"; $ea = new RedBean_AssociationManager_ExtAssociationManager($toolbox); $ea->extAssociate($ad, $webpage, $top); $ads = $redbean->batch("ad", $ea->related($webpage, "ad")); $adsPos = $redbean->batch("ad_webpage", $ea->related($webpage, "ad", TRUE)); asrt(count($ads), 1); asrt(count($adsPos), 1); $theAd = array_pop($ads); $theAdPos = array_pop($adsPos); asrt($theAd->title, $ad->title); asrt($theAdPos->position, $top->position); $ad2 = $redbean->dispense("ad"); $ad2->title = "buy this too!"; $ea->extAssociate($ad2, $webpage, $bottom); $ads = $redbean->batch("ad", $ea->related($webpage, "ad", TRUE)); asrt(count($ads), 2); }
/** * @BeforeSuite */ public static function prepare(SuiteEvent $event) { // prepare system for test suite // before it runs $config = json_decode(file_get_contents(__DIR__ . "/../../.config/database.json")); R::setup($config->driver . ':host=' . $config->host . ';dbname=' . $config->dbname, $config->user, $config->pass); R::nuke(); }
function tearDown() { // Must use nuke() to clear caches etc. R::nuke(); R::close(); $this->bbs = NULL; system("rm -rf " . self::DATA); }
/** * Various tests for OCI. * * @return void */ public function testVaria() { $toolbox = R::$toolbox; $adapter = $toolbox->getDatabaseAdapter(); $writer = $toolbox->getWriter(); $redbean = $toolbox->getRedBean(); $pdo = $adapter->getDatabase(); $page = $redbean->dispense("page"); try { $adapter->exec("an invalid query"); fail(); } catch (RedBean_Exception_SQL $e) { pass(); } asrt((int) $adapter->getCell("SELECT 123 FROM DUAL"), 123); $page->aname = "my page"; $id = (int) $redbean->store($page); asrt((int) $page->id, 1); asrt((int) $pdo->GetCell("SELECT count(*) FROM page"), 1); asrt($pdo->GetCell("SELECT aname FROM page WHERE ROWNUM<=1"), "my page"); asrt((int) $id, 1); $page = $redbean->load("page", 1); asrt($page->aname, "my page"); asrt((bool) $page->getMeta("type"), TRUE); asrt(isset($page->id), TRUE); asrt($page->getMeta("type"), "page"); asrt((int) $page->id, $id); R::nuke(); $rooms = R::dispense('room', 2); $rooms[0]->kind = 'suite'; $rooms[1]->kind = 'classic'; $rooms[0]->number = 6; $rooms[1]->number = 7; R::store($rooms[0]); R::store($rooms[1]); $rooms = R::getAssoc('SELECT ' . R::$writer->esc('number') . ', kind FROM room ORDER BY kind ASC'); foreach ($rooms as $key => $room) { asrt($key === 6 || $key === 7, TRUE); asrt($room == 'classic' || $room == 'suite', TRUE); } $rooms = R::$adapter->getAssoc('SELECT kind FROM room'); foreach ($rooms as $key => $room) { asrt($room == 'classic' || $room == 'suite', TRUE); asrt($room, $key); } $rooms = R::getAssoc('SELECT ' . R::$writer->esc('number') . ', kind FROM rooms2 ORDER BY kind ASC'); asrt(count($rooms), 0); asrt(is_array($rooms), TRUE); $date = R::dispense('mydate'); $date->date = '2012-12-12 20:50'; $date->time = '12:15'; $id = R::store($date); $ok = R::load('mydate', 1); }
/** * Begin testing. * This method runs the actual test pack. * * @return void */ public function run() { R::nuke(); file_put_contents('/tmp/test_log.txt', ''); R::log('/tmp/test_log.txt'); $bean = R::dispense('bean'); $bean->name = TRUE; R::store($bean); $bean->name = 'test'; R::store($bean); $log = file_get_contents('/tmp/test_log.txt'); asrt(strlen($log) > 0, TRUE); echo $log; }
/** * Tests the R::inspect() method on the Facade. * * @return void */ public function testInspect() { testpack('Test R::inspect() '); R::nuke(); R::store(R::graph(array('type' => 'book', 'title' => 'book'))); $info = R::inspect(); asrt(count($info), 1); asrt(strtolower($info[0]), 'book'); $info = R::inspect('book'); asrt(count($info), 2); $keys = array_keys($info); sort($keys); asrt(strtolower($keys[0]), 'id'); asrt(strtolower($keys[1]), 'title'); }
/** * Nuclear test suite. * * @return void */ public function testNuke() { $bean = R::dispense('bean'); R::store($bean); asrt(count(R::$writer->getTables()), 1); R::nuke(); asrt(count(R::$writer->getTables()), 0); $bean = R::dispense('bean'); R::store($bean); asrt(count(R::$writer->getTables()), 1); R::freeze(); R::nuke(); // No effect asrt(count(R::$writer->getTables()), 1); R::freeze(FALSE); }
/** * Tests whether we can store an empty bean. * An empty bean has no properties, only ID. Normally we would * skip the ID field in an INSERT, this test forces the driver * to specify a value for the ID field. Different writers have to * use different values: Mysql uses NULL to insert a new auto-generated ID, * while Postgres has to use DEFAULT. */ public function testEmptyBean() { testpack('Test Empty Bean Storage.'); R::nuke(); $bean = R::dispense('emptybean'); $id = R::store($bean); asrt($id > 0, TRUE); asrt(R::count('emptybean'), 1); $bean = R::dispense('emptybean'); $id = R::store($bean); asrt($id > 0, TRUE); asrt(R::count('emptybean'), 2); //also test in frozen mode R::freeze(TRUE); $bean = R::dispense('emptybean'); $id = R::store($bean); asrt($id > 0, TRUE); asrt(R::count('emptybean'), 3); R::freeze(FALSE); }
/** * Test if RedBeanPHP can properly handle keywords. * * @return void */ public function testKeywords() { $keywords = array('anokeyword', 'znokeyword', 'group', 'DROP', 'inner', 'JOIN', 'select', 'table', 'int', 'cascade', 'float', 'CALL', 'in', 'status', 'order', 'limit', 'having', 'else', 'if', 'while', 'distinct', 'like'); R::setStrictTyping(FALSE); RedBean_OODBBean::setFlagBeautifulColumnNames(FALSE); foreach ($keywords as $k) { R::nuke(); $bean = R::dispense($k); $bean->{$k} = $k; $id = R::store($bean); $bean = R::load($k, $id); $bean2 = R::dispense('other'); $bean2->name = $k; $bean->bean = $bean2; $bean->ownBean[] = $bean2; $bean->sharedBean[] = $bean2; $id = R::store($bean); R::trash($bean); pass(); } RedBean_OODBBean::setFlagBeautifulColumnNames(TRUE); R::setStrictTyping(TRUE); }
/** * MySQL specific tests. * * @return void */ public function testMySQL() { if ($this->currentlyActiveDriverID !== 'mysql') { return; } testpack('Throw exception in case of issue with assoc constraint'); $bunny = R::dispense('bunny'); $carrot = R::dispense('carrot'); $faultyWriter = new FaultyWriter(R::$toolbox->getDatabaseAdapter()); $faultyToolbox = new RedBean_ToolBox(R::$toolbox->getRedBean(), R::$toolbox->getDatabaseAdapter(), $faultyWriter); $faultyAssociationManager = new RedBean_AssociationManager($faultyToolbox); $faultyWriter->setSQLState('23000'); $faultyAssociationManager->associate($bunny, $carrot); pass(); $faultyWriter->setSQLState('42S22'); R::nuke(); try { $faultyAssociationManager->associate($bunny, $carrot); fail(); } catch (RedBean_Exception_SQL $exception) { pass(); } }
function droptables() { global $nukepass; R::nuke(); if (!count($t = R::$writer->getTables())) { $nukepass++; } else { echo "\nFailed to clean up database: " . print_r($t, 1); fail(); } return; if ($db == 'mysql') { R::exec('SET FOREIGN_KEY_CHECKS=0;'); } if ($db == 'sqlite') { R::exec('PRAGMA foreign_keys = 0 '); } R::exec('drop view if exists people'); R::exec('drop view if exists library2'); foreach (R::$writer->getTables() as $t) { if ($db == 'mysql') { R::exec("drop table `{$t}`"); } if ($db == 'pgsql') { R::exec("drop table \"{$t}\" cascade"); } if ($db == 'sqlite') { R::exec("drop table {$t} "); } } if ($db == 'mysql') { R::exec('SET FOREIGN_KEY_CHECKS=1;'); } if ($db == 'sqlite') { R::exec('PRAGMA foreign_keys = 1 '); } }
/** * Test boxing beans. * * @return void */ public function testBoxing() { R::nuke(); $bean = R::dispense('boxedbean')->box(); R::trash($bean); pass(); $bean = R::dispense('boxedbean'); $bean->sharedBoxbean = R::dispense('boxedbean')->box(); R::store($bean); pass(); $bean = R::dispense('boxedbean'); $bean->ownBoxedbean = R::dispense('boxedbean')->box(); R::store($bean); pass(); $bean = R::dispense('boxedbean'); $bean->other = R::dispense('boxedbean')->box(); R::store($bean); pass(); $bean = R::dispense('boxedbean'); $bean->title = 'MyBean'; $box = $bean->box(); asrt($box instanceof Model_Boxedbean, TRUE); R::store($box); }
/** * Run the actual test */ public function run() { $class = new ReflectionClass($this); $skip = array('run', 'getTargetDrivers', 'onEvent'); // Call all methods except run automatically foreach ($class->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { // Skip methods inherited from parent class if ($method->class != $class->getName()) { continue; } if (in_array($method->name, $skip)) { continue; } $classname = str_replace($class->getParentClass()->getName() . '_', '', $method->class); printtext("\n\t" . $classname . "->" . $method->name . " [" . $method->class . "->" . $method->name . "]" . " \n\t"); $call = $method->name; $this->{$call}(); try { R::nuke(); } catch (PDOException $e) { // Some tests use a broken database on purpose, so an exception is ok } } }
/** * Test tree traversal with searchIn(). * * @return void */ public function testTreeTraversal() { testpack('Test Tree Traversal'); R::nuke(); $page = R::dispense('page', 10); //Setup the test data for this series of tests $i = 0; foreach ($page as $pageItem) { $pageItem->name = 'page' . $i; $pageItem->number = $i; $i++; R::store($pageItem); } $page[0]->ownPage = array($page[1], $page[2]); $page[1]->ownPage = array($page[3], $page[4]); $page[3]->ownPage = array($page[5]); $page[5]->ownPage = array($page[7]); $page[9]->document = $page[8]; $page[9]->book = R::dispense('book'); R::store($page[9]); $id = R::store($page[0]); $book = $page[9]->book; //Basics, load child nodes in tree $referencePage = R::load('page', $id); $found = $referencePage->searchIn('ownPage'); $foundStr = ''; $foundItems = array(); foreach ($found as $foundBean) { $foundItems[] = $foundBean->name; } sort($foundItems); $foundStr = implode('', $foundItems); asrt($foundStr, 'page1page2page3page4page5page7'); //A subset of a tree... $referencePage = R::load('page', $page[3]->id); $found = $referencePage->searchIn('ownPage'); $foundStr = ''; $foundItems = array(); foreach ($found as $foundBean) { $foundItems[] = $foundBean->name; } sort($foundItems); $foundStr = implode('', $foundItems); asrt($foundStr, 'page5page7'); //Now test with a condition $referencePage = R::load('page', $page[1]->id); $found = $referencePage->withCondition(' page.number > 6 ')->searchIn('ownPage'); $foundStr = ''; $foundItems = array(); foreach ($found as $foundBean) { $foundItems[] = $foundBean->name; } sort($foundItems); $foundStr = implode('', $foundItems); asrt($foundStr, 'page7'); //Condition and slot $referencePage = R::load('page', $page[1]->id); $found = $referencePage->withCondition(' page.number > ? ', array(6))->searchIn('ownPage'); $foundStr = ''; $foundItems = array(); foreach ($found as $foundBean) { $foundItems[] = $foundBean->name; } sort($foundItems); $foundStr = implode('', $foundItems); asrt($foundStr, 'page7'); //Condition and named slot $referencePage = R::load('page', $page[1]->id); $found = $referencePage->withCondition(' page.number > :num ', array(':num' => 6))->searchIn('ownPage'); $foundStr = ''; $foundItems = array(); foreach ($found as $foundBean) { $foundItems[] = $foundBean->name; } sort($foundItems); $foundStr = implode('', $foundItems); asrt($foundStr, 'page7'); //Ordering... works different, orders within a set! $referencePage = R::load('page', $page[0]->id); $found = $referencePage->with(' ORDER BY page.number DESC ')->searchIn('ownPage'); $foundStr = ''; $foundItems = array(); foreach ($found as $foundBean) { $foundItems[] = $foundBean->name; } $foundStr = implode('', $foundItems); asrt($foundStr, 'page2page1page4page3page5page7'); //now with parents $referencePage = R::load('page', $page[5]->id); $found = $referencePage->searchIn('page'); $foundStr = ''; $foundItems = array(); foreach ($found as $foundBean) { $foundItems[] = $foundBean->name; } sort($foundItems); $foundStr = implode('', $foundItems); asrt($foundStr, 'page0page1page3'); //now with parents and aliases... $otherPage = R::dispense('page'); $otherPage->document = $page[0]; $otherPage->name = 'pagex'; $page[0]->document = $page[6]; $page[6]->document = $page[8]; R::store($otherPage); $referencePage = R::load('page', $otherPage->id); $found = $referencePage->fetchAs('page')->searchIn('document'); $foundStr = ''; $foundItems = array(); foreach ($found as $foundBean) { $foundItems[] = $foundBean->name; } sort($foundItems); $foundStr = implode('', $foundItems); asrt($foundStr, 'page0page6page8'); //now with parents and condition $referencePage = R::load('page', $otherPage->id); $found = $referencePage->withCondition(' page.number > 6 ')->fetchAs('page')->searchIn('document'); $foundStr = ''; $foundItems = array(); foreach ($found as $foundBean) { $foundItems[] = $foundBean->name; } sort($foundItems); $foundStr = implode('', $foundItems); asrt($foundStr, 'page8'); //now with parents and condition (variation) R::$writer->setUseCache(FALSE); $referencePage = R::load('page', $page[7]->id); $found = $referencePage->withCondition(' ( page.number < ? OR page.number = 5 ) ', array(3))->searchIn('page'); $foundStr = ''; $foundItems = array(); foreach ($found as $foundBean) { $foundItems[] = $foundBean->name; } sort($foundItems); $foundStr = implode('', $foundItems); asrt($foundStr, 'page0page1page5'); //try to cause a cache error... (parent cache) $referencePage = R::load('page', $otherPage->id); $parentPage = $referencePage->fetchAs('page')->document; $referencePages = $parentPage->alias('document')->withCondition(' id = ?', array($otherPage->id))->ownPage; $referencePage = reset($referencePages); $found = $referencePage->withCondition(' page.number > 6 ')->fetchAs('page')->searchIn('document'); $foundStr = ''; $foundItems = array(); foreach ($found as $foundBean) { $foundItems[] = $foundBean->name; } sort($foundItems); $foundStr = implode('', $foundItems); asrt($foundStr, 'page8'); $referencePage = R::load('page', $page[8]->id); $found = $referencePage->alias('document')->withCondition(' page.number > 5 ')->searchIn('ownPage'); $foundStr = ''; $foundItems = array(); foreach ($found as $foundBean) { $foundItems[] = $foundBean->name; } sort($foundItems); $foundStr = implode('', $foundItems); asrt($foundStr, 'page6page9'); //also test if with-condition has been cleared! asrt(count($book->fresh()->ownPage), 1); //also if you find nothing.. $found = $referencePage->withCondition(' page.number = 999 ')->searchIn('ownPage'); asrt(count($found), 0); asrt(count($book->ownPage), 1); //store should not affect next search R::store($referencePage); $referencePage = R::load('page', $page[8]->id); $found = $referencePage->alias('document')->searchIn('ownPage'); $foundStr = ''; $foundItems = array(); foreach ($found as $foundBean) { $foundItems[] = $foundBean->name; } sort($foundItems); $foundStr = implode('', $foundItems); asrt($foundStr, 'page0page6page9pagex'); //shared search not allowed try { $referencePage->searchIn('sharedPage'); fail(); } catch (RedBean_Exception_Security $exception) { pass(); } //but shareditem should be allowed try { $referencePage->searchIn('sharedpage'); pass(); } catch (RedBean_Exception_Security $exception) { fail(); } }
/** * Test graph() method. * * @return void */ public function testGraph() { RedBean_Plugin_Cooker::enableBeanLoading(TRUE); R::dependencies(array()); $currentDriver = $this->currentlyActiveDriverID; global $lifeCycle; $toolbox = R::$toolbox; $adapter = $toolbox->getDatabaseAdapter(); $writer = $toolbox->getWriter(); $redbean = $toolbox->getRedBean(); try { R::graph(array(array(array('a' => 'b')))); fail(); } catch (RedBean_Exception_Security $e) { pass(); } try { R::graph('ABC'); fail(); } catch (RedBean_Exception_Security $e) { pass(); } try { R::graph(123); fail(); } catch (RedBean_Exception_Security $e) { pass(); } try { R::graph(array(new stdClass())); fail(); } catch (RedBean_Exception_Security $e) { pass(); } list($v1, $v2, $v3) = R::dispense('village', 3); list($b1, $b2, $b3, $b4, $b5, $b6) = R::dispense('building', 6); list($f1, $f2, $f3, $f4, $f5, $f6) = R::dispense('farmer', 6); list($u1, $u2, $u3, $u4, $u5, $u6) = R::dispense('furniture', 6); list($a1, $a2) = R::dispense('army', 2); $a1->strength = 100; $a2->strength = 200; $v1->name = 'v1'; $v2->name = 'v2'; $v3->name = 'v3'; $v1->ownBuilding = array($b4, $b6); $v2->ownBuilding = array($b1); $v3->ownBuilding = array($b5); $b1->ownFarmer = array($f1, $f2); $b6->ownFarmer = array($f3); $b5->ownFarmer = array($f4); $b5->ownFurniture = array($u6, $u5, $u4); $v2->sharedArmy[] = $a2; $v3->sharedArmy = array($a2, $a1); $i2 = R::store($v2); $i1 = R::store($v1); $i3 = R::store($v3); $v1 = R::load('village', $i1); $v2 = R::load('village', $i2); $v3 = R::load('village', $i3); asrt(count($v3->ownBuilding), 1); asrt(count(reset($v3->ownBuilding)->ownFarmer), 1); asrt(count(reset($v3->ownBuilding)->ownFurniture), 3); asrt(count($v3->sharedArmy), 2); asrt(count($v1->sharedArmy), 0); asrt(count($v2->sharedArmy), 1); asrt(count($v2->ownBuilding), 1); asrt(count($v1->ownBuilding), 2); asrt(count(reset($v1->ownBuilding)->ownFarmer), 0); asrt(count(end($v1->ownBuilding)->ownFarmer), 1); asrt(count($v3->ownTapestry), 0); // Change the names and add the same building should not change the graph $v1->name = 'village I'; $v2->name = 'village II'; $v3->name = 'village III'; $v1->ownBuilding[] = $b4; $i2 = R::store($v2); $i1 = R::store($v1); $i3 = R::store($v3); $v1 = R::load('village', $i1); $v2 = R::load('village', $i2); $v3 = R::load('village', $i3); asrt(count($v3->ownBuilding), 1); asrt(count(reset($v3->ownBuilding)->ownFarmer), 1); asrt(count(reset($v3->ownBuilding)->ownFurniture), 3); asrt(count($v3->sharedArmy), 2); asrt(count($v1->sharedArmy), 0); asrt(count($v2->sharedArmy), 1); asrt(count($v2->ownBuilding), 1); asrt(count($v1->ownBuilding), 2); asrt(count(reset($v1->ownBuilding)->ownFarmer), 0); asrt(count(end($v1->ownBuilding)->ownFarmer), 1); asrt(count($v3->ownTapestry), 0); $b = reset($v1->ownBuilding); R::trash($b); $n = R::count('village'); asrt($n, 3); $n = R::count('army'); R::trash($v1); asrt(R::count('army'), $n); R::trash($v2); asrt(R::count('army'), $n); R::trash($v3); asrt(R::count('army'), $n); $json = '{"mysongs": { "type": "playlist", "name": "JazzList", "ownTrack": [ { "type": "track", "name": "harlem nocturne", "order": "1", "sharedSong": [ { "type": "song", "url": "music.com.harlem" } ], "cover": { "type": "cover", "url": "albumart.com\\/duke1" } }, { "type": "track", "name": "brazil", "order": "2", "sharedSong": [ { "type": "song", "url": "music.com\\/djan" } ], "cover": { "type": "cover", "url": "picasa\\/django" } } ] }}'; $playList = json_decode($json, TRUE); $playList = R::graph($playList); $id = R::store(reset($playList)); $play = R::load("playlist", $id); asrt(count($play->ownTrack), 2); foreach ($play->ownTrack as $track) { asrt(count($track->sharedSong), 1); asrt($track->cover instanceof RedBean_OODBBean, TRUE); } $json = '{"mysongs": { "type": "playlist", "id": "1", "ownTrack": [ { "type": "track", "name": "harlem nocturne", "order": "1", "sharedSong": [ { "type": "song", "id": "1" } ], "cover": { "type": "cover", "id": "2" } }, { "type": "track", "name": "brazil", "order": "2", "sharedSong": [ { "type": "song", "url": "music.com\\/djan" } ], "cover": { "type": "cover", "url": "picasa\\/django" } } ] }}'; $playList = json_decode($json, TRUE); $cooker = new RedBean_Plugin_Cooker(); $cooker->setToolbox(R::$toolbox); $playList = $cooker->graph($playList); $id = R::store(reset($playList)); $play = R::load("playlist", $id); asrt(count($play->ownTrack), 2); foreach ($play->ownTrack as $track) { asrt(count($track->sharedSong), 1); asrt($track->cover instanceof RedBean_OODBBean, TRUE); } $track = reset($play->ownTrack); $song = reset($track->sharedSong); asrt(intval($song->id), 1); asrt($song->url, "music.com.harlem"); $json = '{"mysongs": { "type": "playlist", "id": "1", "ownTrack": [ { "type": "track", "name": "harlem nocturne", "order": "1", "sharedSong": [ { "type": "song", "id": "1", "url": "changedurl" } ], "cover": { "type": "cover", "id": "2" } }, { "type": "track", "name": "brazil", "order": "2", "sharedSong": [ { "type": "song", "url": "music.com\\/djan" } ], "cover": { "type": "cover", "url": "picasa\\/django" } } ] }}'; $playList = json_decode($json, TRUE); $cooker = new RedBean_Plugin_Cooker(); $cooker->setToolbox(R::$toolbox); $playList = $cooker->graph($playList); $id = R::store(reset($playList)); $play = R::load("playlist", $id); asrt(count($play->ownTrack), 2); foreach ($play->ownTrack as $track) { asrt(count($track->sharedSong), 1); asrt($track->cover instanceof RedBean_OODBBean, TRUE); } $track = reset($play->ownTrack); $song = reset($track->sharedSong); asrt(intval($song->id), 1); asrt($song->url, "changedurl"); // Tree $page = R::dispense('page'); $page->name = 'root of all evil'; list($subPage, $subSubPage, $subNeighbour, $subOfSubNeighbour, $subSister) = R::dispense('page', 5); $subPage->name = 'subPage'; $subSubPage->name = 'subSubPage'; $subOfSubNeighbour->name = 'subOfSubNeighbour'; $subNeighbour->name = 'subNeighbour'; $subSister->name = 'subSister'; $page->ownPage = array($subPage, $subNeighbour, $subSister); R::store($page); asrt(count($page->ownPage), 3); foreach ($page->ownPage as $p) { if ($p->name == 'subPage') { $p->ownPage[] = $subSubPage; } if ($p->name == 'subNeighbour') { $p->ownPage[] = $subOfSubNeighbour; } } R::store($page); asrt(count($page->ownPage), 3); list($first, $second) = array_keys($page->ownPage); foreach ($page->ownPage as $p) { if ($p->name == 'subPage' || $p->name == 'subNeighbour') { asrt(count($p->ownPage), 1); } else { asrt(count($p->ownPage), 0); } } R::nuke(); $canes = candy_canes(); $id = R::store($canes[0]); $cane = R::load('cane', $id); asrt($cane->label, 'Cane No. 0'); asrt($cane->cane->label, 'Cane No. 1'); asrt($cane->cane->cane->label, 'Cane No. 4'); asrt($cane->cane->cane->cane->label, 'Cane No. 7'); asrt($cane->cane->cane->cane->cane, NULL); // Test backward compatibility asrt($page->owner, NULL); RedBean_ModelHelper::setModelFormatter(NULL); $band = R::dispense('band'); $musicians = R::dispense('bandmember', 5); $band->ownBandmember = $musicians; try { R::store($band); fail(); } catch (Exception $e) { pass(); } $band = R::dispense('band'); $musicians = R::dispense('bandmember', 4); $band->ownBandmember = $musicians; try { $id = R::store($band); pass(); } catch (Exception $e) { fail(); } $band = R::load('band', $id); $band->ownBandmember[] = R::dispense('bandmember'); try { R::store($band); fail(); } catch (Exception $e) { pass(); } // Test fuse $lifeCycle = ""; $bandmember = R::dispense('bandmember'); $bandmember->name = 'Fatz Waller'; $id = R::store($bandmember); $bandmember = R::load('bandmember', $id); R::trash($bandmember); $expected = 'calleddispenseid0calledupdateid0nameFatzWallercalledafter_updateid5nameFatzWallercalleddispenseid0calledopen5calleddeleteid5band_idnullnameFatzWallercalledafter_deleteid0band_idnullnameFatzWaller'; $lifeCycle = preg_replace("/\\W/", "", $lifeCycle); asrt($lifeCycle, $expected); // Test whether a nested bean will be saved if tainted R::nuke(); $page = R::dispense('page'); $page->title = 'a blank page'; $book = R::dispense('book'); $book->title = 'shiny white pages'; $book->ownPage[] = $page; $id = R::store($book); $book = R::load('book', $id); $page = reset($book->ownPage); asrt($page->title, 'a blank page'); $page->title = 'slightly different white'; R::store($book); $book = R::load('book', $id); $page = reset($book->ownPage); asrt($page->title, 'slightly different white'); $page = R::dispense('page'); $page->title = 'x'; $book = R::load('book', $id); $book->title = 'snow white pages'; $page->book = $book; $pid = R::store($page); $page = R::load('page', $pid); asrt($page->book->title, 'snow white pages'); // Test you cannot unset a relation list asrt(count($book->ownPage), 2); unset($book->ownPage); $book = R::load('book', R::store($book)); asrt(count($book->ownPage), 2); $book->sharedTree = R::dispense('tree'); R::store($book); $c = R::count('page'); asrt(R::count('tree'), 1); R::trash($book); asrt(R::count('page'), $c); asrt(R::count('tree'), 1); R::nuke(); $v = R::dispense('village'); list($b1, $b2) = R::dispense('building', 2); $b1->name = 'a'; $b2->name = 'b'; $b2->village = $v; $v->ownBuilding[] = $b1; $b1->ownFurniture[] = R::dispense('furniture'); $id = R::store($b2); $b2 = R::load('building', $id); asrt(count($b2->village->ownBuilding), 2); $buildings = $b2->village->ownBuilding; foreach ($buildings as $b) { if ($b->id != $id) { asrt(count($b->ownFurniture), 1); } } // Save a form using graph and ignore empty beans R::nuke(); $product = R::dispense('product'); $product->name = 'shampoo'; $productID = R::store($product); $coupon = R::dispense('coupon'); $coupon->name = '567'; $couponID = R::store($coupon); $form = array('type' => 'order', 'ownProduct' => array(array('id' => $productID, 'type' => 'product')), 'ownCustomer' => array(array('type' => 'customer', 'name' => 'Bill'), array('type' => 'customer', 'name' => '')), 'sharedCoupon' => array(array('type' => 'coupon', 'name' => '123'), array('type' => 'coupon', 'id' => $couponID))); $order = R::graph($form, TRUE); asrt($order->getMeta('type'), 'order'); asrt(count($order->ownProduct), 1); asrt(count($order->ownCustomer), 1); asrt(count($order->sharedCoupon), 2); asrt(end($order->ownProduct)->id, $productID); asrt(end($order->ownProduct)->name, 'shampoo'); asrt(end($order->ownCustomer)->name, 'Bill'); asrt($order->sharedCoupon[$couponID]->name, '567'); R::nuke(); $form = array('type' => 'person', 'name' => 'Fred', 'phone' => ''); $bean = R::graph($form); asrt($bean->name, 'Fred'); asrt($bean->phone, ''); $cooker = new RedBean_Plugin_Cooker(); $cooker->setUseNullFlag(TRUE); $form = array('type' => 'person', 'name' => 'Fred', 'phone' => ''); $bean = R::graph($form); asrt($bean->name, 'Fred'); asrt($bean->phone, NULL); RedBean_Plugin_Cooker::setUseNullFlagSt(FALSE); // Save a form using graph and ignore empty beans, wrong nesting R::nuke(); $product = R::dispense('product'); $product->name = 'shampoo'; $productID = R::store($product); $coupon = R::dispense('coupon'); $coupon->name = '567'; $couponID = R::store($coupon); $form = array('type' => 'order', 'ownProduct' => array(array(array('id' => $productID, 'type' => 'product')))); try { $order = R::graph($form, TRUE); fail(); } catch (RedBean_Exception_Security $e) { pass(); } // Without ignore empty beans R::nuke(); $product = R::dispense('product'); $product->name = 'shampoo'; $productID = R::store($product); $coupon = R::dispense('coupon'); $coupon->name = '567'; $couponID = R::store($coupon); $form = array('type' => 'order', 'ownProduct' => array(array('id' => $productID, 'type' => 'product')), 'ownCustomer' => array(array('type' => 'customer', 'name' => 'Bill'), array('type' => 'customer', 'name' => '')), 'sharedCoupon' => array(array('type' => 'coupon', 'name' => '123'), array('type' => 'coupon', 'id' => $couponID))); RedBean_Plugin_Cooker::enableBeanLoading(FALSE); $exc = FALSE; try { $order = R::graph($form); fail(); } catch (Exception $e) { $exc = $e; } asrt($exc instanceof RedBean_Exception_Security, TRUE); RedBean_Plugin_Cooker::enableBeanLoading(TRUE); $order = R::graph($form); asrt($order->getMeta('type'), 'order'); asrt(count($order->ownProduct), 1); asrt(count($order->ownCustomer), 2); asrt(count($order->sharedCoupon), 2); asrt(end($order->ownProduct)->id, $productID); // Make sure zeros are preserved $form = array('type' => 'laptop', 'price' => 0); $product = R::graph($form); asrt(isset($product->price), TRUE); asrt($product->price, 0); }
public function install($debug = false) { /** * Load models */ $this->load->model('account/account_model'); $this->load->model('account/group_model'); $this->load->model('account/uriresource_model'); $this->load->model('layout/layout_model'); $this->load->model('admin/modmenu_model'); $this->load->model('map_model'); $this->load->model('layer_model'); $this->load->model('mapserver/mapserver_model'); $this->load->model('googleearth/googleearth_model'); $this->load->model('openlayers/openlayers_model'); $this->load->model('admin/dataexplorer_model'); // Debug SQL R::debug($debug); // EMPTY DATABASE R::nuke(); // Create admin account $account_admin = $this->account_model->create('*****@*****.**', 'admin', 'admin'); R::store($account_admin); // Create guest account $account_guest = $this->account_model->create('*****@*****.**', 'guest', ''); R::store($account_guest); // Create geo account $account_geo = $this->account_model->create('*****@*****.**', 'geo', 'geo'); R::store($account_geo); // Create admin group $group_admin = $this->group_model->create('admin'); R::store($group_admin); // Create guest group $group_guest = $this->group_model->create('guest'); R::store($group_admin); // Create geo group $group_geo = $this->group_model->create('geo'); R::store($group_geo); // Add admin account to admin group $group_admin->sharedAccount[] = $account_admin; R::store($group_admin); // Add guest account to guest group $group_guest->sharedAccount[] = $account_guest; R::store($group_guest); // Add geo account to geo group $group_geo->sharedAccount[] = $account_geo; R::store($group_geo); // Create Uri Resources $uriresource_admin = $this->uriresource_model->create('/^admin/i'); R::store($uriresource_admin); $uriresource_user = $this->uriresource_model->create('/^user/i'); R::store($uriresource_user); // Add permission to guest group $this->group_model->addPermission($group_guest, $uriresource_user, 'deny', 0); $this->group_model->addPermission($group_guest, $uriresource_admin, 'deny', 0); R::store($group_guest); // Register authentication module block $previewimg = 'web/images/module/simpleauth.png'; $authblock = $this->layout_model->createModule('Simple Authentication', 'account/modauth_lblock', NULL, $previewimg); $authblock->owner = $account_admin; R::store($authblock); // Register tickets block $previewimg = 'web/images/module/tickets.png'; $ticketsblock = $this->layout_model->createModule('Tickets', 'crm/tickets_lblock', NULL, $previewimg); $ticketsblock->owner = $account_admin; R::store($ticketsblock); // Register layerswitcher block $previewimg = 'web/images/module/layerswitcher.png'; $layerswitcherblock = $this->layout_model->createModule('Layer Switcher', 'openlayers/layerswitcher_lblock', 'olmap', $previewimg); $layerswitcherblock->owner = $account_admin; R::store($layerswitcherblock); // Register Footer Module $footermod = $this->layout_model->createModule('Footer', 'admin/footer_lblock'); $footermod->owner = $account_admin; R::store($footermod); // Register Credits Module $creditsmod = $this->layout_model->createModule('Credits', 'admin/credits_lblock'); $creditsmod->owner = $account_admin; R::store($creditsmod); // Register language switcher module block $previewimg = 'web/images/module/idiomswitcher.png'; $langblock = $this->layout_model->createModule('Language Selection', 'crm/lang_lblock', NULL, $previewimg); $langblock->owner = $account_admin; R::store($langblock); // Register featuresearch block $previewimg = 'web/images/module/featuresearch.png'; $searchblock = $this->layout_model->createModule('Feature Search', 'openlayers/featuresearch_lblock', 'olmap', $previewimg); $searchblock->owner = $account_admin; R::store($searchblock); // Register Google Maps Api module $modgmapsapi = $this->layout_model->createModule('Load Google Maps API', 'openlayers/modgmapsapiv3_lblock'); $modgmapsapi->owner = $account_admin; R::store($modgmapsapi); // Register gefeaturesearch block $previewimg = 'web/images/module/gefeaturesearch.png'; $gesearchblock = $this->layout_model->createModule('Google Earth Search', 'googleearth/gefeaturesearch_lblock', 'gemap', $previewimg); $gesearchblock->owner = $account_admin; R::store($gesearchblock); // Create layout $layout_public = $this->layout_model->create('public', 'layout/publicfullscreen2'); $layout_public->owner = $account_admin; R::store($layout_public); // Create public layout slots $pslot1 = $this->layout_model->createSlot('slot1', $layout_public); $pslot2 = $this->layout_model->createSlot('slot2', $layout_public); $pslot3 = $this->layout_model->createSlot('slot3', $layout_public); $pslot4 = $this->layout_model->createSlot('slot4', $layout_public); $pslot5 = $this->layout_model->createSlot('slot5', $layout_public); $pslot1->owner = $account_admin; $pslot2->owner = $account_admin; $pslot3->owner = $account_admin; $pslot4->owner = $account_admin; $pslot5->owner = $account_admin; R::storeAll(array($pslot1, $pslot2, $pslot3, $pslot4, $pslot5)); // Create module layout $layout_mod = $this->layout_model->create('module', 'layout/module'); $layout_mod->owner = $account_admin; R::store($layout_mod); // Create module layout slots $mslot1 = $this->layout_model->createSlot('slot1', $layout_mod); $mslot2 = $this->layout_model->createSlot('slot2', $layout_mod); $mslot3 = $this->layout_model->createSlot('slot3', $layout_mod); $mslot1->owner = $account_admin; $mslot2->owner = $account_admin; $mslot3->owner = $account_admin; R::storeAll(array($mslot1, $mslot2, $mslot3)); // Create layout blocks $lblock2 = $this->layout_model->createBlock('authblock', $authblock, 1); $lblock2->owner = $account_admin; R::storeAll(array($lblock2)); // Create tickets layout block $lblock3 = $this->layout_model->createBlock('tickets', $ticketsblock, 2); $lblock3->owner = $account_admin; R::storeAll(array($lblock3)); // Create layerswitcher layout block $lblock4 = $this->layout_model->createBlock('layerswitcher1', $layerswitcherblock, 3, '', 1); $lblock4->owner = $account_admin; R::storeAll(array($lblock4)); // Create layout blocks $footerblock = $this->layout_model->createBlock('footerblock', $footermod, 1); $footerblock->owner = $account_admin; $creditsblock = $this->layout_model->createBlock('creditsblock', $creditsmod, 2); $creditsblock->owner = $account_admin; R::storeAll(array($footerblock, $creditsblock)); // Create language blocks $lblock5 = $this->layout_model->createBlock('langblock', $langblock, 3); $lblock5->owner = $account_admin; R::storeAll(array($lblock5)); // Assign layout blocks to slots $this->layout_model->slotAddBlock($pslot2, $lblock3); $this->layout_model->slotAddBlock($pslot2, $lblock2); $this->layout_model->slotAddBlock($pslot4, $footerblock); $this->layout_model->slotAddBlock($pslot5, $creditsblock); $this->layout_model->slotAddBlock($pslot2, $lblock5); // Create registered layout $layout_reg = $this->layout_model->create('registered', 'layout/registered'); $layout_reg->owner = $account_admin; R::store($layout_reg); // Create registered layout slots $rslot1 = $this->layout_model->createSlot('slot1', $layout_reg); $rslot2 = $this->layout_model->createSlot('slot2', $layout_reg); $rslot3 = $this->layout_model->createSlot('slot3', $layout_reg); $rslot4 = $this->layout_model->createSlot('slot4', $layout_reg); $rslot5 = $this->layout_model->createSlot('slot5', $layout_reg); $rslot1->owner = $account_admin; $rslot2->owner = $account_admin; $rslot3->owner = $account_admin; $rslot4->owner = $account_admin; $rslot5->owner = $account_admin; R::storeAll(array($rslot1, $rslot2, $rslot3, $rslot4, $rslot5)); // Create layout blocks $rlblock2 = $this->layout_model->createBlock('userauthblock', $authblock, 1); $rlblock2->owner = $account_admin; R::storeAll(array($rlblock2)); // Assign layout blocks to slots $this->layout_model->slotAddBlock($rslot2, $rlblock2); // Create Admin Menu $menu1 = $this->modmenu_model->create('admin'); $menu1->owner = $account_admin; $this->modmenu_model->save($menu1); // Register admin menu block $previewimg = 'web/images/module/menu.png'; $menublock1 = $this->layout_model->createModule('Menu', 'admin/modmenu_lblock', 'modmenu', $previewimg); $menublock1->owner = $account_admin; R::store($menublock1); // Register WFS get feature popup module $previewimg = 'web/images/module/wfsgetfeature.png'; $wfsgetfeature1 = $this->layout_model->createModule('WFSGetFeature Popup', 'openlayers/mapwfsgetfeaturepopup_lblock', 'layer', $previewimg); $wfsgetfeature1->owner = $account_admin; R::store($wfsgetfeature1); // Register WFS get feature module $previewimg = 'web/images/module/wfsgetfeature.png'; $wfsgetfeature2 = $this->layout_model->createModule('WFSGetFeature', 'openlayers/mapwfsgetfeaturecontent_lblock', 'layer', $previewimg); $wfsgetfeature2->owner = $account_admin; R::store($wfsgetfeature2); // Register CKEditor module $previewimg = 'web/images/module/ckeditor.png'; $ckeditor1 = $this->layout_model->createModule('CKEditor', 'layout/ckeditor_lblock', NULL, $previewimg); $ckeditor1->owner = $account_admin; R::store($ckeditor1); // Register rating module $previewimg = 'web/images/module/rating.png'; $ratingmod1 = $this->layout_model->createModule('Rating', 'rating/rating_lblock', NULL, $previewimg); $ratingmod1->owner = $account_admin; R::store($ratingmod1); // Create rating blocks $ratingblock = $this->layout_model->createBlock('ratingblock', $ratingmod1, 2); $ratingblock->owner = $account_admin; R::storeAll(array($ratingblock)); // Assign layout blocks to slots $this->layout_model->slotAddBlock($pslot3, $ratingblock); // Create admin layout $layout_adm = $this->layout_model->create('admin', 'layout/admin'); $layout_adm->owner = $account_admin; R::store($layout_adm); // Create registered layout slots $aslot1 = $this->layout_model->createSlot('slot1', $layout_adm); $aslot2 = $this->layout_model->createSlot('slot2', $layout_adm); $aslot3 = $this->layout_model->createSlot('slot3', $layout_adm); $aslot4 = $this->layout_model->createSlot('slot4', $layout_adm); $aslot5 = $this->layout_model->createSlot('slot5', $layout_adm); $aslot1->owner = $account_admin; $aslot2->owner = $account_admin; $aslot3->owner = $account_admin; $aslot4->owner = $account_admin; $aslot5->owner = $account_admin; R::storeAll(array($aslot1, $aslot2, $aslot3, $aslot4, $aslot5)); // Create layout blocks $alblock1 = $this->layout_model->createBlock('menu1', $menublock1, 1, '', $menu1->id); $alblock1->owner = $account_admin; $alblock2 = $this->layout_model->createBlock('adminauthblock', $authblock, 2); $alblock2->owner = $account_admin; R::storeAll(array($alblock1, $alblock2)); // Assign layout blocks to slots $this->layout_model->slotAddBlock($aslot2, $alblock1); $this->layout_model->slotAddBlock($aslot1, $alblock2); // Create CKEditor Admin block $alblock3 = $this->layout_model->createBlock('ckeditorblock', $ckeditor1, 1); $alblock3->owner = $account_admin; R::storeAll(array($alblock3)); // Assign CKEditor block to admin layout slot $this->layout_model->slotAddBlock($aslot1, $alblock3); // Create CKEditor User block $rlblock3 = $this->layout_model->createBlock('ckeditorblock', $ckeditor1, 1); $rlblock3->owner = $account_admin; R::storeAll(array($rlblock3)); // Assign CKEditor block to user layout slot $this->layout_model->slotAddBlock($rslot1, $rlblock3); // Add admin menu items $menuitems[] = $this->modmenu_model->addItem('Admin Home', 'admin/admin', 1, $menu1, 1); $menuitems[] = $this->modmenu_model->addItem('Frontpage', '', 1, $menu1, 2); $menuitems[] = $this->modmenu_model->addItem('User Home', 'user/user', 1, $menu1, 3); $menuitems[] = $this->modmenu_model->addItem('System', 'admin/adminsystem', 1, $menu1, 4); foreach ($menuitems as $menuitem) { $menuitem->owner = $account_admin; } R::storeAll($menuitems); // Mapserver // Create metadata $msmetadata[] = $this->mapserver_model->createMetadata('wms_encoding'); $msmetadata[] = $this->mapserver_model->createMetadata('wms_title'); $msmetadata[] = $this->mapserver_model->createMetadata('wms_abstract'); $msmetadata[] = $this->mapserver_model->createMetadata('wms_srs'); $msmetadata[] = $this->mapserver_model->createMetadata('wms_onlineresource'); $msmetadata[] = $this->mapserver_model->createMetadata('gml_include_items'); $msmetadata[] = $this->mapserver_model->createMetadata('wms_extent'); $msmetadata[] = $this->mapserver_model->createMetadata('ows_srs'); $msmetadata[] = $this->mapserver_model->createMetadata('ows_enable_request'); $msmetadata[] = $this->mapserver_model->createMetadata('wms_feature_info_mime_type'); $msmetadata[] = $this->mapserver_model->createMetadata('wfs_title'); $msmetadata[] = $this->mapserver_model->createMetadata('wfs_onlineresource'); $msmetadata[] = $this->mapserver_model->createMetadata('wfs_abstract'); $msmetadata[] = $this->mapserver_model->createMetadata('wfs_srs'); $msmetadata[] = $this->mapserver_model->createMetadata('wfs_enable_request'); $msmetadata[] = $this->mapserver_model->createMetadata('gml_featureid'); foreach ($msmetadata as &$item) { $item->owner = $account_admin; } R::storeAll($msmetadata); // Create units $msunits[] = $this->mapserver_model->createUnits('dd'); $msunits[] = $this->mapserver_model->createUnits('feet'); $msunits[] = $this->mapserver_model->createUnits('inches'); $msunits[] = $this->mapserver_model->createUnits('kilometers'); $msunits[] = $this->mapserver_model->createUnits('meters'); $msunits[] = $this->mapserver_model->createUnits('miles'); $msunits[] = $this->mapserver_model->createUnits('nauticalmiles'); $msunits[] = $this->mapserver_model->createUnits('percentages'); $msunits[] = $this->mapserver_model->createUnits('pixels'); foreach ($msunits as &$item) { $item->owner = $account_admin; } R::storeAll($msunits); // Create Layer Types $mslayertype[] = $this->mapserver_model->createLayerType('annotation'); $mslayertype[] = $this->mapserver_model->createLayerType('chart'); $mslayertype[] = $this->mapserver_model->createLayerType('circle'); $mslayertype[] = $this->mapserver_model->createLayerType('line'); $mslayertype[] = $this->mapserver_model->createLayerType('point'); $mslayertype[] = $this->mapserver_model->createLayerType('polygon'); $mslayertype[] = $this->mapserver_model->createLayerType('raster'); $mslayertype[] = $this->mapserver_model->createLayerType('query'); foreach ($mslayertype as &$item) { $item->owner = $account_admin; } R::storeAll($mslayertype); // Create Layer Connection Types $mslayerconntype[] = $this->mapserver_model->createLayerConnectionType('local'); $mslayerconntype[] = $this->mapserver_model->createLayerConnectionType('ogr'); $mslayerconntype[] = $this->mapserver_model->createLayerConnectionType('oraclespatial'); $mslayerconntype[] = $this->mapserver_model->createLayerConnectionType('plugin'); $mslayerconntype[] = $this->mapserver_model->createLayerConnectionType('postgis'); $mslayerconntype[] = $this->mapserver_model->createLayerConnectionType('sde'); $mslayerconntype[] = $this->mapserver_model->createLayerConnectionType('wfs'); $mslayerconntype[] = $this->mapserver_model->createLayerConnectionType('wms'); foreach ($mslayerconntype as &$item) { $item->owner = $account_admin; } R::storeAll($mslayerconntype); // Create a new map $map = $this->map_model->create('Demo Map', 'Simple map with OSM + WMS', 'demo'); $map->owner = $account_admin; $this->map_model->save($map); // Create layer $layer = $this->layer_model->create('Demo Layer', 'Data from shapefile', 'layer1'); $layer->owner = $account_admin; $this->layer_model->save($layer); // Add Layer to Map $this->map_model->addMapLayer($map, $layer); // Create wfsgetfeature block $wfsgetfeaturelblockconfig = '{ "popupfunction":"popupfeature", "htmlurl":null }'; $wfsgetfeaturelblock = $this->layout_model->createBlock('wfsgetfeature1', $wfsgetfeature2, 1, $wfsgetfeaturelblockconfig, $layer->id); $wfsgetfeaturelblock->owner = $account_admin; R::storeAll(array($wfsgetfeaturelblock)); // assignwfsgetfeature block $this->layout_model->slotAddBlock($pslot3, $wfsgetfeaturelblock); // Create mapfile $extent = '-20037508.34 -20037508.34 20037508.34 20037508.34'; $projection = "init=epsg:3857"; $mapfile = $this->mapserver_model->createMapfile($map, $extent, $projection); $mapfile->msunits = $msunits[4]; $mapfile->debug = 'off'; $mapfile->fontset = './mapfile/fonts/fonts.list'; $mapfile->symbolset = './mapfile/symbols/symbols.txt'; $mapfile->owner = $account_admin; $this->mapserver_model->save($mapfile); // Add metadata $this->mapserver_model->addMapfileMetadata($mapfile, $msmetadata[0], 'UTF8'); $this->mapserver_model->addMapfileMetadata($mapfile, $msmetadata[1], 'Cities'); $this->mapserver_model->addMapfileMetadata($mapfile, $msmetadata[2], 'No info'); $this->mapserver_model->addMapfileMetadata($mapfile, $msmetadata[3], 'EPSG:20790 EPSG:3857 EPSG:4326'); $this->mapserver_model->addMapfileMetadata($mapfile, $msmetadata[4], 'mapserver?map=' . $map->alias . '.map'); $this->mapserver_model->addMapfileMetadata($mapfile, $msmetadata[8], '*'); $this->mapserver_model->addMapfileMetadata($mapfile, $msmetadata[9], 'text/html'); // Create Mapserver Layer $extent = '-13207.017577 49518.222243 452964.525460 291327.263653'; $projection = "proj=tmerc lat_0=39.66666666666666 lon_0=1 k=1 x_0=200000 y_0=300000 ellps=intl towgs84=-304.046,-60.576,103.64,0,0,0,0 pm=lisbon units=m no_defs"; $mslayer = $this->mapserver_model->createLayer($layer, $extent, $projection); $mslayer->mslayertype = $mslayertype[4]; $mslayer->template = './mapfile/template/shape_feature_body.html'; $mslayer->data = './mapfile/shapefile/map1.shp'; $mslayer->labelitem = 'concelho'; $mslayer->owner = $account_admin; // Add Layer Metadata $this->mapserver_model->addLayerMetadata($mslayer, $msmetadata[5], 'all'); $this->mapserver_model->addLayerMetadata($mslayer, $msmetadata[7], 'EPSG:3857'); $this->mapserver_model->save($mslayer); $this->mapserver_model->addMapfileLayer($mapfile, $mslayer); // Create Layer Class $msclass = $this->mapserver_model->createClass($mslayer, 'Cities'); $msclass->owner = $account_admin; $this->mapserver_model->save($msclass); // Create MapIcons Point Style $msstyle1 = $this->mapserver_model->createStyle('Simple Map Icon'); $msstyle1->symbol = './mapfile/symbols/mapiconscollection-tourism/bigcity.png'; $msstyle1->size = 32; $msstyle1->color = '0 255 0'; $msstyle1->owner = $account_admin; $this->mapserver_model->save($msstyle1); // Add Style to Class $this->mapserver_model->addClassStyle($msclass, $msstyle1); // Create Default Point Style $msstyle3 = $this->mapserver_model->createStyle('Simple Icon'); $msstyle3->symbol = './mapfile/symbols/google-marker-small.png'; $msstyle3->size = 30; $msstyle3->color = '0 255 0'; $msstyle3->owner = $account_admin; $this->mapserver_model->save($msstyle3); // Create Default Area Style $msstyle2 = $this->mapserver_model->createStyle('Simple Area'); $msstyle2->symbol = ''; $msstyle2->owner = $account_admin; $this->mapserver_model->save($msstyle2); // Create Label $mslabel = $this->mapserver_model->createLabel('Simple Label'); $mslabel->owner = $account_admin; $this->mapserver_model->save($mslabel); // Add Label to Class $this->mapserver_model->addClassLabel($msclass, $mslabel); // Create Map Legend $mslegend = $this->mapserver_model->createLegend($mapfile); $mslegend->template = './mapfile/template/shape_legend_body.html'; $mslegend->owner = $account_admin; $this->mapserver_model->save($mslegend); // Update mapfile AFTER all mapfile is set $this->mapserver_model->updateMapfile($mapfile->id); // Add Label to Map Legend $this->mapserver_model->addLegendLabel($mslegend, $mslabel); // Register openlayers map block $previewimg = 'web/images/module/openlayersmap.png'; $olmapblock = $this->layout_model->createModule('OpenLayers Map', 'openlayers/modmap_lblock', 'olmap', $previewimg); $olmapblock->owner = $account_admin; R::store($olmapblock); // Create Openlayers Layers Type $ollayertype[] = $this->openlayers_model->createLayerType('OSM', 'OpenLayers.Layer.OSM'); $ollayertype[] = $this->openlayers_model->createLayerType('Google', 'OpenLayers.Layer.Google'); $ollayertype[] = $this->openlayers_model->createLayerType('Bing', 'OpenLayers.Layer.Bing'); $ollayertype[] = $this->openlayers_model->createLayerType('Internal WMS', 'OpenLayers.Layer.WMS'); $ollayertype[] = $this->openlayers_model->createLayerType('External WMS', 'OpenLayers.Layer.WMS'); foreach ($ollayertype as &$item) { $item->owner = $account_admin; } R::storeAll($ollayertype); // Create Abstract Layers $osmlayer = $this->layer_model->create('OSM', 'OpenStreetMap Layer', 'osm1'); $osmlayer->owner = $account_admin; $this->layer_model->save($osmlayer); $googlelayer = $this->layer_model->create('Google', 'Google Layer', 'google1'); $googlelayer->owner = $account_admin; $this->layer_model->save($googlelayer); $binglayer = $this->layer_model->create('Bing', 'Bing Layer', 'bing1'); $binglayer->owner = $account_admin; $this->layer_model->save($binglayer); // Add Layers to Map $this->map_model->addMapLayer($map, $osmlayer); $this->map_model->addMapLayer($map, $googlelayer); $this->map_model->addMapLayer($map, $binglayer); // Create Specific OL Layers // Create OSM Layer $opts = "{\n\"isBaseLayer\": true\n}"; $olosmlayer = $this->openlayers_model->createLayer($osmlayer, $ollayertype[0], '', $opts); $olosmlayer->owner = $account_admin; $this->openlayers_model->save($olosmlayer); // Create Google Layer $opts = "{\n\"isBaseLayer\": true\n}"; $vendoropts = "{\n\"type\":\"satellite\",\n\"numZoomLevels\":22\n}"; $olgooglelayer = $this->openlayers_model->createLayer($googlelayer, $ollayertype[1], '', $opts, $vendoropts); $olgooglelayer->owner = $account_admin; $this->openlayers_model->save($olgooglelayer); // Create Bing Layer $opts = "{\n\"isBaseLayer\": true\n}"; $vendoropts = "{\n\"name\": \"" . $binglayer->title . "\",\n\"key\":\"AqTGBsziZHIJYYxgivLBf0hVdrAk9mWO5cQcb8Yux8sW5M8c8opEC2lZqKR1ZZXf\",\n\"type\":\"Aerial\"\n}"; $olbinglayer = $this->openlayers_model->createLayer($binglayer, $ollayertype[2], '', $opts, $vendoropts); $olbinglayer->owner = $account_admin; $this->openlayers_model->save($olbinglayer); // Create WMS Mapserver Layer defined above $opts = "{\n\"isBaseLayer\": false,\n\"gutter\": 15\n}"; $vendoropts = "{\n\"layers\":\"" . $layer->alias . "\",\n\"transparent\": true,\n\"projection\":\"EPSG:20790\"\n}"; $url = $map->alias; $olwmslayer = $this->openlayers_model->createLayer($layer, $ollayertype[3], $url, $opts, $vendoropts); $olwmslayer->owner = $account_admin; $this->openlayers_model->save($olwmslayer); // Create Openlayers Map $olmap = $this->openlayers_model->createMap($map); $olmap->projection = 'EPSG:3857'; $olmap->owner = $account_admin; $this->openlayers_model->save($olmap); // Create feature search block $lblock6 = $this->layout_model->createBlock('searchblock', $searchblock, 1, '[]', $olmap->id); $lblock6->owner = $account_admin; R::storeAll(array($lblock6)); // Add OL Layers to OL Map $this->openlayers_model->addMapLayer($olmap, $olosmlayer); //$this->openlayers_model->addMapLayer($olmap, $olgooglelayer); //$this->openlayers_model->addMapLayer($olmap, $olbinglayer); $this->openlayers_model->addMapLayer($olmap, $olwmslayer); // Create publicmap layout block $olmapblockconfig = '{ "run":["layerswitcher1","wfsgetfeature1","searchblock"], "center":[-8.5,38.58], "zoom":8 }'; $lblock5 = $this->layout_model->createBlock('publicmap1', $olmapblock, 1, $olmapblockconfig, $olmap->id); $lblock5->owner = $account_admin; R::storeAll(array($lblock5)); // Add publicmap block to public layout slot $this->layout_model->slotAddBlock($pslot1, $lblock5); // Add Links to Mapserver Administration $menuitems[] = $this->modmenu_model->addItem("Explorer", 'admin/dataexplorer', 1, $menu1, 5); $menuitems[] = $this->modmenu_model->addItem("Tickets", 'admin/adminticket', 1, $menu1, 6); $menuitems[] = $this->modmenu_model->addItem("Maps", 'admin/adminmap', 1, $menu1, 7); $menuitems[] = $this->modmenu_model->addItem("Layers", 'admin/adminlayer', 1, $menu1, 8); $menuitems[] = $this->modmenu_model->addItem("Places", 'admin/adminpgplace', 1, $menu1, 9); $menuitems[] = $this->modmenu_model->addItem("MapServer Options", 'admin/adminmapserver', 1, $menu1, 10); $menuitems[] = $this->modmenu_model->addItem("OpenLayers Options", 'admin/adminopenlayers', 1, $menu1, 11); $menuitems[] = $this->modmenu_model->addItem("Google Earth Options", 'admin/admingoogleearth', 1, $menu1, 12); $menuitems[] = $this->modmenu_model->addItem("Import", 'admin/import', 1, $menu1, 13); foreach ($menuitems as &$item) { $item->owner = $account_admin; } R::storeAll($menuitems); // Register default files $this->dataexplorer_model->registerpath('./mapfile', 'private', $account_admin); // Create User Menu $menu2 = $this->modmenu_model->create('usermenu'); $menu2->owner = $account_admin; $this->modmenu_model->save($menu2); // Add user menu items $menu2items[] = $this->modmenu_model->addItem('Home', 'user/user', 1, $menu2, 1); $menu2items[] = $this->modmenu_model->addItem('Frontpage', '', 1, $menu2, 2); $menu2items[] = $this->modmenu_model->addItem('My Maps', 'user/managemap', 1, $menu2, 3); $menu2items[] = $this->modmenu_model->addItem('My Layers', 'user/managelayer', 1, $menu2, 4); $menu2items[] = $this->modmenu_model->addItem('My Places', 'user/managepgplace', 1, $menu2, 5); $menu2items[] = $this->modmenu_model->addItem('My Styles', 'user/managemsstyle', 1, $menu2, 6); $menu2items[] = $this->modmenu_model->addItem('My Tickets', 'user/manageticket', 1, $menu2, 7); $menu2items[] = $this->modmenu_model->addItem("Import", 'user/userimport', 1, $menu2, 8); foreach ($menu2items as &$item) { $item->owner = $account_admin; } R::storeAll($menu2items); // Create user menu block $rlblock3 = $this->layout_model->createBlock('usermenu1', $menublock1, 1, '', $menu2->id); $rlblock3->owner = $account_admin; R::storeAll(array($rlblock3)); // Assign layout blocks to slots $this->layout_model->slotAddBlock($rslot3, $rlblock3); // Create full screen layout $fullscreenedit_public = $this->layout_model->create('fullscreenedit', 'admin/fullscreenedit'); $fullscreenedit_public->owner = $account_admin; R::store($fullscreenedit_public); // Assign blocks to public layout $this->layout_model->slotAddBlock($pslot3, $lblock6); $this->layout_model->slotAddBlock($pslot3, $lblock4); // Register public controllers $ctl = $this->create('controller'); $ctl->path = 'publicmap'; $ctl->layout = $layout_public; $this->save($ctl); $ctl = $this->create('controller'); $ctl->path = 'auth'; $ctl->layout = $layout_mod; $this->save($ctl); $ctl = $this->create('controller'); $ctl->path = 'tickets'; $ctl->layout = $layout_mod; $this->save($ctl); // Register Google Earth map moduke $previewimg = 'web/images/module/openlayersmap.png'; $gemapmod = $this->layout_model->createModule('Google Earth Map', 'googleearth/modgemap_lblock', 'gemap', $previewimg); $gemapmod->owner = $account_admin; R::store($gemapmod); // Create Google Earth Layer Type $gelayertype[] = $this->googleearth_model->createLayerType('KML'); foreach ($gelayertype as &$item) { $item->owner = $account_admin; } R::storeAll($gelayertype); // Set Application version (should be last instruction) $this->database_model->setVersion($this->config->item('_version')); // Return success return true; }
/** * Test BIG INT primary key support. * * @return void */ public function testBigIntSupport() { R::nuke(); $createPageTableSQL = ' CREATE TABLE page ( id BIGSERIAL PRIMARY KEY, book_id BIGSERIAL, magazine_id BIGSERIAL, title VARCHAR(255) )'; $createBookTableSQL = ' CREATE TABLE book ( id BIGSERIAL PRIMARY KEY, title VARCHAR(255) )'; $createPagePageTableSQL = ' CREATE TABLE page_page ( id BIGSERIAL PRIMARY KEY, page_id BIGSERIAL, page2_id BIGSERIAL ) '; R::exec($createBookTableSQL); R::exec($createPageTableSQL); R::exec($createPagePageTableSQL); //insert some records $book1ID = '2223372036854775808'; $book2ID = '2223372036854775809'; $page1ID = '1223372036854775808'; $page2ID = '1223372036854775809'; $page3ID = '1223372036854775890'; $pagePage1ID = '3223372036854775808'; R::exec("ALTER SEQUENCE book_id_seq RESTART WITH {$book1ID}"); R::exec("ALTER SEQUENCE page_id_seq RESTART WITH {$page1ID}"); R::exec("ALTER SEQUENCE page_page_id_seq RESTART WITH {$pagePage1ID}"); $insertBook1SQL = "\n\t\t\tINSERT INTO book (title) VALUES( 'book 1' );\n\t\t"; $insertBook2SQL = "\n\t\t\tINSERT INTO book (title) VALUES( 'book 2' );\n\t\t"; $insertPage1SQL = "\n\t\t\tINSERT INTO page (id, book_id, title, magazine_id) VALUES( '{$page1ID}', '{$book1ID}', 'page 1 of book 1', '{$book2ID}' );\n\t\t"; $insertPage2SQL = "\n\t\t\tINSERT INTO page (id, book_id, title) VALUES( '{$page2ID}', '{$book1ID}', 'page 2 of book 1' );\n\t\t"; $insertPage3SQL = "\n\t\t\tINSERT INTO page (id, book_id, title) VALUES( '{$page3ID}', '{$book2ID}', 'page 1 of book 2' );\n\t\t"; $insertPagePage1SQL = "\n\t\t\tINSERT INTO page_page (id, page_id, page2_id) VALUES( '{$pagePage1ID}', '{$page2ID}', '{$page3ID}' );\n\t\t"; R::exec($insertBook1SQL); R::exec($insertBook2SQL); R::exec($insertPage1SQL); R::exec($insertPage2SQL); R::exec($insertPage3SQL); R::exec($insertPagePage1SQL); //basic tour of basic functions.... $book1 = R::load('book', $book1ID); asrt($book1->id, $book1ID); asrt($book1->title, 'book 1'); $book2 = R::load('book', $book2ID); asrt($book2->id, $book2ID); asrt($book2->title, 'book 2'); asrt(count($book1->ownPage), 2); asrt(count($book1->fresh()->with('LIMIT 1')->ownPage), 1); asrt(count($book1->fresh()->withCondition(' title = ? ', array('page 2 of book 1'))->ownPage), 1); asrt(count($book2->ownPage), 1); asrt($book2->fresh()->countOwn('page'), 1); $page1 = R::load('page', $page1ID); asrt(count($page1->sharedPage), 0); asrt($page1->fetchAs('book')->magazine->id, $book2ID); $page2 = R::load('page', $page2ID); asrt(count($page2->sharedPage), 1); asrt($page2->fresh()->countShared('page'), 1); $page3 = R::findOne('page', ' title = ? ', array('page 1 of book 2')); asrt($page3->id, $page3ID); asrt($page3->book->id, $book2ID); R::each($page2->fresh(), 'book', function ($page, $book) use($page2ID, $book1ID) { asrt($page->id, $page2ID); asrt($book->id, $book1ID); }); }
/** * Stored and reloads spatial data to see if the * value is preserved correctly. */ protected function setGetSpatial($data) { R::nuke(); $place = R::dispense('place'); $place->location = $data; //R::$f->GeomFromText('"'.$data.'"'); R::store($place); asrt(R::getCell('SELECT AsText(location) FROM place LIMIT 1'), $data); }
function clean() { R::nuke(); }
/** * SetGet function, sets a value in a bean and retrieves it again * after storage, useful for tests that want to make sure the same value * that gets in, comes out again. * * @param mixed $val the value that needs to be preserved * * @return mixed $val the value as returned after storage-retrieval cycle. */ function setget($val) { R::nuke(); $bean = R::dispense("page"); $bean->prop = $val; $id = R::store($bean); $bean = R::load("page", $id); return $bean->prop; }
/** * Test usage of named parameters in SQL snippets. * Issue #299 on Github. * * @return void */ public function testNamedParamsInSnippets() { testpack('Test whether we can use named parameters in SQL snippets.'); R::nuke(); $book = R::dispense('book'); $page = R::dispense('page'); $book->title = 'book'; R::associate($book, $page); //should not give error like: Uncaught [HY093] - SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters $books = R::related($page, 'book', ' title = :title ', array(':title' => 'book')); asrt(count($books), 1); //should not give error... $books = $page->withCondition(' title = :title ', array(':title' => 'book'))->sharedBook; asrt(count($books), 1); //should not give error... $links = R::$associationManager->related($page, 'book', TRUE, ' title = :title ', array(':title' => 'book')); asrt(count($links), 1); $book2 = R::dispense('book'); R::associate($book, $book2); //cross table, duplicate slots? $books = R::related($book2, 'book', ' title = :title ', array(':title' => 'book')); asrt(count($books), 1); R::nuke(); $book = R::dispense('book'); $page = R::dispense('page'); $book->title = 'book'; $book->comment = 'comment'; $page->title = 'page'; $book->ownPage[] = $page; R::store($book); //should also not give error.. $count = $book->countOwn('page'); asrt($count, 1); $book = $book->fresh(); //should also not give error.. $count = $book->withCondition(' title = ? ', array('page'))->countOwn('page'); asrt($count, 1); $book = $book->fresh(); //should also not give error.. $count = $book->withCondition(' title = :title ', array(':title' => 'page'))->countOwn('page'); asrt($count, 1); $book = $book->fresh(); $pages = $book->withCondition(' title = :title ', array(':title' => 'page'))->ownPage; asrt(count($pages), 1); //test with duplicate slots... $page = reset($pages); $page2 = R::dispense('page'); $page2->ownPage[] = $page; R::store($page2); $page2 = $page2->fresh(); $pages = $page2->withCondition(' title = :title ', array(':title' => 'page'))->ownPage; asrt(count($pages), 1); //test with find() $books = R::$redbean->find('book', array('title' => array('book')), ' AND title = :title ', array(':title' => 'book')); asrt(count($books), 1); $books = R::$redbean->find('book', array('title' => array('book', 'book2'), 'comment' => array('comment', 'comment2')), ' AND title = :title ', array(':title' => 'book')); asrt(count($books), 1); //just check numeric works as well... $books = R::$redbean->find('book', array('title' => array('book', 'book2'), 'comment' => array('comment', 'comment2')), ' AND title = ? ', array('book')); asrt(count($books), 1); //just extra check to verify glue works $books = R::$redbean->find('book', array('title' => array('book', 'book2'), 'comment' => array('comment', 'comment2')), ' ORDER BY id '); asrt(count($books), 1); //also check with preloader $book = $book->fresh(); R::preload($book, array('ownPage' => array('page', array(' title = :title ', array(':title' => 'page'))))); asrt(count($book->ownPage), 1); }
/** * Test special data types. * * @return void */ public function testSpecialDataTypes() { testpack('Special data types'); $bean = R::dispense('bean'); $bean->date = 'someday'; R::store($bean); $cols = R::getColumns('bean'); asrt($cols['date'], 'TEXT'); $bean = R::dispense('bean'); $bean->date = '2011-10-10'; R::nuke(); $bean = R::dispense('bean'); $bean->date = '2011-10-10'; R::store($bean); $cols = R::getColumns('bean'); asrt($cols['date'], 'NUMERIC'); }
fputs($fd, 'RewriteRule ^(ajax.*) $1 [L,NC,QSA]' . PHP_EOL . 'RewriteRule ^(assets)/(.*) $1/$2 [L,NC]' . PHP_EOL . 'RewriteRule ^.*$ index.php [L,QSA]' . PHP_EOL); fclose($fd); /* * Try opening the database and setting up the User table */ require 'rb.php'; try { $now = r::isodatetime(time() - date('Z')); # make sure the timestamp is in UTC (this should fix a weird problem with some XAMPP installations) $vals['dbhost'] = $cvalue['dbhost']; $vals['dbname'] = $cvalue['dbname']; $vals['dbuser'] = $cvalue['dbuser']; R::setup('mysql:host=' . $cvalue['dbhost'] . ';dbname=' . $cvalue['dbname'], $cvalue['dbuser'], $cvalue['dbpass']); # mysql initialiser R::freeze(FALSE); R::nuke(); # clear everything..... $user = R::dispense('user'); $user->email = $cvalue['email']; $user->login = $cvalue['admin']; $user->password = password_hash($cvalue['adminpw'], PASSWORD_DEFAULT); $user->active = 1; $user->confirm = 1; $user->joined = $now; R::store($user); /** * Now initialise the confirmation code table */ $conf = R::dispense('confirm'); $conf->code = 'this is a rubbish code'; $conf->issued = $now;
/** * Test BIG INT primary key support. * * @return void */ public function testBigIntSupport() { R::nuke(); $createPageTableSQL = ' CREATE TABLE `page` ( id BIGINT(20) UNSIGNED NOT NULL, book_id BIGINT(20) UNSIGNED NOT NULL, magazine_id BIGINT(20) UNSIGNED NOT NULL, title VARCHAR(255), PRIMARY KEY ( id ) ) ENGINE = InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT = 1223372036854775808'; $createBookTableSQL = ' CREATE TABLE `book` ( id BIGINT(20) UNSIGNED NOT NULL, title VARCHAR(255), PRIMARY KEY ( id ) ) ENGINE = InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT = 2223372036854775808'; $createPagePageTableSQL = ' CREATE TABLE `page_page` ( id BIGINT(20) UNSIGNED NOT NULL, page_id BIGINT(20) UNSIGNED NOT NULL, page2_id BIGINT(20) UNSIGNED NOT NULL, PRIMARY KEY ( id ) ) ENGINE = InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT = 3223372036854775808'; R::exec($createBookTableSQL); R::exec($createPageTableSQL); R::exec($createPagePageTableSQL); //insert some records $book1ID = '2223372036854775808'; $book2ID = '2223372036854775809'; $page1ID = '1223372036854775808'; $page2ID = '1223372036854775809'; $page3ID = '1223372036854775890'; $pagePage1ID = '3223372036854775808'; $insertBook1SQL = "\n\t\t\tINSERT INTO book (id, title) VALUES( '{$book1ID}', 'book 1' );\n\t\t"; $insertBook2SQL = "\n\t\t\tINSERT INTO book (id, title) VALUES( '{$book2ID}', 'book 2' );\n\t\t"; $insertPage1SQL = "\n\t\t\tINSERT INTO page (id, book_id, title, magazine_id) VALUES( '{$page1ID}', '{$book1ID}', 'page 1 of book 1', '{$book2ID}' );\n\t\t"; $insertPage2SQL = "\n\t\t\tINSERT INTO page (id, book_id, title) VALUES( '{$page2ID}', '{$book1ID}', 'page 2 of book 1' );\n\t\t"; $insertPage3SQL = "\n\t\t\tINSERT INTO page (id, book_id, title) VALUES( '{$page3ID}', '{$book2ID}', 'page 1 of book 2' );\n\t\t"; $insertPagePage1SQL = "\n\t\t\tINSERT INTO page_page (id, page_id, page2_id) VALUES( '{$pagePage1ID}', '{$page2ID}', '{$page3ID}' );\n\t\t"; R::exec($insertBook1SQL); R::exec($insertBook2SQL); R::exec($insertPage1SQL); R::exec($insertPage2SQL); R::exec($insertPage3SQL); R::exec($insertPagePage1SQL); //basic tour of basic functions.... $book1 = R::load('book', $book1ID); asrt($book1->id, $book1ID); asrt($book1->title, 'book 1'); $book2 = R::load('book', $book2ID); asrt($book2->id, $book2ID); asrt($book2->title, 'book 2'); asrt(count($book1->ownPage), 2); asrt(count($book1->fresh()->with('LIMIT 1')->ownPage), 1); asrt(count($book1->fresh()->withCondition(' title = ? ', array('page 2 of book 1'))->ownPage), 1); asrt(count($book2->ownPage), 1); asrt($book2->fresh()->countOwn('page'), 1); $page1 = R::load('page', $page1ID); asrt(count($page1->sharedPage), 0); asrt($page1->fetchAs('book')->magazine->id, $book2ID); $page2 = R::load('page', $page2ID); asrt(count($page2->sharedPage), 1); asrt($page2->fresh()->countShared('page'), 1); $page3 = R::findOne('page', ' title = ? ', array('page 1 of book 2')); asrt($page3->id, $page3ID); asrt($page3->book->id, $book2ID); R::each($page2->fresh(), 'book', function ($page, $book) use($page2ID, $book1ID) { asrt($page->id, $page2ID); asrt($book->id, $book1ID); }); }
/** * Various. * Tests whether writer correctly handles keyword 'group' and SQL state 23000 issue. * These tests remain here to make sure issues 9 and 10 never happen again. * However this bug will probably never re-appear due to changed architecture. * * @return void */ public function testIssue9and10() { $toolbox = R::$toolbox; $redbean = $toolbox->getRedBean(); $adapter = $toolbox->getDatabaseAdapter(); $a = new RedBean_AssociationManager($toolbox); $book = $redbean->dispense("book"); $author1 = $redbean->dispense("author"); $author2 = $redbean->dispense("author"); $book->title = "My First Post"; $author1->name = "Derek"; $author2->name = "Whoever"; $a->associate($book, $author1); $a->associate($book, $author2); pass(); testpack("Test Association Issue Group keyword (Issues 9 and 10)"); R::nuke(); $group = $redbean->dispense("group"); $group->name = "mygroup"; $redbean->store($group); try { $a->associate($group, $book); pass(); } catch (RedBean_Exception_SQL $e) { fail(); } // Test issue SQL error 23000 try { $a->associate($group, $book); pass(); } catch (RedBean_Exception_SQL $e) { fail(); } asrt((int) $adapter->getCell("select count(*) from book_group"), 1); //just 1 rec! }
/** * Tests whether we can update or unset a parent bean * with an alias without having to use fetchAs and * without loading the aliased bean causing table-not-found * errors. */ public function testUpdatingParentBeansWithAliases() { testpack('Test updating parent beans with aliases'); R::nuke(); $trans = R::dispense('transaction'); $seller = R::dispense('user'); $trans->seller = $seller; $id = R::store($trans); R::freeze(true); $trans = R::load('transaction', $id); //should not try to load seller, should not require fetchAs(). try { $trans->seller = R::dispense('user'); pass(); } catch (Exception $e) { fail(); } $trans = R::load('transaction', $id); //same for unset... try { unset($trans->seller); pass(); } catch (Exception $e) { fail(); } R::freeze(false); $account = R::dispense('user'); asrt(count($account->alias('seller')->ownTransaction), 0); $account->alias('seller')->ownTransaction = R::dispense('transaction', 10); $account->alias('boo'); //try to trick me... $id = R::store($account); R::freeze(true); $account = R::load('user', $id); asrt(count($account->alias('seller')->ownTransaction), 10); //you cannot unset a list unset($account->alias('seller')->ownTransaction); $id = R::store($account); $account = R::load('user', $id); asrt(count($account->alias('seller')->ownTransaction), 10); $account->alias('seller')->ownTransaction = array(); $id = R::store($account); $account = R::load('user', $id); asrt(count($account->alias('seller')->ownTransaction), 0); asrt(count($account->ownTransaction), 0); R::freeze(false); //but also make sure we don't cause extra column issue #335 R::nuke(); $building = R::dispense('building'); $village = R::dispense('village'); $building->village = $village; R::store($building); $building = $building->fresh(); $building->village = NULL; R::store($building); $building = $building->fresh(); $columns = R::inspect('building'); asrt(isset($columns['village']), false); asrt(isset($building->village), false); R::nuke(); $building = R::dispense('building'); $village = R::dispense('village'); $building->village = $village; R::store($building); $building = $building->fresh(); unset($building->village); R::store($building); $building = $building->fresh(); $columns = R::inspect('building'); asrt(isset($columns['village']), false); asrt(isset($building->village), false); $building = R::dispense('building'); $village = R::dispense('village'); $building->village = $village; R::store($building); $building = $building->fresh(); $building->village = false; R::store($building); $building = $building->fresh(); $columns = R::inspect('building'); asrt(isset($columns['village']), false); asrt(isset($building->village), false); }
/** * Test Read-support. * * @return void */ public function testUUIDReadSupport() { R::nuke(); $createPageTableSQL = ' CREATE TABLE `page` ( id CHAR( 40 ), book_id CHAR( 40 ), magazine_id CHAR( 40 ), title VARCHAR(255), PRIMARY KEY ( id ) ) ENGINE = InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci '; $createBookTableSQL = ' CREATE TABLE `book` ( id CHAR( 40 ), title VARCHAR(255), PRIMARY KEY ( id ) ) ENGINE = InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci '; $createPagePageTableSQL = ' CREATE TABLE `page_page` ( id CHAR( 40 ), page_id CHAR( 40 ), page2_id CHAR( 40 ), PRIMARY KEY ( id ) ) ENGINE = InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci '; R::exec($createBookTableSQL); R::exec($createPageTableSQL); R::exec($createPagePageTableSQL); //insert some records $book1ID = '6ccd780c-baba-1026-9564-0040f4311e21'; $book2ID = '6ccd780c-baba-1026-9564-0040f4311e22'; $page1ID = '6ccd780c-baba-1026-9564-0040f4311e23'; $page2ID = '6ccd780c-baba-1026-9564-0040f4311e24'; $page3ID = '6ccd780c-baba-1026-9564-0040f4311e25'; $pagePage1ID = '6ccd780c-baba-1026-9564-0040f4311e26'; $insertBook1SQL = "\n\t\t\tINSERT INTO book (id, title) VALUES( '{$book1ID}', 'book 1' );\n\t\t"; $insertBook2SQL = "\n\t\t\tINSERT INTO book (id, title) VALUES( '{$book2ID}', 'book 2' );\n\t\t"; $insertPage1SQL = "\n\t\t\tINSERT INTO page (id, book_id, title, magazine_id) VALUES( '{$page1ID}', '{$book1ID}', 'page 1 of book 1', '{$book2ID}' );\n\t\t"; $insertPage2SQL = "\n\t\t\tINSERT INTO page (id, book_id, title) VALUES( '{$page2ID}', '{$book1ID}', 'page 2 of book 1' );\n\t\t"; $insertPage3SQL = "\n\t\t\tINSERT INTO page (id, book_id, title) VALUES( '{$page3ID}', '{$book2ID}', 'page 1 of book 2' );\n\t\t"; $insertPagePage1SQL = "\n\t\t\tINSERT INTO page_page (id, page_id, page2_id) VALUES( '{$pagePage1ID}', '{$page2ID}', '{$page3ID}' );\n\t\t"; R::exec($insertBook1SQL); R::exec($insertBook2SQL); R::exec($insertPage1SQL); R::exec($insertPage2SQL); R::exec($insertPage3SQL); R::exec($insertPagePage1SQL); //basic tour of basic functions.... $book1 = R::load('book', $book1ID); asrt($book1->id, $book1ID); asrt($book1->title, 'book 1'); $book2 = R::load('book', $book2ID); asrt($book2->id, $book2ID); asrt($book2->title, 'book 2'); asrt(count($book1->ownPage), 2); asrt(count($book1->fresh()->with('LIMIT 1')->ownPage), 1); asrt(count($book1->fresh()->withCondition(' title = ? ', array('page 2 of book 1'))->ownPage), 1); asrt(count($book2->ownPage), 1); asrt($book2->fresh()->countOwn('page'), 1); $page1 = R::load('page', $page1ID); asrt(count($page1->sharedPage), 0); asrt($page1->fetchAs('book')->magazine->id, $book2ID); $page2 = R::load('page', $page2ID); asrt(count($page2->sharedPage), 1); asrt($page2->fresh()->countShared('page'), 1); $page3 = R::findOne('page', ' title = ? ', array('page 1 of book 2')); asrt($page3->id, $page3ID); asrt($page3->book->id, $book2ID); R::each($page2->fresh(), 'book', function ($page, $book) use($page2ID, $book1ID) { asrt($page->id, $page2ID); asrt($book->id, $book1ID); }); }
/** * * Same as above.. test keep cache. * * @return void */ public function testInstructNoDrop() { $str = 'SELECT * FROM ' . R::$writer->esc('bean', TRUE) . ' -- keep-cache'; $bean = R::dispense('bean'); $bean->title = 'abc'; $id = R::store($bean); $bean = R::load('bean', $id); $bean->title = 'xxx'; R::store($bean); R::exec($str); $bean = R::load('bean', $id); asrt($bean->title, 'abc'); R::nuke(); // Now INSTRUCT the cache to not drop the cache CASE 2 $str = 'SELECT * FROM ' . R::$writer->esc('bean', TRUE) . ' -- keep-cache'; $bean = R::dispense('bean'); $bean->title = 'abc'; $id = R::store($bean); $bean = R::load('bean', $id); $bean->title = 'xxx'; R::store($bean); R::findOne('bean', ' title = ? ', array('cache')); $bean = R::load('bean', $id); asrt($bean->title, 'xxx'); }