/** * Removes all sepcified tags from the bean. The tags specified in * the second parameter will no longer be associated with the bean. * * Tag list can be either an array with tag names or a comma separated list * of tag names. * * @param RedBean_OODBBean $bean tagged bean * @param array|string $tagList list of tags (names) * * @return void */ public function untag($bean, $tagList) { $tags = $this->extractTagsIfNeeded($tagList); foreach ($tags as $tag) { if ($t = $this->findTagByTitle($tag)) { $this->associationManager->unassociate($bean, $t); } } }
/** * Breaks the association between two beans. * This functions breaks the association between a pair of beans. After * calling this functions the beans will no longer be associated with * eachother. Calling related() with either one of the beans will no longer * return the other bean. * * @param RedBean_OODBBean $bean1 bean * @param RedBean_OODBBean $bean2 bean * * @return mixed */ public static function unassociate(RedBean_OODBBean $bean1, RedBean_OODBBean $bean2, $fast = false) { return self::$associationManager->unassociate($bean1, $bean2, $fast); }
/** * Test legacy methods, backward compatibility with removed * setAssoc function. * * @return void */ public function testBackwardCompat() { $toolbox = R::$toolbox; $adapter = $toolbox->getDatabaseAdapter(); $writer = $toolbox->getWriter(); $redbean = $toolbox->getRedBean(); $pdo = $adapter->getDatabase(); $rb = $redbean; $testA = $rb->dispense('testA'); $testB = $rb->dispense('testB'); $a = new RedBean_AssociationManager($toolbox); try { $a->related($testA, "testB"); pass(); } catch (Exception $e) { fail(); } $user = $redbean->dispense("user"); $user->name = "John"; $redbean->store($user); $page = $redbean->dispense("page"); $page->name = "John's page"; $redbean->store($page); $page2 = $redbean->dispense("page"); $page2->name = "John's second page"; $redbean->store($page2); $a = new RedBean_AssociationManager($toolbox); $a->associate($page, $user); asrt(count($a->related($user, "page")), 1); $a->associate($user, $page2); asrt(count($a->related($user, "page")), 2); // Can we fetch the assoc ids themselves? $pageKeys = $a->related($user, "page"); $pages = $redbean->batch("page", $pageKeys); $links = $redbean->batch("page_user", $a->related($user, "page", TRUE)); asrt(count($links), 2); // Confirm that the link beans are ok. $link = array_pop($links); asrt(isset($link->page_id), TRUE); asrt(isset($link->user_id), TRUE); asrt(isset($link->id), TRUE); $link = array_pop($links); asrt(isset($link->page_id), TRUE); asrt(isset($link->user_id), TRUE); asrt(isset($link->id), TRUE); $a->unassociate($page, $user); asrt(count($a->related($user, "page")), 1); $a->clearRelations($user, "page"); asrt(count($a->related($user, "page")), 0); $user2 = $redbean->dispense("user"); $user2->name = "Second User"; set1toNAssoc($a, $user2, $page); set1toNAssoc($a, $user, $page); asrt(count($a->related($user2, "page")), 0); asrt(count($a->related($user, "page")), 1); set1toNAssoc($a, $user, $page2); asrt(count($a->related($user, "page")), 2); $pages = $redbean->batch("page", $a->related($user, "page")); asrt(count($pages), 2); $apage = array_shift($pages); asrt($apage->name == "John's page" || $apage->name == "John's second page", TRUE); $apage = array_shift($pages); asrt($apage->name == "John's page" || $apage->name == "John's second page", TRUE); // Test save on the fly $page = $redbean->dispense("page"); $page2 = $redbean->dispense("page"); $page->name = "idless page 1"; $page2->name = "idless page 1"; $a->associate($page, $page2); asrt($page->id > 0, TRUE); asrt($page2->id > 0, TRUE); $idpage = $page->id; $idpage2 = $page2->id; $page = $redbean->dispense("page"); $page->name = "test page"; $id = $redbean->store($page); $user = $redbean->dispense("user"); $a->unassociate($user, $page); pass(); // No error $a->unassociate($page, $user); pass(); // No error $a->clearRelations($page, "user"); pass(); // No error $a->clearRelations($user, "page"); pass(); // No error $a->associate($user, $page); pass(); asrt(count($a->related($user, "page")), 1); asrt(count($a->related($page, "user")), 1); $a->clearRelations($user, "page"); pass(); // No error asrt(count($a->related($user, "page")), 0); asrt(count($a->related($page, "user")), 0); $page = $redbean->load("page", $id); pass(); asrt($page->name, "test page"); }
/** * Breaks the association between two beans. * This functions breaks the association between a pair of beans. After * calling this functions the beans will no longer be associated with * eachother. Calling related() with either one of the beans will no longer * return the other bean. * * @param RedBean_OODBBean $bean1 bean * @param RedBean_OODBBean $bean2 bean * * @return mixed */ public static function unassociate($beans1, $beans2, $fast = FALSE) { self::$associationManager->unassociate($beans1, $beans2, $fast); }
/** * Breaks the association between this OODBBean an the one belonging * to the other model. * @param RedBean_DomainObject $other */ protected function unassociate(RedBean_DomainObject $other) { $this->associationManager->unassociate($this->bean, $other->bean); }
$toolbox2 = new RedBean_ToolBox($redbean2, $adapter, $writer2); $a2 = new RedBean_AssociationManager($toolbox2); $a2->associate($movie1, $movie2); $movies = $a2->related($movie1, "movie"); asrt(count($movies), 1); asrt((int) $movies[0], (int) $movieid2); $movies = $a2->related($movie2, "movie"); asrt(count($movies), 1); asrt((int) $movies[0], (int) $movieid); $genre = $redbean2->dispense("genre"); $genre->name = "western"; $a2->associate($movie, $genre); $movies = $a2->related($genre, "movie"); asrt(count($movies), 1); asrt((int) $movies[0], (int) $movieid); $a2->unassociate($movie, $genre); $movies = $a2->related($genre, "movie"); asrt(count($movies), 0); $a2->clearRelations($movie, "movie"); $movies = $a2->related($movie1, "movie"); asrt(count($movies), 0); //$pdo->setDebugMode(0); testpack("Test Table Prefixes"); R::setup("pgsql:host={$ini['pgsql']['host']} dbname={$ini['pgsql']['schema']}", $ini['pgsql']['user'], $ini['pgsql']['pass']); class MyTableFormatter implements RedBean_IBeanFormatter { public function formatBeanTable($table) { return "xx_{$table}"; } public function formatBeanID($table)
/** * Test scanning and coding. * * @return void */ public function testScanningAndCoding() { $toolbox = R::$toolbox; $adapter = $toolbox->getDatabaseAdapter(); $writer = $toolbox->getWriter(); $redbean = $toolbox->getRedBean(); $pdo = $adapter->getDatabase(); $a = new RedBean_AssociationManager($toolbox); $this->dropTableIfExists($adapter, 'testtable'); asrt(in_array("testtable", $adapter->getCol("SELECT LOWER(table_name) FROM user_tables")), FALSE); $writer->createTable("testtable"); asrt(in_array("testtable", $adapter->getCol("SELECT LOWER(table_name) FROM user_tables")), TRUE); asrt(count(array_diff($writer->getTables(), $adapter->getCol("SELECT LOWER(table_name) FROM user_tables"))), 0); asrt(count(array_keys($writer->getColumns("testtable"))), 1); asrt(in_array("id", array_keys($writer->getColumns("testtable"))), TRUE); asrt(in_array("c1", array_keys($writer->getColumns("testtable"))), FALSE); $writer->addColumn("testtable", "c1", 1); asrt(count(array_keys($writer->getColumns("testtable"))), 2); asrt(in_array("c1", array_keys($writer->getColumns("testtable"))), TRUE); foreach ($writer->sqltype_typeno as $key => $type) { if ($type < 100) { asrt($writer->code($key), $type); } else { asrt($writer->code($key), 99); } } asrt($writer->code("unknown"), 99); asrt($writer->scanType(FALSE), 0); asrt($writer->scanType(NULL), 0); asrt($writer->scanType(2), 1); asrt($writer->scanType(255), 1); asrt($writer->scanType(256), 2); asrt($writer->scanType(-1), 3); asrt($writer->scanType(1.5), 3); asrt($writer->scanType(INF), 4); asrt($writer->scanType("abc"), 4); asrt($writer->scanType(str_repeat('abcd', 100000)), RedBean_QueryWriter_MySQL::C_DATATYPE_TEXT32); asrt($writer->scanType("2001-10-10", TRUE), RedBean_QueryWriter_Oracle::C_DATATYPE_SPECIAL_DATE); asrt($writer->scanType("2001-10-10 10:00:00", TRUE), RedBean_QueryWriter_Oracle::C_DATATYPE_SPECIAL_DATE); asrt($writer->scanType("2001-10-10 10:00:00.99", TRUE), RedBean_QueryWriter_Oracle::C_DATATYPE_SPECIAL_TIMESTAMP); asrt($writer->scanType("2001-10-10"), 4); asrt($writer->scanType("2001-10-10 10:00:00"), 4); asrt($writer->scanType("2001-10-10 10:00:00.99"), 4); //asrt($writer->scanType("POINT(1 2)",TRUE),RedBean_QueryWriter_MySQL::C_DATATYPE_SPECIAL_POINT); //asrt($writer->scanType("POINT(1 2)"),4); asrt($writer->scanType(str_repeat("lorem ipsum", 100)), 5); $writer->widenColumn("testtable", "c1", 2); $cols = $writer->getColumns("testtable"); asrt($writer->code($cols["c1"]), 2); $writer->widenColumn("testtable", "c1", 3); $cols = $writer->getColumns("testtable"); asrt($writer->code($cols["c1"]), 3); $writer->widenColumn("testtable", "c1", 4); $cols = $writer->getColumns("testtable"); asrt($writer->code($cols["c1"]), 4); $writer->widenColumn("testtable", "c1", 5); $cols = $writer->getColumns("testtable"); asrt($writer->code($cols["c1"]), 5); $id = $writer->updateRecord("testtable", array(array("property" => "c1", "value" => "lorem ipsum"))); $row = $writer->selectRecord("testtable", array("id" => array($id))); asrt($row[0]["c1"], "lorem ipsum"); $writer->updateRecord("testtable", array(array("property" => "c1", "value" => "ipsum lorem")), $id); $row = $writer->queryRecord("testtable", array("id" => array($id))); asrt($row[0]["c1"], "ipsum lorem"); $writer->deleteRecord("testtable", array("id" => array($id))); $row = $writer->queryRecord("testtable", array("id" => array($id))); asrt(empty($row), TRUE); //$pdo->setDebugMode(1); $writer->addColumn("testtable", "c2", 2); try { $writer->addUniqueIndex("testtable", array("c1", "c2")); echo 'to be fixed'; //fail(); //should fail, no content length blob } catch (RedBean_Exception_SQL $e) { pass(); } $writer->addColumn("testtable", "c3", 2); try { $writer->addUniqueIndex("testtable", array("c2", "c3")); pass(); //should fail, no content length blob } catch (RedBean_Exception_SQL $e) { fail(); } $a = $adapter->get("select INDEX_NAME from user_ind_columns where TABLE_NAME='TESTTABLE'"); asrt(count($a), 5); //asrt($a[1]["Key_name"],"UQ_64b283449b9c396053fe1724b4c685a80fd1a54d"); //asrt($a[2]["Key_name"],"UQ_64b283449b9c396053fe1724b4c685a80fd1a54d"); //Zero issue (FALSE should be stored as 0 not as '') testpack("Zero issue"); $this->dropTableIfExists($adapter, 'zero'); $bean = $redbean->dispense("zero"); $bean->zero = FALSE; $bean->title = "bla"; $redbean->store($bean); asrt(count($redbean->find("zero", array(), " zero = 0 ")), 1); R::store(R::dispense('hack')); testpack("Test RedBean Security - bean interface "); asrt(in_array("hack", $adapter->getCol("SELECT LOWER(table_name) FROM user_tables")), TRUE); $bean = $redbean->load("page", "13; drop table hack"); asrt(in_array("hack", $adapter->getCol("SELECT LOWER(table_name) FROM user_tables")), TRUE); try { $bean = $redbean->load("page where 1; drop table hack", 1); } catch (Exception $e) { } asrt(in_array("hack", $adapter->getCol("SELECT LOWER(table_name) FROM user_tables")), TRUE); $bean = $redbean->dispense("page"); $evil = "; drop table hack"; $bean->id = $evil; try { $redbean->store($bean); } catch (Exception $e) { } asrt(in_array("hack", $adapter->getCol("SELECT LOWER(table_name) FROM user_tables")), TRUE); unset($bean->id); $bean->name = "\"" . $evil; try { $redbean->store($bean); } catch (Exception $e) { } asrt(in_array("hack", $adapter->getCol("SELECT LOWER(table_name) FROM user_tables")), TRUE); $bean->name = "'" . $evil; try { $redbean->store($bean); } catch (Exception $e) { } asrt(in_array("hack", $adapter->getCol("SELECT LOWER(table_name) FROM user_tables")), TRUE); $bean->{$evil} = 1; try { $redbean->store($bean); } catch (Exception $e) { } asrt(in_array("hack", $adapter->getCol("SELECT LOWER(table_name) FROM user_tables")), TRUE); unset($bean->{$evil}); $bean->id = 1; $bean->name = "\"" . $evil; try { $redbean->store($bean); } catch (Exception $e) { } asrt(in_array("hack", $adapter->getCol("SELECT LOWER(table_name) FROM user_tables")), TRUE); $bean->name = "'" . $evil; try { $redbean->store($bean); } catch (Exception $e) { } asrt(in_array("hack", $adapter->getCol("SELECT LOWER(table_name) FROM user_tables")), TRUE); $bean->{$evil} = 1; try { $redbean->store($bean); } catch (Exception $e) { } asrt(in_array("hack", $adapter->getCol("SELECT LOWER(table_name) FROM user_tables")), TRUE); try { $redbean->trash($bean); } catch (Exception $e) { } asrt(in_array("hack", $adapter->getCol("SELECT LOWER(table_name) FROM user_tables")), TRUE); try { $redbean->find("::", array(), ""); } catch (Exception $e) { pass(); } $this->dropTableIfExists($adapter, 'sometable'); testpack("Test RedBean Security - query writer"); try { $writer->createTable("sometable` ( `id` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT , PRIMARY KEY ( `id` ) ) ENGINE = InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ; drop table hack; --"); } catch (Exception $e) { } asrt(in_array("hack", $adapter->getCol("SELECT LOWER(table_name) FROM user_tables")), TRUE); testpack("Test ANSI92 issue in clearrelations"); $this->dropTableIfExists($adapter, 'book_group'); $this->dropTableIfExists($adapter, 'author_book'); $this->dropTableIfExists($adapter, 'book'); $this->dropTableIfExists($adapter, 'author'); $redbean = $toolbox->getRedBean(); $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"; set1toNAssoc($a, $book, $author1); set1toNAssoc($a, $book, $author2); pass(); $this->dropTableIfExists($adapter, 'book_group'); $this->dropTableIfExists($adapter, 'book_author'); $this->dropTableIfExists($adapter, 'author_book'); $this->dropTableIfExists($adapter, 'book'); $this->dropTableIfExists($adapter, 'author'); $redbean = $toolbox->getRedBean(); $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)"); $this->dropTableIfExists($adapter, 'book_group'); $this->dropTableIfExists($adapter, '"group"'); $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! $this->dropTableIfExists($adapter, 'book_group'); $this->dropTableIfExists($adapter, 'author_book'); $this->dropTableIfExists($adapter, 'book'); $this->dropTableIfExists($adapter, 'author'); $redbean = $toolbox->getRedBean(); $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->unassociate($book, $author1); $a->unassociate($book, $author2); pass(); $redbean->trash($redbean->dispense("bla")); pass(); $bean = $redbean->dispense("bla"); $bean->name = 1; $bean->id = 2; $redbean->trash($bean); pass(); }
/** * Breaks the association between two beans. * This functions breaks the association between a pair of beans. After * calling this functions the beans will no longer be associated with * eachother. Calling related() with either one of the beans will no longer * return the other bean. * * @param RedBean_OODBBean $bean1 bean * @param RedBean_OODBBean $bean2 bean * * @return mixed */ public static function unassociate($beans1, $beans2, $fast = false) { return self::$associationManager->unassociate($beans1, $beans2, $fast); }
/** * Breaks the association between two beans. * This functions breaks the association between a pair of beans. After * calling this functions the beans will no longer be associated with * eachother. Calling related() with either one of the beans will no longer * return the other bean. * * @param RedBean_OODBBean $bean1 bean * @param RedBean_OODBBean $bean2 bean * * @return mixed */ public function unassociate($beans1, $beans2, $fast = FALSE) { $this->associationManager->unassociate($beans1, $beans2, $fast); }
} asrt((int) $adapter->getCell("select count(*) from book_group"), 1); //just 1 rec! $pdo->Execute("DROP TABLE IF EXISTS book"); $pdo->Execute("DROP TABLE IF EXISTS author"); $pdo->Execute("DROP TABLE IF EXISTS book_author"); $pdo->Execute("DROP TABLE IF EXISTS author_book"); $redbean = $toolbox->getRedBean(); $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->unassociate($book, $author1); $a->unassociate($book, $author2); pass(); $redbean->trash($redbean->dispense("bla")); pass(); $bean = $redbean->dispense("bla"); $bean->name = 1; $bean->id = 2; $redbean->trash($bean); pass(); //Can we save and load? -- with empty properties? $book = $redbean->dispense("book"); $id = $redbean->store($book); $book = $redbean->load("book", $id); $id = $redbean->store($book); pass();
/** * Test various. * Test various somewhat uncommon trash/unassociate scenarios. * (i.e. unassociate unrelated beans, trash non-persistant beans etc). * Should be handled gracefully - no output checking. * * @return void */ public function testVaria2() { $toolbox = R::$toolbox; $redbean = $toolbox->getRedBean(); $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->unassociate($book, $author1); $a->unassociate($book, $author2); pass(); $redbean->trash($redbean->dispense("bla")); pass(); $bean = $redbean->dispense("bla"); $bean->name = 1; $bean->id = 2; $redbean->trash($bean); pass(); }
/** * Unassociates the list items in the trashcan. * * @param RedBean_OODBBean $bean bean * @param array $sharedTrashcan list * * @return void */ private function processSharedTrashcan($bean, $sharedTrashcan) { foreach ($sharedTrashcan as $trash) { $this->assocManager->unassociate($trash, $bean); } }
/** * Test self referential N-M relations (page_page). * * @return void */ public function testSelfReferential() { $toolbox = R::$toolbox; $adapter = $toolbox->getDatabaseAdapter(); $writer = $toolbox->getWriter(); $redbean = $toolbox->getRedBean(); $pdo = $adapter->getDatabase(); $a = new RedBean_AssociationManager($toolbox); $page = $redbean->dispense("page"); $page->name = "John's page"; $idpage = $redbean->store($page); $page2 = $redbean->dispense("page"); $page2->name = "John's second page"; $idpage2 = $redbean->store($page2); $a->associate($page, $page2); $ids = $a->related($page, "page"); asrt(count($ids), 1); asrt(intval(array_pop($ids)), intval($idpage2)); $ids = $a->related($page2, "page"); asrt(count($ids), 1); asrt(intval(array_pop($ids)), intval($idpage)); $page3 = $redbean->dispense("page"); $page3->name = "third"; $page4 = $redbean->dispense("page"); $page4->name = "fourth"; $a->associate($page3, $page2); $a->associate($page2, $page4); $a->unassociate($page, $page2); asrt(count($a->related($page, "page")), 0); $ids = $a->related($page2, "page"); asrt(count($ids), 2); asrt(in_array($page3->id, $ids), TRUE); asrt(in_array($page4->id, $ids), TRUE); asrt(in_array($page->id, $ids), FALSE); asrt(count($a->related($page3, "page")), 1); asrt(count($a->related($page4, "page")), 1); $a->clearRelations($page2, "page"); asrt(count($a->related($page2, "page")), 0); asrt(count($a->related($page3, "page")), 0); asrt(count($a->related($page4, "page")), 0); try { $a->associate($page2, $page2); pass(); } catch (RedBean_Exception_SQL $e) { fail(); } $pageOne = $redbean->dispense("page"); $pageOne->name = "one"; $pageMore = $redbean->dispense("page"); $pageMore->name = "more"; $pageEvenMore = $redbean->dispense("page"); $pageEvenMore->name = "evenmore"; $pageOther = $redbean->dispense("page"); $pageOther->name = "othermore"; set1toNAssoc($a, $pageOther, $pageMore); set1toNAssoc($a, $pageOne, $pageMore); set1toNAssoc($a, $pageOne, $pageEvenMore); asrt(count($a->related($pageOne, "page")), 2); asrt(count($a->related($pageMore, "page")), 1); asrt(count($a->related($pageEvenMore, "page")), 1); asrt(count($a->related($pageOther, "page")), 0); }
/** * Breaks the association between two beans. * This functions breaks the association between a pair of beans. After * calling this functions the beans will no longer be associated with * eachother. Calling related() with either one of the beans will no longer * return the other bean. * * @param RedBean_OODBBean $bean1 bean * @param RedBean_OODBBean $bean2 bean * @param string $table specific the table name if needed * * @return mixed */ public static function unassociate(RedBean_OODBBean $bean1, RedBean_OODBBean $bean2, $fast = false, $table = null) { return static::$associationManager->unassociate($bean1, $bean2, $fast, $table); }
/** * (FALSE should be stored as 0 not as '') * * @return voids */ public function testZeroIssue() { testpack("Zero issue"); $toolbox = R::$toolbox; $redbean = $toolbox->getRedBean(); $adapter = $toolbox->getDatabaseAdapter(); $writer = $toolbox->getWriter(); $pdo = $adapter->getDatabase(); $pdo->Execute("DROP TABLE IF EXISTS `zero`"); $bean = $redbean->dispense("zero"); $bean->zero = FALSE; $bean->title = "bla"; $redbean->store($bean); asrt(count($redbean->find("zero", array(), " zero = 0 ")), 1); R::store(R::dispense('hack')); testpack("Test RedBean Security - bean interface "); asrt(in_array("hack", $adapter->getCol("show tables")), TRUE); $bean = $redbean->load("page", "13; drop table hack"); asrt(in_array("hack", $adapter->getCol("show tables")), TRUE); try { $bean = $redbean->load("page where 1; drop table hack", 1); } catch (Exception $e) { } asrt(in_array("hack", $adapter->getCol("show tables")), TRUE); $bean = $redbean->dispense("page"); $evil = "; drop table hack"; $bean->id = $evil; try { $redbean->store($bean); } catch (Exception $e) { } asrt(in_array("hack", $adapter->getCol("show tables")), TRUE); unset($bean->id); $bean->name = "\"" . $evil; try { $redbean->store($bean); } catch (Exception $e) { } asrt(in_array("hack", $adapter->getCol("show tables")), TRUE); $bean->name = "'" . $evil; try { $redbean->store($bean); } catch (Exception $e) { } asrt(in_array("hack", $adapter->getCol("show tables")), TRUE); $bean->{$evil} = 1; try { $redbean->store($bean); } catch (Exception $e) { } asrt(in_array("hack", $adapter->getCol("show tables")), TRUE); unset($bean->{$evil}); $bean->id = 1; $bean->name = "\"" . $evil; try { $redbean->store($bean); } catch (Exception $e) { } asrt(in_array("hack", $adapter->getCol("show tables")), TRUE); $bean->name = "'" . $evil; try { $redbean->store($bean); } catch (Exception $e) { } asrt(in_array("hack", $adapter->getCol("show tables")), TRUE); $bean->{$evil} = 1; try { $redbean->store($bean); } catch (Exception $e) { } asrt(in_array("hack", $adapter->getCol("show tables")), TRUE); try { $redbean->trash($bean); } catch (Exception $e) { } asrt(in_array("hack", $adapter->getCol("show tables")), TRUE); try { $redbean->find("::", array(), ""); } catch (Exception $e) { pass(); } $adapter->exec("drop table if exists sometable"); testpack("Test RedBean Security - query writer"); try { $writer->createTable("sometable` ( `id` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT , PRIMARY KEY ( `id` ) ) ENGINE = InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ; drop table hack; --"); } catch (Exception $e) { } asrt(in_array("hack", $adapter->getCol("show tables")), TRUE); testpack("Test ANSI92 issue in clearrelations"); $pdo->Execute("DROP TABLE IF EXISTS book_group"); $pdo->Execute("DROP TABLE IF EXISTS author_book"); $pdo->Execute("DROP TABLE IF EXISTS book"); $pdo->Execute("DROP TABLE IF EXISTS author"); $redbean = $toolbox->getRedBean(); $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"; set1toNAssoc($a, $book, $author1); set1toNAssoc($a, $book, $author2); pass(); $pdo->Execute("DROP TABLE IF EXISTS book_group"); $pdo->Execute("DROP TABLE IF EXISTS book_author"); $pdo->Execute("DROP TABLE IF EXISTS author_book"); $pdo->Execute("DROP TABLE IF EXISTS book"); $pdo->Execute("DROP TABLE IF EXISTS author"); $redbean = $toolbox->getRedBean(); $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)"); $pdo->Execute("DROP TABLE IF EXISTS `book_group`"); $pdo->Execute("DROP TABLE IF EXISTS `group`"); $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! $pdo->Execute("DROP TABLE IF EXISTS book_group"); $pdo->Execute("DROP TABLE IF EXISTS author_book"); $pdo->Execute("DROP TABLE IF EXISTS book"); $pdo->Execute("DROP TABLE IF EXISTS author"); $redbean = $toolbox->getRedBean(); $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->unassociate($book, $author1); $a->unassociate($book, $author2); pass(); $redbean->trash($redbean->dispense("bla")); pass(); $bean = $redbean->dispense("bla"); $bean->name = 1; $bean->id = 2; $redbean->trash($bean); pass(); }
/** * Tests freezing the database. * After freezing the database, schema modifications are no longer * allowed and referring to missing columns will now cause exceptions. * * @return void */ public function testFreezer() { $toolbox = R::$toolbox; $adapter = $toolbox->getDatabaseAdapter(); $writer = $toolbox->getWriter(); $redbean = $toolbox->getRedBean(); $pdo = $adapter->getDatabase(); $a = new RedBean_AssociationManager($toolbox); $post = $redbean->dispense('post'); $post->title = 'title'; $redbean->store($post); $page = $redbean->dispense('page'); $page->name = 'title'; $redbean->store($page); $page = $redbean->dispense("page"); $page->name = "John's page"; $idpage = $redbean->store($page); $page2 = $redbean->dispense("page"); $page2->name = "John's second page"; $idpage2 = $redbean->store($page2); $a->associate($page, $page2); $redbean->freeze(TRUE); $page = $redbean->dispense("page"); $page->sections = 10; $page->name = "half a page"; try { $id = $redbean->store($page); fail(); } catch (RedBean_Exception_SQL $e) { pass(); } $post = $redbean->dispense("post"); $post->title = "existing table"; try { $id = $redbean->store($post); pass(); } catch (RedBean_Exception_SQL $e) { fail(); } asrt(in_array("name", array_keys($writer->getColumns("page"))), TRUE); asrt(in_array("sections", array_keys($writer->getColumns("page"))), FALSE); $newtype = $redbean->dispense("newtype"); $newtype->property = 1; try { $id = $redbean->store($newtype); fail(); } catch (RedBean_Exception_SQL $e) { pass(); } $logger = RedBean_Plugin_QueryLogger::getInstanceAndAttach($adapter); // Now log and make sure no 'describe SQL' happens $page = $redbean->dispense("page"); $page->name = "just another page that has been frozen..."; $id = $redbean->store($page); $page = $redbean->load("page", $id); $page->name = "just a frozen page..."; $redbean->store($page); $page2 = $redbean->dispense("page"); $page2->name = "an associated frozen page"; $a->associate($page, $page2); $a->related($page, "page"); $a->unassociate($page, $page2); $a->clearRelations($page, "page"); $items = $redbean->find("page", array(), array("1")); $redbean->trash($page); $redbean->freeze(FALSE); asrt(count($logger->grep("SELECT")) > 0, TRUE); asrt(count($logger->grep("describe")) < 1, TRUE); asrt(is_array($logger->getLogs()), TRUE); }