Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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