Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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