Version Information RedBean Version @version 4
Author: Gabor de Mooij and the RedBeanPHP Community
Example #1
0
 public function testDatabaseInstallation()
 {
     $this->url('index.php');
     //All Fields initially with empty
     $this->fillFields(array('hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'phpback_test', 'adminemail' => '*****@*****.**', 'adminname' => 'admin', 'adminpass' => 'admin', 'adminrpass' => 'admin'));
     //Submit form
     $this->byName('install-form')->submit();
     //Should delete first intallation files
     $this->assertFileNotExists('install/index.php');
     $this->assertFileNotExists('install/install1.php');
     $this->assertFileNotExists('install/database_tables.sql');
     //Should create configuration file
     $this->assertFileExists('application/config/database.php');
     include 'application/config/database.php';
     $this->assertEquals($db['default']['username'], 'root');
     $this->assertEquals($db['default']['password'], '');
     $this->assertEquals($db['default']['database'], 'phpback_test');
     $this->assertEquals($db['default']['username'], 'root');
     $this->assertEquals($db['default']['dbdriver'], 'mysqli');
     //Should have updated database with new tables
     $this->assertEquals(RedBean::inspect(), array('_sessions', 'categories', 'comments', 'flags', 'ideas', 'logs', 'settings', 'users', 'votes'));
     //Should have created the admin user
     $adminUser = RedBean::load('users', 1);
     $this->assertEquals($adminUser->name, 'admin');
     $this->assertEquals($adminUser->email, '*****@*****.**');
     $this->assertEquals($adminUser->isadmin, '3');
     $this->assertEquals($adminUser->votes, '20');
 }
Example #2
0
 /**
  * Tests R::getInsertID convenience method.
  *
  * @return void
  */
 public function testGetInsertID()
 {
     R::nuke();
     $id = R::store(R::dispense('book'));
     $id2 = R::getInsertID();
     asrt($id, $id2);
 }
Example #3
0
 /**
  * Test SQLite table rebuilding.
  *
  * @return void
  */
 public function testRebuilder()
 {
     $toolbox = R::getToolBox();
     $adapter = $toolbox->getDatabaseAdapter();
     $writer = $toolbox->getWriter();
     $redbean = $toolbox->getRedBean();
     $pdo = $adapter->getDatabase();
     $book = R::dispense('book');
     $page = R::dispense('page');
     $book->xownPage[] = $page;
     $id = R::store($book);
     $book = R::load('book', $id);
     asrt(count($book->xownPage), 1);
     asrt((int) R::getCell('SELECT COUNT(*) FROM page'), 1);
     R::trash($book);
     asrt((int) R::getCell('SELECT COUNT(*) FROM page'), 0);
     $book = R::dispense('book');
     $page = R::dispense('page');
     $book->xownPage[] = $page;
     $id = R::store($book);
     $book = R::load('book', $id);
     asrt(count($book->xownPage), 1);
     asrt((int) R::getCell('SELECT COUNT(*) FROM page'), 1);
     $book->added = 2;
     R::store($book);
     $book->added = 'added';
     R::store($book);
     R::trash($book);
     asrt((int) R::getCell('SELECT COUNT(*) FROM page'), 0);
 }
Example #4
0
 /**
  * Test integration with pre-existing schemas.
  *
  * @return void
  */
 public function testPlaysNiceWithPreExitsingSchema()
 {
     $toolbox = R::getToolBox();
     $adapter = $toolbox->getDatabaseAdapter();
     $writer = $toolbox->getWriter();
     $redbean = $toolbox->getRedBean();
     $pdo = $adapter->getDatabase();
     $a = new 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);
     $adapter->exec("ALTER TABLE " . $writer->esc('page') . "\n\t\tCHANGE " . $writer->esc('name') . " " . $writer->esc('name') . "\n\t\tVARCHAR( 254 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL ");
     $page = $redbean->dispense("page");
     $page->name = "Just Another Page In a Table";
     $cols = $writer->getColumns("page");
     asrt($cols["name"], "varchar(254)");
     //$pdo->SethMode(1);
     $redbean->store($page);
     pass();
     // No crash?
     $cols = $writer->getColumns("page");
     asrt($cols["name"], "varchar(254)");
     //must still be same
 }
Example #5
0
 /**
  * Does the toolbox contain the necessary tools ?
  * 
  * @return void
  */
 public function testDoesToolboxContainTheTools()
 {
     $toolbox = R::getToolBox();
     asrt($toolbox->getDatabaseAdapter() instanceof Adapter, TRUE);
     asrt($toolbox->getRedBean() instanceof OODB, TRUE);
     asrt($toolbox->getWriter() instanceof QueryWriter, TRUE);
 }
Example #6
0
 /**
  * Test tainted.
  *
  * @return void
  */
 public function testTainted()
 {
     testpack('Original Tainted Tests');
     $redbean = R::getRedBean();
     $spoon = $redbean->dispense("spoon");
     asrt($spoon->getMeta("tainted"), TRUE);
     $spoon->dirty = "yes";
     asrt($spoon->getMeta("tainted"), TRUE);
     testpack('Tainted List test');
     $note = R::dispense('note');
     $note->text = 'abc';
     $note->ownNote[] = R::dispense('note')->setAttr('text', 'def');
     $id = R::store($note);
     $note = R::load('note', $id);
     asrt($note->isTainted(), FALSE);
     // Shouldn't affect tainted
     $note->text;
     asrt($note->isTainted(), FALSE);
     $note->ownNote;
     asrt($note->isTainted(), TRUE);
     testpack('Tainted Test Old Value');
     $text = $note->old('text');
     asrt($text, 'abc');
     asrt($note->hasChanged('text'), FALSE);
     $note->text = 'xxx';
     asrt($note->hasChanged('text'), TRUE);
     $text = $note->old('text');
     asrt($text, 'abc');
     testpack('Tainted Non-exist');
     asrt($note->hasChanged('text2'), FALSE);
     testpack('Misc Tainted Tests');
     $bean = R::dispense('bean');
     $bean->hasChanged('prop');
     $bean->old('prop');
 }
 public function logout()
 {
     $adminId = $this->app->getSession()->get('adminId');
     $sql = "delete from `adminsession` where `admin_id` = '" . $adminId . "'";
     R::exec($sql);
     $this->app->getSession()->remove('adminId');
 }
Example #8
0
 /**
  * Test varchar 191 condition.
  *
  * @return void
  */
 public function testInnoDBIndexLimit()
 {
     R::nuke();
     $book = R::dispense('book');
     $book->text = 'abcd';
     R::store($book);
     $columns = R::inspect('book');
     asrt(isset($columns['text']), TRUE);
     asrt($columns['text'], 'varchar(191)');
     $book = $book->fresh();
     $book->text = str_repeat('x', 190);
     R::store($book);
     $columns = R::inspect('book');
     asrt(isset($columns['text']), TRUE);
     asrt($columns['text'], 'varchar(191)');
     $book = $book->fresh();
     $book->text = str_repeat('x', 191);
     R::store($book);
     $columns = R::inspect('book');
     asrt(isset($columns['text']), TRUE);
     asrt($columns['text'], 'varchar(191)');
     $book = $book->fresh();
     $book->text = str_repeat('x', 192);
     R::store($book);
     $columns = R::inspect('book');
     asrt(isset($columns['text']), TRUE);
     asrt($columns['text'], 'varchar(255)');
 }
 public function uploadImage()
 {
     $error = '';
     $uploadedImages = array();
     if (isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") {
         // Find instance
         $id = $this->getParam($this->instanceName . '_id');
         if (!empty($id)) {
             /** @var \RedbeanPHP\SimpleModel $instanceBean */
             $instanceBean = R::findOne($this->model(), 'id = ?', array($id));
             /** @var \Skully\App\Models\Setting $instance */
             $instance = $instanceBean->box();
         } else {
             $instance = null;
         }
         try {
             $uploadedImages = $this->processUploadedImage($instance, $this->getParam('settingName'));
         } catch (\Exception $e) {
             $error = $e->getMessage();
         }
     }
     if (!empty($error)) {
         echo json_encode(array('error' => $error));
     } else {
         echo json_encode($uploadedImages);
     }
 }
Example #10
0
 /**
  * Test dump().
  *
  * @return void
  */
 public function testDump()
 {
     $beans = R::dispense('bean', 2);
     $beans[0]->name = 'hello';
     $beans[1]->name = 'world';
     $array = R::dump($beans);
     asrt(is_array($array), TRUE);
     foreach ($array as $item) {
         asrt(is_string($item), TRUE);
     }
     $beans[1]->name = 'world, and a very long string that should be shortened';
     $array = R::dump($beans);
     asrt(is_array($array), TRUE);
     asrt(strpos($array[1], '...'), 35);
     //just to get 100% test cov, we dont need to test this
     dmp($beans);
     pass();
     //test wrong input
     asrt(is_array(R::dump(NULL)), TRUE);
     asrt(count(R::dump(NULL)), 0);
     asrt(is_array(R::dump('')), TRUE);
     asrt(count(R::dump('')), 0);
     asrt(is_array(R::dump(1)), TRUE);
     asrt(count(R::dump(1)), 0);
     asrt(is_array(R::dump(TRUE)), TRUE);
     asrt(count(R::dump(FALSE)), 0);
 }
Example #11
0
 public function testDisableVoteIdea()
 {
     Scripts::LoginUser('*****@*****.**', 'Gates123');
     $this->url('home/profile/4');
     $this->byLinkText('Delete votes')->click();
     $voteidea = RedBean::load('ideas', 3);
     $this->assertEquals($voteidea->votes, '0');
 }
Example #12
0
 /**
  * Test boxing.
  *
  * @return void
  */
 public function testBasicBox()
 {
     $soup = R::dispense('soup');
     $soup->flavour = 'tomato';
     $this->giveMeSoup($soup->box());
     $this->giveMeBean($soup->box()->unbox());
     $this->giveMeBean($soup);
 }
Example #13
0
 /**
  * Cant have underscored beans
  * @expectedException \RedBeanPHP\RedException
  */
 public function testTwoWordsUnderscoredBean()
 {
     R::freeze(false);
     $testName = R::dispense('test_name');
     $id = R::store($testName);
     R::load('test_name', $id);
     R::freeze($this->frozen);
 }
 public function testCreateAdmin()
 {
     $this->migrate();
     $admin = $this->app->createModel('admin', array('name' => 'Admin', 'email' => '*****@*****.**', 'password' => 'lepass', 'password_confirmation' => 'lepass', 'status' => Admin::STATUS_ACTIVE));
     R::store($admin);
     $bean = R::findOne('admin', 'name = ?', array('Admin'));
     $this->assertEquals($bean->name, $admin->get('name'));
 }
Example #15
0
 public function getSettings()
 {
     $settings = array();
     $result = RedBean::findAll('settings');
     foreach ($result as $setting) {
         $settings[$setting->name] = $setting->value;
     }
     return $settings;
 }
Example #16
0
 /**
  * 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';
     $book->sharedPage[] = $page;
     R::store($book);
     //should not give error like: Uncaught [HY093] - SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters
     $books = $page->withCondition(' title = :title ', array(':title' => 'book'))->sharedBook;
     asrt(count($books), 1);
     //should not give error...
     $books = $page->withCondition(' title = :title ', array(':title' => 'book'))->sharedBook;
     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::getRedBean()->find('book', array('title' => array('book')), ' AND title = :title ', array(':title' => 'book'));
     asrt(count($books), 1);
     $books = R::getRedBean()->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::getRedBean()->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::getRedBean()->find('book', array('title' => array('book', 'book2'), 'comment' => array('comment', 'comment2')), ' ORDER BY id ');
     asrt(count($books), 1);
 }
Example #17
0
 /**
  * Test meta column type.
  * 
  * @return void
  */
 public function TypeColumn()
 {
     $book = R::dispense('book');
     $page = R::dispense('page');
     $page->book = $book;
     R::store($page);
     pass();
     asrt($page->getMeta('cast.book_id'), 'id');
 }
Example #18
0
 /**
  * Test fix for issue #512 - thanks for reporting Bernhard H.
  * OODBBean::__toString() implementation only works with C_ERR_IGNORE
  *
  * @return void
  */
 public function testToStringIssue512()
 {
     R::setErrorHandlingFUSE(\RedBeanPHP\OODBBean::C_ERR_FATAL);
     $boxedBean = R::dispense('boxedbean');
     $str = (string) $boxedBean;
     asrt($str, '{"id":0}');
     //no fatal error
     R::setErrorHandlingFUSE(FALSE);
 }
 /**
  * @return array
  */
 public function getResults()
 {
     if ($this->storedNumber > $this->maxResults) {
         $startFrom = $this->maxResults * $this->startPage;
         $extraSQL = 'ORDER BY id ASC LIMIT ' . $startFrom . ',' . $this->maxResults;
         return Facade::findAll($this->DBTable, $extraSQL);
     } else {
         return Facade::findAll($this->DBTable);
     }
 }
Example #20
0
 /**
  * You cant access meta data using the array accessors.
  *
  * @return void
  */
 public function testNoArrayMetaAccess()
 {
     $bean = R::dispense('bean');
     $bean->setMeta('greet', 'hello');
     asrt(isset($bean['greet']), FALSE);
     asrt(isset($bean['__info']['greet']), FALSE);
     asrt(isset($bean['__info']), FALSE);
     asrt(isset($bean['meta']), FALSE);
     asrt(count($bean), 1);
 }
Example #21
0
 /**
  * Test UTF8 handling.
  *
  * @return void
  */
 public function testUTF8()
 {
     $str = '𠜎ὃ𠻗𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜';
     $bean = R::dispense('bean');
     $bean->bla = $str;
     R::store($bean);
     $bean = R::load('bean', $bean->id);
     asrt($bean->bla, $str);
     pass();
 }
Example #22
0
 /**
  * Test for issue #386.
  * Can we use large numbers in LIMIT ?
  * 
  * @return void
  */
 public function testLargeNum()
 {
     $number = R::dispense('number');
     $number->name = 'big number';
     R::store($number);
     //This should not cause an error... (some people use LIMIT 0, HUGE to simulate OFFSET on MYSQL).
     $beans = R::findAll('number', ' LIMIT ? ', array(PHP_INT_MAX));
     asrt(is_array($beans), TRUE);
     asrt(count($beans), 1);
     pass();
 }
 public function getAdmin()
 {
     /** @var \Skully\Application $this */
     /** @var \RedBean_SimpleModel $adminBean */
     $adminBean = R::findOne('admin', "id = ?", array($this->getSession()->get('adminId')));
     if (!empty($adminBean)) {
         return $adminBean->box();
     } else {
         return null;
     }
 }
Example #24
0
 /**
  * Test the __toString method of the SQLHelper. 
  */
 public function testToString()
 {
     $toolbox = R::getToolBox();
     $adapter = $toolbox->getDatabaseAdapter();
     $sqlHelper = new SQLHelper($adapter);
     $sqlHelper->begin()->select('*')->from('table')->where('name = ?')->put('name');
     $str = (string) $sqlHelper;
     asrt(strpos($str, 'query') !== false, true);
     asrt(strpos($str, 'select * from table where name = ?') !== false, true);
     asrt(strpos($str, '=> Array') !== false, true);
     asrt(strpos($str, 'params') !== false, true);
 }
Example #25
0
 /**
  * Test basic labels.
  *
  * @return void
  */
 public function testLabels()
 {
     testpack('Test Labels');
     $meals = R::dispenseLabels('meal', array('meat', 'fish', 'vegetarian'));
     asrt(is_array($meals), TRUE);
     asrt(count($meals), 3);
     foreach ($meals as $m) {
         asrt($m instanceof OODBBean, TRUE);
     }
     $listOfMeals = implode(',', R::gatherLabels($meals));
     asrt($listOfMeals, 'fish,meat,vegetarian');
 }
 protected function login()
 {
     $admin = $this->app->createModel('admin', array('name' => 'Admin', 'email' => '*****@*****.**', 'password' => 'lepass', 'password_confirmation' => 'lepass', 'status' => Admin::STATUS_ACTIVE));
     R::store($admin);
     $params = array('email' => '*****@*****.**', 'password' => 'lepass');
     // This is the code to emulate sending parameter to a page:
     $this->app->runControllerFromRawUrl('admin/loginProcess', $params);
     // This is the alternative way we can do it:
     //        $controller = $this->app->getController('Admin\\Admins', 'loginProcess');
     //        $controller->setParams($params);
     //        $controller->runCurrentAction();
     $this->assertEquals(1, $this->app->getSession()->get('adminId'));
 }
Example #27
0
 /**
  * Test self referential N-M relations (page_page).
  * 
  * @return void
  */
 public function testSelfReferential()
 {
     $page = R::dispense('page')->setAttr('title', 'a');
     $page->sharedPage[] = R::dispense('page')->setAttr('title', 'b');
     R::store($page);
     $page = $page->fresh();
     $page = reset($page->sharedPage);
     asrt($page->title, 'b');
     $tables = array_flip(R::inspect());
     asrt(isset($tables['page_page']), true);
     $columns = R::inspect('page_page');
     asrt(isset($columns['page2_id']), true);
 }
Example #28
0
 /**
  * Store a new record.
  *
  * @param  Request  $request
  * @return Response
  */
 public function store(Request $request)
 {
     $item_id = $request->input('item_id');
     $item = R::load('item', $item_id);
     $attribute = R::dispense('attribute');
     $attribute->key = $request->input('key');
     $attribute->value = $request->input('value');
     $item->ownAttributeList[] = $attribute;
     R::store($item);
     echo "<pre>\n";
     var_dump($attribute->export());
     var_dump($item->export());
 }
Example #29
0
 /**
  * Test for issue90.
  * Checking 'own' relationship, makes it impossible to trash a bean.
  *
  * @return void
  */
 public function testIssue90()
 {
     $s = R::dispense('box');
     $s->name = 'a';
     $f = R::dispense('bottle');
     $s->ownBottle[] = $f;
     R::store($s);
     $s2 = R::dispense('box');
     $s2->name = 'a';
     R::store($s2);
     R::trash($s2);
     pass();
 }
Example #30
0
 /**
  * Test to make sure stash cache works with recursively opening models
  * with FUSE.
  *
  * @return void
  */
 public function testIssue259()
 {
     testpack('Testing Issue #259 - Stash Cache breaks model delegation in open().');
     $mother = R::dispense('mother');
     $mother->desc = 'I am mother';
     R::store($mother);
     $child = R::dispense('child');
     $child->mother = $mother;
     $child->desc = 'I am child';
     $id = R::store($child);
     R::findOne('child', ' id = ?', array($id));
     R::find('child', ' id = ? ', array($id));
     R::load('child', $id);
 }