3 namespace RedUNIT\Mysql;
5 use RedBeanPHP\Facade as R;
6 use RedBeanPHP\AssociationManager as AssociationManager;
7 use RedBeanPHP\QueryWriter as QueryWriter;
8 use RedBeanPHP\QueryWriter\MySQL as MySQL;
9 use RedBeanPHP\QueryWriter\AQueryWriter as AQueryWriter;
10 use RedBeanPHP\RedException\SQL as SQL;
11 use RedBeanPHP\RedException as RedException;
16 * Tests for MySQL and MariaDB Query Writer.
17 * This test class contains Query Writer specific tests.
18 * Use this class to add tests to test Query Writer specific
19 * behaviours, quirks and issues.
21 * @file RedUNIT/Mysql/Writer.php
22 * @desc A collection of database specific writer functions.
23 * @author Gabor de Mooij and the RedBeanPHP Community
24 * @license New BSD/GPLv2
26 * (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
27 * This source file is subject to the New BSD/GPLv2 License that is bundled
28 * with this source code in the file license.txt.
30 class Writer extends \RedUNIT\Mysql
34 * Test Facade bind function method.
35 * Test for MySQL WKT spatial format.
37 public function testFunctionFilters()
40 R::bindFunc( 'read', 'location.point', 'asText' );
41 R::bindFunc( 'write', 'location.point', 'GeomFromText' );
42 R::store(R::dispense('location'));
47 } catch( SQL $exception ) {
54 } catch( SQL $exception ) {
57 $location = R::dispense( 'location' );
58 $location->point = 'POINT(14 6)';
60 $columns = R::inspect( 'location' );
61 asrt( $columns['point'], 'point' );
62 $location = $location->fresh();
63 asrt( $location->point, 'POINT(14 6)' );
65 $location = R::dispense( 'location' );
66 $location->point = 'LINESTRING(0 0,1 1,2 2)';
68 $columns = R::inspect( 'location' );
69 asrt( $columns['point'], 'linestring' );
70 $location->bustcache = 2;
72 $location = $location->fresh();
73 asrt( $location->point, 'LINESTRING(0 0,1 1,2 2)' );
75 $location = R::dispense( 'location' );
76 $location->point = 'POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7,5 5))';
78 $columns = R::inspect( 'location' );
79 asrt( $columns['point'], 'polygon' );
80 $location->bustcache = 4;
82 $location = $location->fresh();
83 asrt( $location->point, 'POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7,5 5))' );
84 R::bindFunc( 'read', 'location.point', NULL );
85 $location->bustcache = 1;
87 $location = $location->fresh();
88 asrt( ( $location->point === 'POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7,5 5))' ), FALSE );
89 $filters = AQueryWriter::getSQLFilters();
90 asrt( is_array( $filters ), TRUE );
91 asrt( count( $filters ), 2 );
92 asrt( isset( $filters[ QueryWriter::C_SQLFILTER_READ] ), TRUE );
93 asrt( isset( $filters[ QueryWriter::C_SQLFILTER_WRITE] ), TRUE );
94 R::bindFunc( 'read', 'place.point', 'asText' );
95 R::bindFunc( 'write', 'place.point', 'GeomFromText' );
96 R::bindFunc( 'read', 'place.line', 'asText' );
97 R::bindFunc( 'write', 'place.line', 'GeomFromText' );
99 $place = R::dispense( 'place' );
100 $place->point = 'POINT(13.2 666.6)';
101 $place->line = 'LINESTRING(9.2 0,3 1.33)';
103 $columns = R::inspect( 'place' );
104 asrt( $columns['point'], 'point' );
105 asrt( $columns['line'], 'linestring' );
106 $place = R::findOne('place');
107 asrt( $place->point, 'POINT(13.2 666.6)' );
108 asrt( $place->line, 'LINESTRING(9.2 0,3 1.33)' );
109 R::bindFunc( 'read', 'place.point', NULL );
110 R::bindFunc( 'write', 'place.point', NULL );
111 R::bindFunc( 'read', 'place.line', NULL );
112 R::bindFunc( 'write', 'place.line', NULL );
116 * Test scanning and coding of values.
120 public function testScanningAndCoding()
122 $toolbox = R::getToolBox();
123 $adapter = $toolbox->getDatabaseAdapter();
124 $writer = $toolbox->getWriter();
125 $redbean = $toolbox->getRedBean();
126 $pdo = $adapter->getDatabase();
128 $a = new AssociationManager( $toolbox );
130 $adapter->exec( "DROP TABLE IF EXISTS testtable" );
132 asrt( in_array( "testtable", $adapter->getCol( "show tables" ) ), FALSE );
134 $writer->createTable( "testtable" );
136 asrt( in_array( "testtable", $adapter->getCol( "show tables" ) ), TRUE );
138 asrt( count( array_diff( $writer->getTables(), $adapter->getCol( "show tables" ) ) ), 0 );
139 asrt( count( array_keys( $writer->getColumns( "testtable" ) ) ), 1 );
141 asrt( in_array( "id", array_keys( $writer->getColumns( "testtable" ) ) ), TRUE );
142 asrt( in_array( "c1", array_keys( $writer->getColumns( "testtable" ) ) ), FALSE );
144 $writer->addColumn( "testtable", "c1", MySQL::C_DATATYPE_UINT32 );
146 asrt( count( array_keys( $writer->getColumns( "testtable" ) ) ), 2 );
148 asrt( in_array( "c1", array_keys( $writer->getColumns( "testtable" ) ) ), TRUE );
150 foreach ( $writer->sqltype_typeno as $key => $type ) {
152 asrt( $writer->code( $key, TRUE ), $type );
154 asrt( $writer->code( $key, TRUE ), MySQL::C_DATATYPE_SPECIFIED );
158 asrt( $writer->code( MySQL::C_DATATYPE_SPECIAL_DATETIME ), MySQL::C_DATATYPE_SPECIFIED );
160 asrt( $writer->code( "unknown" ), MySQL::C_DATATYPE_SPECIFIED );
162 asrt( $writer->scanType( FALSE ), MySQL::C_DATATYPE_BOOL );
163 asrt( $writer->scanType( TRUE ), MySQL::C_DATATYPE_BOOL );
164 asrt( $writer->scanType( INF ), MySQL::C_DATATYPE_TEXT7 );
166 asrt( $writer->scanType( NULL ), MySQL::C_DATATYPE_BOOL );
168 asrt( $writer->scanType( 2 ), MySQL::C_DATATYPE_UINT32 );
169 asrt( $writer->scanType( 255 ), MySQL::C_DATATYPE_UINT32 ); //no more uint8
170 asrt( $writer->scanType( 256 ), MySQL::C_DATATYPE_UINT32 );
172 asrt( $writer->scanType( -1 ), MySQL::C_DATATYPE_DOUBLE );
173 asrt( $writer->scanType( 1.5 ), MySQL::C_DATATYPE_DOUBLE );
175 asrt( $writer->scanType( "abc" ), MySQL::C_DATATYPE_TEXT7 );
177 asrt( $writer->scanType( str_repeat( 'abcd', 100000 ) ), MySQL::C_DATATYPE_TEXT32 );
179 asrt( $writer->scanType( "2001-10-10", TRUE ), MySQL::C_DATATYPE_SPECIAL_DATE );
181 asrt( $writer->scanType( "2001-10-10 10:00:00", TRUE ), MySQL::C_DATATYPE_SPECIAL_DATETIME );
183 asrt( $writer->scanType( "2001-10-10" ), MySQL::C_DATATYPE_TEXT7 );
185 asrt( $writer->scanType( "2001-10-10 10:00:00" ), MySQL::C_DATATYPE_TEXT7 );
187 asrt( $writer->scanType( "1.23", TRUE ), MySQL::C_DATATYPE_SPECIAL_MONEY );
188 asrt( $writer->scanType( "12.23", TRUE ), MySQL::C_DATATYPE_SPECIAL_MONEY );
189 asrt( $writer->scanType( "124.23", TRUE ), MySQL::C_DATATYPE_SPECIAL_MONEY );
191 asrt( $writer->scanType( str_repeat( "lorem ipsum", 100 ) ), MySQL::C_DATATYPE_TEXT16 );
193 $writer->widenColumn( "testtable", "c1", MySQL::C_DATATYPE_UINT32 );
195 $writer->addColumn( "testtable", "special", MySQL::C_DATATYPE_SPECIAL_DATE );
197 $cols = $writer->getColumns( "testtable" );
199 asrt( $writer->code( $cols['special'], TRUE ), MySQL::C_DATATYPE_SPECIAL_DATE );
201 asrt( $writer->code( $cols['special'], FALSE ), MySQL::C_DATATYPE_SPECIFIED );
203 $writer->addColumn( "testtable", "special2", MySQL::C_DATATYPE_SPECIAL_DATETIME );
205 $cols = $writer->getColumns( "testtable" );
207 asrt( $writer->code( $cols['special2'], TRUE ), MySQL::C_DATATYPE_SPECIAL_DATETIME );
209 asrt( $writer->code( $cols['special'], FALSE ), MySQL::C_DATATYPE_SPECIFIED );
211 $cols = $writer->getColumns( "testtable" );
213 asrt( $writer->code( $cols["c1"] ), MySQL::C_DATATYPE_UINT32 );
215 $writer->widenColumn( "testtable", "c1", MySQL::C_DATATYPE_DOUBLE );
217 $cols = $writer->getColumns( "testtable" );
219 asrt( $writer->code( $cols["c1"] ), MySQL::C_DATATYPE_DOUBLE );
221 $writer->widenColumn( "testtable", "c1", MySQL::C_DATATYPE_TEXT7 );
223 $cols = $writer->getColumns( "testtable" );
225 asrt( $writer->code( $cols["c1"] ), MySQL::C_DATATYPE_TEXT7 );
227 $writer->widenColumn( "testtable", "c1", MySQL::C_DATATYPE_TEXT8 );
229 $cols = $writer->getColumns( "testtable" );
231 asrt( $writer->code( $cols["c1"] ), MySQL::C_DATATYPE_TEXT8 );
233 $id = $writer->updateRecord( "testtable", array( array( "property" => "c1", "value" => "lorem ipsum" ) ) );
235 $row = $writer->queryRecord( "testtable", array( "id" => array( $id ) ) );
237 asrt( $row[0]["c1"], "lorem ipsum" );
239 $writer->updateRecord( "testtable", array( array( "property" => "c1", "value" => "ipsum lorem" ) ), $id );
241 $row = $writer->queryRecord( "testtable", array( "id" => array( $id ) ) );
243 asrt( $row[0]["c1"], "ipsum lorem" );
245 $writer->deleteRecord( "testtable", array( "id" => array( $id ) ) );
247 $row = $writer->queryRecord( "testtable", array( "id" => array( $id ) ) );
249 asrt( empty( $row ), TRUE );
251 $writer->addColumn( "testtable", "c2", MySQL::C_DATATYPE_UINT32 );
255 * (FALSE should be stored as 0 not as '')
259 public function testZeroIssue()
261 testpack( "Zero issue" );
263 $toolbox = R::getToolBox();
264 $redbean = $toolbox->getRedBean();
265 $adapter = $toolbox->getDatabaseAdapter();
266 $writer = $toolbox->getWriter();
267 $pdo = $adapter->getDatabase();
269 $pdo->Execute( "DROP TABLE IF EXISTS `zero`" );
271 $bean = $redbean->dispense( "zero" );
274 $bean->title = "bla";
276 $redbean->store( $bean );
278 asrt( count( $redbean->find( "zero", array(), " zero = 0 " ) ), 1 );
280 R::store( R::dispense( 'hack' ) );
282 testpack( "Test RedBean Security - bean interface " );
284 asrt( in_array( "hack", $adapter->getCol( "show tables" ) ), TRUE );
286 $bean = $redbean->load( "page", "13; drop table hack" );
288 asrt( in_array( "hack", $adapter->getCol( "show tables" ) ), TRUE );
290 $bean = $redbean->load( "page where 1; drop table hack", 1 );
291 } catch (\Exception $e ) {
294 asrt( in_array( "hack", $adapter->getCol( "show tables" ) ), TRUE );
296 $bean = $redbean->dispense( "page" );
298 $evil = "; drop table hack";
303 $redbean->store( $bean );
304 } catch (\Exception $e ) {
307 asrt( in_array( "hack", $adapter->getCol( "show tables" ) ), TRUE );
311 $bean->name = "\"" . $evil;
314 $redbean->store( $bean );
315 } catch (\Exception $e ) {
318 asrt( in_array( "hack", $adapter->getCol( "show tables" ) ), TRUE );
320 $bean->name = "'" . $evil;
323 $redbean->store( $bean );
324 } catch (\Exception $e ) {
327 asrt( in_array( "hack", $adapter->getCol( "show tables" ) ), TRUE );
332 $redbean->store( $bean );
333 } catch (\Exception $e ) {
336 asrt( in_array( "hack", $adapter->getCol( "show tables" ) ), TRUE );
338 unset( $bean->$evil );
341 $bean->name = "\"" . $evil;
344 $redbean->store( $bean );
345 } catch (\Exception $e ) {
348 asrt( in_array( "hack", $adapter->getCol( "show tables" ) ), TRUE );
350 $bean->name = "'" . $evil;
353 $redbean->store( $bean );
354 } catch (\Exception $e ) {
357 asrt( in_array( "hack", $adapter->getCol( "show tables" ) ), TRUE );
362 $redbean->store( $bean );
363 } catch (\Exception $e ) {
366 asrt( in_array( "hack", $adapter->getCol( "show tables" ) ), TRUE );
369 $redbean->trash( $bean );
370 } catch (\Exception $e ) {
373 asrt( in_array( "hack", $adapter->getCol( "show tables" ) ), TRUE );
376 $redbean->find( "::", array(), "" );
377 } catch (\Exception $e ) {
381 $adapter->exec( "drop table if exists sometable" );
383 testpack( "Test RedBean Security - query writer" );
386 $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; --" );
387 } catch (\Exception $e ) {
390 asrt( in_array( "hack", $adapter->getCol( "show tables" ) ), TRUE );
392 testpack( "Test ANSI92 issue in clearrelations" );
394 $pdo->Execute( "DROP TABLE IF EXISTS book_group" );
395 $pdo->Execute( "DROP TABLE IF EXISTS author_book" );
396 $pdo->Execute( "DROP TABLE IF EXISTS book" );
397 $pdo->Execute( "DROP TABLE IF EXISTS author" );
399 $redbean = $toolbox->getRedBean();
401 $a = new AssociationManager( $toolbox );
403 $book = $redbean->dispense( "book" );
404 $author1 = $redbean->dispense( "author" );
405 $author2 = $redbean->dispense( "author" );
407 $book->title = "My First Post";
409 $author1->name = "Derek";
410 $author2->name = "Whoever";
412 set1toNAssoc( $a, $book, $author1 );
413 set1toNAssoc( $a, $book, $author2 );
417 $pdo->Execute( "DROP TABLE IF EXISTS book_group" );
418 $pdo->Execute( "DROP TABLE IF EXISTS book_author" );
419 $pdo->Execute( "DROP TABLE IF EXISTS author_book" );
420 $pdo->Execute( "DROP TABLE IF EXISTS book" );
421 $pdo->Execute( "DROP TABLE IF EXISTS author" );
423 $redbean = $toolbox->getRedBean();
425 $a = new AssociationManager( $toolbox );
427 $book = $redbean->dispense( "book" );
428 $author1 = $redbean->dispense( "author" );
429 $author2 = $redbean->dispense( "author" );
431 $book->title = "My First Post";
433 $author1->name = "Derek";
434 $author2->name = "Whoever";
436 $a->associate( $book, $author1 );
437 $a->associate( $book, $author2 );
441 testpack( "Test Association Issue Group keyword (Issues 9 and 10)" );
443 $pdo->Execute( "DROP TABLE IF EXISTS `book_group`" );
444 $pdo->Execute( "DROP TABLE IF EXISTS `group`" );
446 $group = $redbean->dispense( "group" );
448 $group->name = "mygroup";
450 $redbean->store( $group );
453 $a->associate( $group, $book );
460 // Test issue SQL error 23000
462 $a->associate( $group, $book );
469 asrt( (int) $adapter->getCell( "select count(*) from book_group" ), 1 ); //just 1 rec!
471 $pdo->Execute( "DROP TABLE IF EXISTS book_group" );
472 $pdo->Execute( "DROP TABLE IF EXISTS author_book" );
473 $pdo->Execute( "DROP TABLE IF EXISTS book" );
474 $pdo->Execute( "DROP TABLE IF EXISTS author" );
476 $redbean = $toolbox->getRedBean();
478 $a = new AssociationManager( $toolbox );
480 $book = $redbean->dispense( "book" );
481 $author1 = $redbean->dispense( "author" );
482 $author2 = $redbean->dispense( "author" );
484 $book->title = "My First Post";
486 $author1->name = "Derek";
487 $author2->name = "Whoever";
489 $a->unassociate( $book, $author1 );
490 $a->unassociate( $book, $author2 );
494 $redbean->trash( $redbean->dispense( "bla" ) );
498 $bean = $redbean->dispense( "bla" );
503 $redbean->trash( $bean );
509 * Test special data types.
513 public function testTypes()
515 testpack( 'Special data types' );
517 $bean = R::dispense( 'bean' );
519 $bean->date = 'someday';
523 $cols = R::getColumns( 'bean' );
525 asrt( $cols['date'], 'varchar(191)' );
527 $bean = R::dispense( 'bean' );
529 $bean->date = '2011-10-10';
533 $cols = R::getColumns( 'bean' );
535 asrt( $cols['date'], 'varchar(191)' );
543 public function testTypesDates()
545 $bean = R::dispense( 'bean' );
547 $bean->date = '2011-10-10';
551 $cols = R::getColumns( 'bean' );
553 asrt( $cols['date'], 'date' );
561 public function testTypesMon()
563 $bean = R::dispense( 'bean' );
565 $bean->amount = '22.99';
569 $cols = R::getColumns( 'bean' );
571 asrt( $cols['amount'], 'decimal(10,2)' );
575 $bean = R::dispense( 'bean' );
577 $bean->amount = '-22.99';
581 $cols = R::getColumns( 'bean' );
583 asrt( $cols['amount'], 'decimal(10,2)' );
592 public function testTypesDateTimes()
594 $bean = R::dispense( 'bean' );
596 $bean->date = '2011-10-10 10:00:00';
600 $cols = R::getColumns( 'bean' );
602 asrt( $cols['date'], 'datetime' );
604 $bean = R::dispense( 'bean' );
607 $bean = R::dispense( 'bean' );
611 $bean->setMeta( 'cast.title', 'invalid' );
616 } catch ( RedException $e ) {
618 } catch (\Exception $e ) {
622 $bean = R::dispense( 'bean' );
626 $bean->setMeta( 'cast.title', 'text' );
630 $cols = R::getColumns( 'bean' );
632 asrt( $cols['title'], 'text' );
636 $bean = R::dispense( 'bean' );
640 $bean->setMeta( 'cast.title', 'string' );
644 $cols = R::getColumns( 'bean' );
646 asrt( $cols['title'], 'varchar(191)' );
650 * Stored and reloads spatial data to see if the
651 * value is preserved correctly.
655 protected function setGetSpatial( $data )
659 $place = R::dispense( 'place' );
661 $place->location = $data;
665 asrt( R::getCell( 'SELECT AsText(location) FROM place LIMIT 1' ), $data );