CakeSchemaTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. <?php
  2. /**
  3. * Test for Schema database management
  4. *
  5. *
  6. * PHP 5
  7. *
  8. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  9. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. *
  11. * Licensed under The MIT License
  12. * Redistributions of files must retain the above copyright notice
  13. *
  14. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  16. * @package cake.tests.cases.libs
  17. * @since CakePHP(tm) v 1.2.0.5550
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::uses('CakeSchema', 'Model');
  21. App::uses('CakeTestFixture', 'TestSuite/Fixture');
  22. /**
  23. * Test for Schema database management
  24. *
  25. * @package cake.tests.cases.libs
  26. */
  27. class MyAppSchema extends CakeSchema {
  28. /**
  29. * name property
  30. *
  31. * @var string 'MyApp'
  32. * @access public
  33. */
  34. public $name = 'MyApp';
  35. /**
  36. * connection property
  37. *
  38. * @var string 'test'
  39. * @access public
  40. */
  41. public $connection = 'test';
  42. /**
  43. * comments property
  44. *
  45. * @var array
  46. * @access public
  47. */
  48. public $comments = array(
  49. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
  50. 'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0),
  51. 'user_id' => array('type' => 'integer', 'null' => false),
  52. 'title' => array('type' => 'string', 'null' => false, 'length' => 100),
  53. 'comment' => array('type' => 'text', 'null' => false, 'default' => null),
  54. 'published' => array('type' => 'string', 'null' => true, 'default' => 'N', 'length' => 1),
  55. 'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
  56. 'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
  57. 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
  58. );
  59. /**
  60. * posts property
  61. *
  62. * @var array
  63. * @access public
  64. */
  65. public $posts = array(
  66. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
  67. 'author_id' => array('type' => 'integer', 'null' => true, 'default' => ''),
  68. 'title' => array('type' => 'string', 'null' => false, 'default' => 'Title'),
  69. 'body' => array('type' => 'text', 'null' => true, 'default' => null),
  70. 'summary' => array('type' => 'text', 'null' => true),
  71. 'published' => array('type' => 'string', 'null' => true, 'default' => 'Y', 'length' => 1),
  72. 'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
  73. 'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
  74. 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
  75. );
  76. /**
  77. * _foo property
  78. *
  79. * @var array
  80. * @access protected
  81. */
  82. protected $_foo = array('bar');
  83. /**
  84. * setup method
  85. *
  86. * @param mixed $version
  87. * @access public
  88. * @return void
  89. */
  90. public function setup($version) {
  91. }
  92. /**
  93. * teardown method
  94. *
  95. * @param mixed $version
  96. * @access public
  97. * @return void
  98. */
  99. public function teardown($version) {
  100. }
  101. /**
  102. * getVar method
  103. *
  104. * @param string $var Name of var
  105. * @return mixed
  106. */
  107. public function getVar($var) {
  108. if (!isset($this->$var)) {
  109. return null;
  110. }
  111. return $this->$var;
  112. }
  113. }
  114. /**
  115. * TestAppSchema class
  116. *
  117. * @package cake.tests.cases.libs.model
  118. */
  119. class TestAppSchema extends CakeSchema {
  120. /**
  121. * name property
  122. *
  123. * @var string 'MyApp'
  124. * @access public
  125. */
  126. public $name = 'MyApp';
  127. /**
  128. * comments property
  129. *
  130. * @var array
  131. * @access public
  132. */
  133. public $comments = array(
  134. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0,'key' => 'primary'),
  135. 'article_id' => array('type' => 'integer', 'null' => false),
  136. 'user_id' => array('type' => 'integer', 'null' => false),
  137. 'comment' => array('type' => 'text', 'null' => true, 'default' => null),
  138. 'published' => array('type' => 'string', 'null' => true, 'default' => 'N', 'length' => 1),
  139. 'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
  140. 'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
  141. 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
  142. 'tableParameters' => array(),
  143. );
  144. /**
  145. * posts property
  146. *
  147. * @var array
  148. * @access public
  149. */
  150. public $posts = array(
  151. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
  152. 'author_id' => array('type' => 'integer', 'null' => false),
  153. 'title' => array('type' => 'string', 'null' => false),
  154. 'body' => array('type' => 'text', 'null' => true, 'default' => null),
  155. 'published' => array('type' => 'string', 'null' => true, 'default' => 'N', 'length' => 1),
  156. 'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
  157. 'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
  158. 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
  159. 'tableParameters' => array(),
  160. );
  161. /**
  162. * posts_tags property
  163. *
  164. * @var array
  165. * @access public
  166. */
  167. public $posts_tags = array(
  168. 'post_id' => array('type' => 'integer', 'null' => false, 'key' => 'primary'),
  169. 'tag_id' => array('type' => 'string', 'null' => false, 'key' => 'primary'),
  170. 'indexes' => array('posts_tag' => array('column' => array('tag_id', 'post_id'), 'unique' => 1)),
  171. 'tableParameters' => array()
  172. );
  173. /**
  174. * tags property
  175. *
  176. * @var array
  177. * @access public
  178. */
  179. public $tags = array(
  180. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
  181. 'tag' => array('type' => 'string', 'null' => false),
  182. 'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
  183. 'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
  184. 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
  185. 'tableParameters' => array()
  186. );
  187. /**
  188. * datatypes property
  189. *
  190. * @var array
  191. * @access public
  192. */
  193. public $datatypes = array(
  194. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
  195. 'float_field' => array('type' => 'float', 'null' => false, 'length' => '5,2', 'default' => ''),
  196. 'bool' => array('type' => 'boolean', 'null' => false, 'default' => false),
  197. 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
  198. 'tableParameters' => array()
  199. );
  200. /**
  201. * setup method
  202. *
  203. * @param mixed $version
  204. * @access public
  205. * @return void
  206. */
  207. public function setup($version) {
  208. }
  209. /**
  210. * teardown method
  211. *
  212. * @param mixed $version
  213. * @access public
  214. * @return void
  215. */
  216. public function teardown($version) {
  217. }
  218. }
  219. /**
  220. * SchmeaPost class
  221. *
  222. * @package cake.tests.cases.libs.model
  223. */
  224. class SchemaPost extends CakeTestModel {
  225. /**
  226. * name property
  227. *
  228. * @var string 'SchemaPost'
  229. * @access public
  230. */
  231. public $name = 'SchemaPost';
  232. /**
  233. * useTable property
  234. *
  235. * @var string 'posts'
  236. * @access public
  237. */
  238. public $useTable = 'posts';
  239. /**
  240. * hasMany property
  241. *
  242. * @var array
  243. * @access public
  244. */
  245. public $hasMany = array('SchemaComment');
  246. /**
  247. * hasAndBelongsToMany property
  248. *
  249. * @var array
  250. * @access public
  251. */
  252. public $hasAndBelongsToMany = array('SchemaTag');
  253. }
  254. /**
  255. * SchemaComment class
  256. *
  257. * @package cake.tests.cases.libs.model
  258. */
  259. class SchemaComment extends CakeTestModel {
  260. /**
  261. * name property
  262. *
  263. * @var string 'SchemaComment'
  264. * @access public
  265. */
  266. public $name = 'SchemaComment';
  267. /**
  268. * useTable property
  269. *
  270. * @var string 'comments'
  271. * @access public
  272. */
  273. public $useTable = 'comments';
  274. /**
  275. * belongsTo property
  276. *
  277. * @var array
  278. * @access public
  279. */
  280. public $belongsTo = array('SchemaPost');
  281. }
  282. /**
  283. * SchemaTag class
  284. *
  285. * @package cake.tests.cases.libs.model
  286. */
  287. class SchemaTag extends CakeTestModel {
  288. /**
  289. * name property
  290. *
  291. * @var string 'SchemaTag'
  292. * @access public
  293. */
  294. public $name = 'SchemaTag';
  295. /**
  296. * useTable property
  297. *
  298. * @var string 'tags'
  299. * @access public
  300. */
  301. public $useTable = 'tags';
  302. /**
  303. * hasAndBelongsToMany property
  304. *
  305. * @var array
  306. * @access public
  307. */
  308. public $hasAndBelongsToMany = array('SchemaPost');
  309. }
  310. /**
  311. * SchemaDatatype class
  312. *
  313. * @package cake.tests.cases.libs.model
  314. */
  315. class SchemaDatatype extends CakeTestModel {
  316. /**
  317. * name property
  318. *
  319. * @var string 'SchemaDatatype'
  320. * @access public
  321. */
  322. public $name = 'SchemaDatatype';
  323. /**
  324. * useTable property
  325. *
  326. * @var string 'datatypes'
  327. * @access public
  328. */
  329. public $useTable = 'datatypes';
  330. }
  331. /**
  332. * Testdescribe class
  333. *
  334. * This class is defined purely to inherit the cacheSources variable otherwise
  335. * testSchemaCreatTable will fail if listSources has already been called and
  336. * its source cache populated - I.e. if the test is run within a group
  337. *
  338. * @uses CakeTestModel
  339. * @package
  340. * @package cake.tests.cases.libs.model
  341. */
  342. class Testdescribe extends CakeTestModel {
  343. /**
  344. * name property
  345. *
  346. * @var string 'Testdescribe'
  347. * @access public
  348. */
  349. public $name = 'Testdescribe';
  350. }
  351. /**
  352. * SchemaCrossDatabase class
  353. *
  354. * @package cake.tests.cases.libs.model
  355. */
  356. class SchemaCrossDatabase extends CakeTestModel {
  357. /**
  358. * name property
  359. *
  360. * @var string 'SchemaCrossDatabase'
  361. * @access public
  362. */
  363. public $name = 'SchemaCrossDatabase';
  364. /**
  365. * useTable property
  366. *
  367. * @var string 'posts'
  368. * @access public
  369. */
  370. public $useTable = 'cross_database';
  371. /**
  372. * useDbConfig property
  373. *
  374. * @var string 'test2'
  375. * @access public
  376. */
  377. public $useDbConfig = 'test2';
  378. }
  379. /**
  380. * SchemaCrossDatabaseFixture class
  381. *
  382. * @package cake.tests.cases.libs.model
  383. */
  384. class SchemaCrossDatabaseFixture extends CakeTestFixture {
  385. /**
  386. * name property
  387. *
  388. * @var string 'CrossDatabase'
  389. * @access public
  390. */
  391. public $name = 'CrossDatabase';
  392. /**
  393. * table property
  394. *
  395. * @access public
  396. */
  397. public $table = 'cross_database';
  398. /**
  399. * fields property
  400. *
  401. * @var array
  402. * @access public
  403. */
  404. public $fields = array(
  405. 'id' => array('type' => 'integer', 'key' => 'primary'),
  406. 'name' => 'string'
  407. );
  408. /**
  409. * records property
  410. *
  411. * @var array
  412. * @access public
  413. */
  414. public $records = array(
  415. array('id' => 1, 'name' => 'First'),
  416. array('id' => 2, 'name' => 'Second'),
  417. );
  418. }
  419. /**
  420. * SchemaPrefixAuthUser class
  421. *
  422. * @package cake.tests.cases.libs.model
  423. */
  424. class SchemaPrefixAuthUser extends CakeTestModel {
  425. /**
  426. * name property
  427. *
  428. * @var string
  429. */
  430. public $name = 'SchemaPrefixAuthUser';
  431. /**
  432. * table prefix
  433. *
  434. * @var string
  435. */
  436. public $tablePrefix = 'auth_';
  437. /**
  438. * useTable
  439. *
  440. * @var string
  441. */
  442. public $useTable = 'users';
  443. }
  444. /**
  445. * CakeSchemaTest
  446. *
  447. * @package cake.tests.cases.libs
  448. */
  449. class CakeSchemaTest extends CakeTestCase {
  450. /**
  451. * fixtures property
  452. *
  453. * @var array
  454. * @access public
  455. */
  456. public $fixtures = array(
  457. 'core.post', 'core.tag', 'core.posts_tag', 'core.test_plugin_comment',
  458. 'core.datatype', 'core.auth_user', 'core.author',
  459. 'core.test_plugin_article', 'core.user', 'core.comment',
  460. 'core.prefix_test'
  461. );
  462. /**
  463. * setUp method
  464. *
  465. * @return void
  466. */
  467. public function setUp() {
  468. parent::setUp();
  469. ConnectionManager::getDataSource('test')->cacheSources = false;
  470. $this->Schema = new TestAppSchema();
  471. }
  472. /**
  473. * tearDown method
  474. *
  475. * @return void
  476. */
  477. public function tearDown() {
  478. parent::tearDown();
  479. if (file_exists(TMP . 'tests' . DS .'schema.php')) {
  480. unlink(TMP . 'tests' . DS .'schema.php');
  481. }
  482. unset($this->Schema);
  483. CakePlugin::unload();
  484. }
  485. /**
  486. * testSchemaName method
  487. *
  488. * @access public
  489. * @return void
  490. */
  491. public function testSchemaName() {
  492. $Schema = new CakeSchema();
  493. $this->assertEqual(strtolower($Schema->name), strtolower(APP_DIR));
  494. Configure::write('App.dir', 'Some.name.with.dots');
  495. $Schema = new CakeSchema();
  496. $this->assertEqual($Schema->name, 'SomeNameWithDots');
  497. Configure::write('App.dir', 'app');
  498. }
  499. /**
  500. * testSchemaRead method
  501. *
  502. * @access public
  503. * @return void
  504. */
  505. public function testSchemaRead() {
  506. $read = $this->Schema->read(array(
  507. 'connection' => 'test',
  508. 'name' => 'TestApp',
  509. 'models' => array('SchemaPost', 'SchemaComment', 'SchemaTag', 'SchemaDatatype')
  510. ));
  511. unset($read['tables']['missing']);
  512. $expected = array('comments', 'datatypes', 'posts', 'posts_tags', 'tags');
  513. foreach ($expected as $table) {
  514. $this->assertTrue(isset($read['tables'][$table]), 'Missing table ' . $table);
  515. }
  516. foreach ($this->Schema->tables as $table => $fields) {
  517. $this->assertEqual(array_keys($fields), array_keys($read['tables'][$table]));
  518. }
  519. $this->assertEqual(
  520. $read['tables']['datatypes']['float_field']['length'],
  521. $this->Schema->tables['datatypes']['float_field']['length']
  522. );
  523. $this->assertEqual(
  524. $read['tables']['datatypes']['float_field']['type'],
  525. $this->Schema->tables['datatypes']['float_field']['type']
  526. );
  527. $this->assertEqual(
  528. $read['tables']['datatypes']['float_field']['null'],
  529. $this->Schema->tables['datatypes']['float_field']['null']
  530. );
  531. $db = ConnectionManager::getDataSource('test');
  532. $config = $db->config;
  533. $config['prefix'] = 'schema_test_prefix_';
  534. ConnectionManager::create('schema_prefix', $config);
  535. $read = $this->Schema->read(array('connection' => 'schema_prefix', 'models' => false));
  536. $this->assertTrue(empty($read['tables']));
  537. $read = $this->Schema->read(array(
  538. 'connection' => 'test',
  539. 'name' => 'TestApp',
  540. 'models' => array('SchemaComment', 'SchemaTag', 'SchemaPost')
  541. ));
  542. $this->assertFalse(isset($read['tables']['missing']['posts_tags']), 'Join table marked as missing');
  543. }
  544. /**
  545. * testSchemaReadWithOddTablePrefix method
  546. *
  547. * @access public
  548. * @return void
  549. */
  550. public function testSchemaReadWithOddTablePrefix() {
  551. $config = ConnectionManager::getDataSource('test')->config;
  552. $this->skipIf(!empty($config['prefix']), 'This test can not be executed with datasource prefix set.');
  553. $SchemaPost = ClassRegistry::init('SchemaPost');
  554. $SchemaPost->tablePrefix = 'po';
  555. $SchemaPost->useTable = 'sts';
  556. $read = $this->Schema->read(array(
  557. 'connection' => 'test',
  558. 'name' => 'TestApp',
  559. 'models' => array('SchemaPost')
  560. ));
  561. $this->assertFalse(isset($read['tables']['missing']['posts']), 'Posts table was not read from tablePrefix');
  562. }
  563. /**
  564. * test read() with tablePrefix properties.
  565. *
  566. * @return void
  567. */
  568. public function testSchemaReadWithTablePrefix() {
  569. $config = ConnectionManager::getDataSource('test')->config;
  570. $this->skipIf(!empty($config['prefix']), 'This test can not be executed with datasource prefix set.');
  571. $model = new SchemaPrefixAuthUser();
  572. $Schema = new CakeSchema();
  573. $read = $Schema->read(array(
  574. 'connection' => 'test',
  575. 'name' => 'TestApp',
  576. 'models' => array('SchemaPrefixAuthUser')
  577. ));
  578. unset($read['tables']['missing']);
  579. $this->assertTrue(isset($read['tables']['auth_users']), 'auth_users key missing %s');
  580. }
  581. /**
  582. * test reading schema with config prefix.
  583. *
  584. * @return void
  585. */
  586. public function testSchemaReadWithConfigPrefix() {
  587. $db = ConnectionManager::getDataSource('test');
  588. $config = $db->config;
  589. $config['prefix'] = 'schema_test_prefix_';
  590. ConnectionManager::create('schema_prefix', $config);
  591. $read = $this->Schema->read(array('connection' => 'schema_prefix', 'models' => false));
  592. $this->assertTrue(empty($read['tables']));
  593. $config['prefix'] = 'prefix_';
  594. ConnectionManager::create('schema_prefix2', $config);
  595. $read = $this->Schema->read(array(
  596. 'connection' => 'schema_prefix2',
  597. 'name' => 'TestApp',
  598. 'models' => false));
  599. $this->assertTrue(isset($read['tables']['prefix_tests']));
  600. }
  601. /**
  602. * test reading schema from plugins.
  603. *
  604. * @return void
  605. */
  606. public function testSchemaReadWithPlugins() {
  607. App::objects('model', null, false);
  608. App::build(array(
  609. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  610. ));
  611. CakePlugin::load('TestPlugin');
  612. $Schema = new CakeSchema();
  613. $Schema->plugin = 'TestPlugin';
  614. $read = $Schema->read(array(
  615. 'connection' => 'test',
  616. 'name' => 'TestApp',
  617. 'models' => true
  618. ));
  619. unset($read['tables']['missing']);
  620. $this->assertTrue(isset($read['tables']['auth_users']));
  621. $this->assertTrue(isset($read['tables']['authors']));
  622. $this->assertTrue(isset($read['tables']['test_plugin_comments']));
  623. $this->assertTrue(isset($read['tables']['posts']));
  624. $this->assertTrue(count($read['tables']) >= 4);
  625. App::build();
  626. }
  627. /**
  628. * test reading schema with tables from another database.
  629. *
  630. * @return void
  631. */
  632. public function testSchemaReadWithCrossDatabase() {
  633. $config = new DATABASE_CONFIG();
  634. $this->skipIf(
  635. !isset($config->test) || !isset($config->test2),
  636. 'Primary and secondary test databases not configured, skipping cross-database join tests.'
  637. . ' To run these tests, you must define $test and $test2 in your database configuration.'
  638. );
  639. $db2 = ConnectionManager::getDataSource('test2');
  640. $fixture = new SchemaCrossDatabaseFixture();
  641. $fixture->create($db2);
  642. $fixture->insert($db2);
  643. $read = $this->Schema->read(array(
  644. 'connection' => 'test',
  645. 'name' => 'TestApp',
  646. 'models' => array('SchemaCrossDatabase', 'SchemaPost')
  647. ));
  648. $this->assertTrue(isset($read['tables']['posts']));
  649. $this->assertFalse(isset($read['tables']['cross_database']), 'Cross database should not appear');
  650. $this->assertFalse(isset($read['tables']['missing']['cross_database']), 'Cross database should not appear');
  651. $read = $this->Schema->read(array(
  652. 'connection' => 'test2',
  653. 'name' => 'TestApp',
  654. 'models' => array('SchemaCrossDatabase', 'SchemaPost')
  655. ));
  656. $this->assertFalse(isset($read['tables']['posts']), 'Posts should not appear');
  657. $this->assertFalse(isset($read['tables']['posts']), 'Posts should not appear');
  658. $this->assertTrue(isset($read['tables']['cross_database']));
  659. $fixture->drop($db2);
  660. }
  661. /**
  662. * test that tables are generated correctly
  663. *
  664. * @return void
  665. */
  666. public function testGenerateTable() {
  667. $posts = array(
  668. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
  669. 'author_id' => array('type' => 'integer', 'null' => false),
  670. 'title' => array('type' => 'string', 'null' => false),
  671. 'body' => array('type' => 'text', 'null' => true, 'default' => null),
  672. 'published' => array('type' => 'string', 'null' => true, 'default' => 'N', 'length' => 1),
  673. 'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
  674. 'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
  675. 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
  676. );
  677. $result = $this->Schema->generateTable('posts', $posts);
  678. $this->assertPattern('/var \$posts/', $result);
  679. }
  680. /**
  681. * testSchemaWrite method
  682. *
  683. * @access public
  684. * @return void
  685. */
  686. public function testSchemaWrite() {
  687. $write = $this->Schema->write(array('name' => 'MyOtherApp', 'tables' => $this->Schema->tables, 'path' => TMP . 'tests'));
  688. $file = file_get_contents(TMP . 'tests' . DS .'schema.php');
  689. $this->assertEqual($write, $file);
  690. require_once( TMP . 'tests' . DS .'schema.php');
  691. $OtherSchema = new MyOtherAppSchema();
  692. $this->assertEqual($this->Schema->tables, $OtherSchema->tables);
  693. }
  694. /**
  695. * testSchemaComparison method
  696. *
  697. * @access public
  698. * @return void
  699. */
  700. public function testSchemaComparison() {
  701. $New = new MyAppSchema();
  702. $compare = $New->compare($this->Schema);
  703. $expected = array(
  704. 'comments' => array(
  705. 'add' => array(
  706. 'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0),
  707. 'title' => array('type' => 'string', 'null' => false, 'length' => 100),
  708. ),
  709. 'drop' => array(
  710. 'article_id' => array('type' => 'integer', 'null' => false),
  711. 'tableParameters' => array(),
  712. ),
  713. 'change' => array(
  714. 'comment' => array('type' => 'text', 'null' => false, 'default' => null),
  715. )
  716. ),
  717. 'posts' => array(
  718. 'add' => array(
  719. 'summary' => array('type' => 'text', 'null' => true),
  720. ),
  721. 'drop' => array(
  722. 'tableParameters' => array(),
  723. ),
  724. 'change' => array(
  725. 'author_id' => array('type' => 'integer', 'null' => true, 'default' => ''),
  726. 'title' => array('type' => 'string', 'null' => false, 'default' => 'Title'),
  727. 'published' => array('type' => 'string', 'null' => true, 'default' => 'Y', 'length' => 1)
  728. )
  729. ),
  730. );
  731. $this->assertEqual($expected, $compare);
  732. $this->assertNull($New->getVar('comments'));
  733. $this->assertEqual($New->getVar('_foo'), array('bar'));
  734. $tables = array(
  735. 'missing' => array(
  736. 'categories' => array(
  737. 'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
  738. 'created' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
  739. 'modified' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
  740. 'name' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 100),
  741. 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)),
  742. 'tableParameters' => array('charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'MyISAM')
  743. )
  744. ),
  745. 'ratings' => array(
  746. 'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
  747. 'foreign_key' => array('type' => 'integer', 'null' => false, 'default' => NULL),
  748. 'model' => array('type' => 'varchar', 'null' => false, 'default' => NULL),
  749. 'value' => array('type' => 'float', 'null' => false, 'length' => '5,2', 'default' => NULL),
  750. 'created' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
  751. 'modified' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
  752. 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)),
  753. 'tableParameters' => array('charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'MyISAM')
  754. )
  755. );
  756. $compare = $New->compare($this->Schema, $tables);
  757. $expected = array(
  758. 'ratings' => array(
  759. 'add' => array(
  760. 'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
  761. 'foreign_key' => array('type' => 'integer', 'null' => false, 'default' => NULL),
  762. 'model' => array('type' => 'varchar', 'null' => false, 'default' => NULL),
  763. 'value' => array('type' => 'float', 'null' => false, 'length' => '5,2', 'default' => NULL),
  764. 'created' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
  765. 'modified' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
  766. 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)),
  767. 'tableParameters' => array('charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'MyISAM')
  768. )
  769. )
  770. );
  771. $this->assertEqual($expected, $compare);
  772. }
  773. /**
  774. * test comparing '' and null and making sure they are different.
  775. *
  776. * @return void
  777. */
  778. public function testCompareEmptyStringAndNull() {
  779. $One = new CakeSchema(array(
  780. 'posts' => array(
  781. 'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
  782. 'name' => array('type' => 'string', 'null' => false, 'default' => '')
  783. )
  784. ));
  785. $Two = new CakeSchema(array(
  786. 'posts' => array(
  787. 'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
  788. 'name' => array('type' => 'string', 'null' => false, 'default' => null)
  789. )
  790. ));
  791. $compare = $One->compare($Two);
  792. $expected = array(
  793. 'posts' => array(
  794. 'change' => array(
  795. 'name' => array('type' => 'string', 'null' => false, 'default' => null)
  796. )
  797. )
  798. );
  799. $this->assertEqual($expected, $compare);
  800. }
  801. /**
  802. * Test comparing tableParameters and indexes.
  803. *
  804. * @return void
  805. */
  806. public function testTableParametersAndIndexComparison() {
  807. $old = array(
  808. 'posts' => array(
  809. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
  810. 'author_id' => array('type' => 'integer', 'null' => false),
  811. 'title' => array('type' => 'string', 'null' => false),
  812. 'indexes' => array(
  813. 'PRIMARY' => array('column' => 'id', 'unique' => true)
  814. ),
  815. 'tableParameters' => array(
  816. 'charset' => 'latin1',
  817. 'collate' => 'latin1_general_ci'
  818. )
  819. ),
  820. 'comments' => array(
  821. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
  822. 'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0),
  823. 'comment' => array('type' => 'text'),
  824. 'indexes' => array(
  825. 'PRIMARY' => array('column' => 'id', 'unique' => true),
  826. 'post_id' => array('column' => 'post_id'),
  827. ),
  828. 'tableParameters' => array(
  829. 'engine' => 'InnoDB',
  830. 'charset' => 'latin1',
  831. 'collate' => 'latin1_general_ci'
  832. )
  833. )
  834. );
  835. $new = array(
  836. 'posts' => array(
  837. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
  838. 'author_id' => array('type' => 'integer', 'null' => false),
  839. 'title' => array('type' => 'string', 'null' => false),
  840. 'indexes' => array(
  841. 'PRIMARY' => array('column' => 'id', 'unique' => true),
  842. 'author_id' => array('column' => 'author_id'),
  843. ),
  844. 'tableParameters' => array(
  845. 'charset' => 'utf8',
  846. 'collate' => 'utf8_general_ci',
  847. 'engine' => 'MyISAM'
  848. )
  849. ),
  850. 'comments' => array(
  851. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
  852. 'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0),
  853. 'comment' => array('type' => 'text'),
  854. 'indexes' => array(
  855. 'PRIMARY' => array('column' => 'id', 'unique' => true),
  856. ),
  857. 'tableParameters' => array(
  858. 'charset' => 'utf8',
  859. 'collate' => 'utf8_general_ci'
  860. )
  861. )
  862. );
  863. $compare = $this->Schema->compare($old, $new);
  864. $expected = array(
  865. 'posts' => array(
  866. 'add' => array(
  867. 'indexes' => array('author_id' => array('column' => 'author_id')),
  868. ),
  869. 'change' => array(
  870. 'tableParameters' => array(
  871. 'charset' => 'utf8',
  872. 'collate' => 'utf8_general_ci',
  873. 'engine' => 'MyISAM'
  874. )
  875. )
  876. ),
  877. 'comments' => array(
  878. 'drop' => array(
  879. 'indexes' => array('post_id' => array('column' => 'post_id')),
  880. ),
  881. 'change' => array(
  882. 'tableParameters' => array(
  883. 'charset' => 'utf8',
  884. 'collate' => 'utf8_general_ci',
  885. )
  886. )
  887. )
  888. );
  889. $this->assertEqual($compare, $expected);
  890. }
  891. /**
  892. * testSchemaLoading method
  893. *
  894. * @access public
  895. * @return void
  896. */
  897. public function testSchemaLoading() {
  898. $Other = $this->Schema->load(array('name' => 'MyOtherApp', 'path' => TMP . 'tests'));
  899. $this->assertEqual($Other->name, 'MyOtherApp');
  900. $this->assertEqual($Other->tables, $this->Schema->tables);
  901. }
  902. /**
  903. * test loading schema files inside of plugins.
  904. *
  905. * @return void
  906. */
  907. public function testSchemaLoadingFromPlugin() {
  908. App::build(array(
  909. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  910. ));
  911. CakePlugin::load('TestPlugin');
  912. $Other = $this->Schema->load(array('name' => 'TestPluginApp', 'plugin' => 'TestPlugin'));
  913. $this->assertEqual($Other->name, 'TestPluginApp');
  914. $this->assertEqual(array_keys($Other->tables), array('test_plugin_acos'));
  915. App::build();
  916. }
  917. /**
  918. * testSchemaCreateTable method
  919. *
  920. * @access public
  921. * @return void
  922. */
  923. public function testSchemaCreateTable() {
  924. $db = ConnectionManager::getDataSource('test');
  925. $db->cacheSources = false;
  926. $Schema = new CakeSchema(array(
  927. 'connection' => 'test',
  928. 'testdescribes' => array(
  929. 'id' => array('type' => 'integer', 'key' => 'primary'),
  930. 'int_null' => array('type' => 'integer', 'null' => true),
  931. 'int_not_null' => array('type' => 'integer', 'null' => false),
  932. ),
  933. ));
  934. $sql = $db->createSchema($Schema);
  935. $col = $Schema->tables['testdescribes']['int_null'];
  936. $col['name'] = 'int_null';
  937. $column = $this->db->buildColumn($col);
  938. $this->assertPattern('/' . preg_quote($column, '/') . '/', $sql);
  939. $col = $Schema->tables['testdescribes']['int_not_null'];
  940. $col['name'] = 'int_not_null';
  941. $column = $this->db->buildColumn($col);
  942. $this->assertPattern('/' . preg_quote($column, '/') . '/', $sql);
  943. }
  944. }