comparison cake/tests/cases/libs/model/model_integration.test.php @ 0:261e66bd5a0c

hg init
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Sun, 24 Jul 2011 21:08:31 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:261e66bd5a0c
1 <?php
2 /* SVN FILE: $Id: model.test.php 8225 2009-07-08 03:25:30Z mark_story $ */
3
4 /**
5 * ModelIntegrationTest file
6 *
7 * PHP versions 4 and 5
8 *
9 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
10 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
11 *
12 * Licensed under The Open Group Test Suite License
13 * Redistributions of files must retain the above copyright notice.
14 *
15 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
16 * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
17 * @package cake
18 * @subpackage cake.tests.cases.libs.model
19 * @since CakePHP(tm) v 1.2.0.4206
20 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
21 */
22 require_once dirname(__FILE__) . DS . 'model.test.php';
23 App::import('Core', 'DboSource');
24
25 /**
26 * DboMock class
27 * A Dbo Source driver to mock a connection and a identity name() method
28 */
29 class DboMock extends DboSource {
30
31 /**
32 * Returns the $field without modifications
33 */
34 function name($field) {
35 return $field;
36 }
37 /**
38 * Returns true to fake a database connection
39 */
40 function connect() {
41 return true;
42 }
43 }
44
45 /**
46 * ModelIntegrationTest
47 *
48 * @package cake
49 * @subpackage cake.tests.cases.libs.model.operations
50 */
51 class ModelIntegrationTest extends BaseModelTest {
52
53 /**
54 * testPkInHAbtmLinkModelArticleB
55 *
56 * @access public
57 * @return void
58 */
59 function testPkInHabtmLinkModelArticleB() {
60 $this->loadFixtures('Article', 'Tag');
61 $TestModel2 =& new ArticleB();
62 $this->assertEqual($TestModel2->ArticlesTag->primaryKey, 'article_id');
63 }
64
65 /**
66 * Tests that $cacheSources can only be disabled in the db using model settings, not enabled
67 *
68 * @access public
69 * @return void
70 */
71 function testCacheSourcesDisabling() {
72 $this->db->cacheSources = true;
73 $TestModel = new JoinA();
74 $TestModel->cacheSources = false;
75 $TestModel->setSource('join_as');
76 $this->assertFalse($this->db->cacheSources);
77
78 $this->db->cacheSources = false;
79 $TestModel = new JoinA();
80 $TestModel->cacheSources = true;
81 $TestModel->setSource('join_as');
82 $this->assertFalse($this->db->cacheSources);
83 }
84
85 /**
86 * testPkInHabtmLinkModel method
87 *
88 * @access public
89 * @return void
90 */
91 function testPkInHabtmLinkModel() {
92 //Test Nonconformant Models
93 $this->loadFixtures('Content', 'ContentAccount', 'Account');
94 $TestModel =& new Content();
95 $this->assertEqual($TestModel->ContentAccount->primaryKey, 'iContentAccountsId');
96
97 //test conformant models with no PK in the join table
98 $this->loadFixtures('Article', 'Tag');
99 $TestModel2 =& new Article();
100 $this->assertEqual($TestModel2->ArticlesTag->primaryKey, 'article_id');
101
102 //test conformant models with PK in join table
103 $this->loadFixtures('Item', 'Portfolio', 'ItemsPortfolio');
104 $TestModel3 =& new Portfolio();
105 $this->assertEqual($TestModel3->ItemsPortfolio->primaryKey, 'id');
106
107 //test conformant models with PK in join table - join table contains extra field
108 $this->loadFixtures('JoinA', 'JoinB', 'JoinAB');
109 $TestModel4 =& new JoinA();
110 $this->assertEqual($TestModel4->JoinAsJoinB->primaryKey, 'id');
111
112 }
113
114 /**
115 * testDynamicBehaviorAttachment method
116 *
117 * @access public
118 * @return void
119 */
120 function testDynamicBehaviorAttachment() {
121 $this->loadFixtures('Apple');
122 $TestModel =& new Apple();
123 $this->assertEqual($TestModel->Behaviors->attached(), array());
124
125 $TestModel->Behaviors->attach('Tree', array('left' => 'left_field', 'right' => 'right_field'));
126 $this->assertTrue(is_object($TestModel->Behaviors->Tree));
127 $this->assertEqual($TestModel->Behaviors->attached(), array('Tree'));
128
129 $expected = array(
130 'parent' => 'parent_id',
131 'left' => 'left_field',
132 'right' => 'right_field',
133 'scope' => '1 = 1',
134 'type' => 'nested',
135 '__parentChange' => false,
136 'recursive' => -1
137 );
138
139 $this->assertEqual($TestModel->Behaviors->Tree->settings['Apple'], $expected);
140
141 $expected['enabled'] = false;
142 $TestModel->Behaviors->attach('Tree', array('enabled' => false));
143 $this->assertEqual($TestModel->Behaviors->Tree->settings['Apple'], $expected);
144 $this->assertEqual($TestModel->Behaviors->attached(), array('Tree'));
145
146 $TestModel->Behaviors->detach('Tree');
147 $this->assertEqual($TestModel->Behaviors->attached(), array());
148 $this->assertFalse(isset($TestModel->Behaviors->Tree));
149 }
150
151 /**
152 * Tests cross database joins. Requires $test and $test2 to both be set in DATABASE_CONFIG
153 * NOTE: When testing on MySQL, you must set 'persistent' => false on *both* database connections,
154 * or one connection will step on the other.
155 */
156 function testCrossDatabaseJoins() {
157 $config = new DATABASE_CONFIG();
158
159 $skip = $this->skipIf(
160 !isset($config->test) || !isset($config->test2),
161 '%s Primary and secondary test databases not configured, skipping cross-database '
162 .'join tests.'
163 .' To run these tests, you must define $test and $test2 in your database configuration.'
164 );
165
166 if ($skip) {
167 return;
168 }
169
170 $this->loadFixtures('Article', 'Tag', 'ArticlesTag', 'User', 'Comment');
171 $TestModel =& new Article();
172
173 $expected = array(
174 array(
175 'Article' => array(
176 'id' => '1',
177 'user_id' => '1',
178 'title' => 'First Article',
179 'body' => 'First Article Body',
180 'published' => 'Y',
181 'created' => '2007-03-18 10:39:23',
182 'updated' => '2007-03-18 10:41:31'
183 ),
184 'User' => array(
185 'id' => '1',
186 'user' => 'mariano',
187 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
188 'created' => '2007-03-17 01:16:23',
189 'updated' => '2007-03-17 01:18:31'
190 ),
191 'Comment' => array(
192 array(
193 'id' => '1',
194 'article_id' => '1',
195 'user_id' => '2',
196 'comment' => 'First Comment for First Article',
197 'published' => 'Y',
198 'created' => '2007-03-18 10:45:23',
199 'updated' => '2007-03-18 10:47:31'
200 ),
201 array(
202 'id' => '2',
203 'article_id' => '1',
204 'user_id' => '4',
205 'comment' => 'Second Comment for First Article',
206 'published' => 'Y',
207 'created' => '2007-03-18 10:47:23',
208 'updated' => '2007-03-18 10:49:31'
209 ),
210 array(
211 'id' => '3',
212 'article_id' => '1',
213 'user_id' => '1',
214 'comment' => 'Third Comment for First Article',
215 'published' => 'Y',
216 'created' => '2007-03-18 10:49:23',
217 'updated' => '2007-03-18 10:51:31'
218 ),
219 array(
220 'id' => '4',
221 'article_id' => '1',
222 'user_id' => '1',
223 'comment' => 'Fourth Comment for First Article',
224 'published' => 'N',
225 'created' => '2007-03-18 10:51:23',
226 'updated' => '2007-03-18 10:53:31'
227 )),
228 'Tag' => array(
229 array(
230 'id' => '1',
231 'tag' => 'tag1',
232 'created' => '2007-03-18 12:22:23',
233 'updated' => '2007-03-18 12:24:31'
234 ),
235 array(
236 'id' => '2',
237 'tag' => 'tag2',
238 'created' => '2007-03-18 12:24:23',
239 'updated' => '2007-03-18 12:26:31'
240 ))),
241 array(
242 'Article' => array(
243 'id' => '2',
244 'user_id' => '3',
245 'title' => 'Second Article',
246 'body' => 'Second Article Body',
247 'published' => 'Y',
248 'created' => '2007-03-18 10:41:23',
249 'updated' => '2007-03-18 10:43:31'
250 ),
251 'User' => array(
252 'id' => '3',
253 'user' => 'larry',
254 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
255 'created' => '2007-03-17 01:20:23',
256 'updated' => '2007-03-17 01:22:31'
257 ),
258 'Comment' => array(
259 array(
260 'id' => '5',
261 'article_id' => '2',
262 'user_id' => '1',
263 'comment' => 'First Comment for Second Article',
264 'published' => 'Y',
265 'created' => '2007-03-18 10:53:23',
266 'updated' => '2007-03-18 10:55:31'
267 ),
268 array(
269 'id' => '6',
270 'article_id' => '2',
271 'user_id' => '2',
272 'comment' => 'Second Comment for Second Article',
273 'published' => 'Y',
274 'created' => '2007-03-18 10:55:23',
275 'updated' => '2007-03-18 10:57:31'
276 )),
277 'Tag' => array(
278 array(
279 'id' => '1',
280 'tag' => 'tag1',
281 'created' => '2007-03-18 12:22:23',
282 'updated' => '2007-03-18 12:24:31'
283 ),
284 array(
285 'id' => '3',
286 'tag' => 'tag3',
287 'created' => '2007-03-18 12:26:23',
288 'updated' => '2007-03-18 12:28:31'
289 ))),
290 array(
291 'Article' => array(
292 'id' => '3',
293 'user_id' => '1',
294 'title' => 'Third Article',
295 'body' => 'Third Article Body',
296 'published' => 'Y',
297 'created' => '2007-03-18 10:43:23',
298 'updated' => '2007-03-18 10:45:31'
299 ),
300 'User' => array(
301 'id' => '1',
302 'user' => 'mariano',
303 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
304 'created' => '2007-03-17 01:16:23',
305 'updated' => '2007-03-17 01:18:31'
306 ),
307 'Comment' => array(),
308 'Tag' => array()
309 ));
310 $this->assertEqual($TestModel->find('all'), $expected);
311
312 $db2 =& ConnectionManager::getDataSource('test2');
313
314 foreach (array('User', 'Comment') as $class) {
315 $this->_fixtures[$this->_fixtureClassMap[$class]]->create($db2);
316 $this->_fixtures[$this->_fixtureClassMap[$class]]->insert($db2);
317 $this->db->truncate(Inflector::pluralize(Inflector::underscore($class)));
318 }
319
320 $this->assertEqual($TestModel->User->find('all'), array());
321 $this->assertEqual($TestModel->Comment->find('all'), array());
322 $this->assertEqual($TestModel->find('count'), 3);
323
324 $TestModel->User->setDataSource('test2');
325 $TestModel->Comment->setDataSource('test2');
326
327 foreach ($expected as $key => $value) {
328 unset($value['Comment'], $value['Tag']);
329 $expected[$key] = $value;
330 }
331
332 $TestModel->recursive = 0;
333 $result = $TestModel->find('all');
334 $this->assertEqual($result, $expected);
335
336 foreach ($expected as $key => $value) {
337 unset($value['Comment'], $value['Tag']);
338 $expected[$key] = $value;
339 }
340
341 $TestModel->recursive = 0;
342 $result = $TestModel->find('all');
343 $this->assertEqual($result, $expected);
344
345 $result = Set::extract($TestModel->User->find('all'), '{n}.User.id');
346 $this->assertEqual($result, array('1', '2', '3', '4'));
347 $this->assertEqual($TestModel->find('all'), $expected);
348
349 $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment')));
350 $expected = array(
351 array(
352 'Comment' => array(
353 'id' => '1',
354 'article_id' => '1',
355 'user_id' => '2',
356 'comment' => 'First Comment for First Article',
357 'published' => 'Y',
358 'created' => '2007-03-18 10:45:23',
359 'updated' => '2007-03-18 10:47:31'
360 ),
361 'User' => array(
362 'id' => '2',
363 'user' => 'nate',
364 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
365 'created' => '2007-03-17 01:18:23',
366 'updated' => '2007-03-17 01:20:31'
367 ),
368 'Article' => array(
369 'id' => '1',
370 'user_id' => '1',
371 'title' => 'First Article',
372 'body' => 'First Article Body',
373 'published' => 'Y',
374 'created' => '2007-03-18 10:39:23',
375 'updated' => '2007-03-18 10:41:31'
376 )),
377 array(
378 'Comment' => array(
379 'id' => '2',
380 'article_id' => '1',
381 'user_id' => '4',
382 'comment' => 'Second Comment for First Article',
383 'published' => 'Y',
384 'created' => '2007-03-18 10:47:23',
385 'updated' => '2007-03-18 10:49:31'
386 ),
387 'User' => array(
388 'id' => '4',
389 'user' => 'garrett',
390 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
391 'created' => '2007-03-17 01:22:23',
392 'updated' => '2007-03-17 01:24:31'
393 ),
394 'Article' => array(
395 'id' => '1',
396 'user_id' => '1',
397 'title' => 'First Article',
398 'body' => 'First Article Body',
399 'published' => 'Y',
400 'created' => '2007-03-18 10:39:23',
401 'updated' => '2007-03-18 10:41:31'
402 )),
403 array(
404 'Comment' => array(
405 'id' => '3',
406 'article_id' => '1',
407 'user_id' => '1',
408 'comment' => 'Third Comment for First Article',
409 'published' => 'Y',
410 'created' => '2007-03-18 10:49:23',
411 'updated' => '2007-03-18 10:51:31'
412 ),
413 'User' => array(
414 'id' => '1',
415 'user' => 'mariano',
416 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
417 'created' => '2007-03-17 01:16:23',
418 'updated' => '2007-03-17 01:18:31'
419 ),
420 'Article' => array(
421 'id' => '1',
422 'user_id' => '1',
423 'title' => 'First Article',
424 'body' => 'First Article Body',
425 'published' => 'Y',
426 'created' => '2007-03-18 10:39:23',
427 'updated' => '2007-03-18 10:41:31'
428 )),
429 array(
430 'Comment' => array(
431 'id' => '4',
432 'article_id' => '1',
433 'user_id' => '1',
434 'comment' => 'Fourth Comment for First Article',
435 'published' => 'N',
436 'created' => '2007-03-18 10:51:23',
437 'updated' => '2007-03-18 10:53:31'
438 ),
439 'User' => array(
440 'id' => '1',
441 'user' => 'mariano',
442 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
443 'created' => '2007-03-17 01:16:23',
444 'updated' => '2007-03-17 01:18:31'
445 ),
446 'Article' => array(
447 'id' => '1',
448 'user_id' => '1',
449 'title' => 'First Article',
450 'body' => 'First Article Body',
451 'published' => 'Y',
452 'created' => '2007-03-18 10:39:23',
453 'updated' => '2007-03-18 10:41:31'
454 )),
455 array(
456 'Comment' => array(
457 'id' => '5',
458 'article_id' => '2',
459 'user_id' => '1',
460 'comment' => 'First Comment for Second Article',
461 'published' => 'Y',
462 'created' => '2007-03-18 10:53:23',
463 'updated' => '2007-03-18 10:55:31'
464 ),
465 'User' => array(
466 'id' => '1',
467 'user' => 'mariano',
468 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
469 'created' => '2007-03-17 01:16:23',
470 'updated' => '2007-03-17 01:18:31'
471 ),
472 'Article' => array(
473 'id' => '2',
474 'user_id' => '3',
475 'title' => 'Second Article',
476 'body' => 'Second Article Body',
477 'published' => 'Y',
478 'created' => '2007-03-18 10:41:23',
479 'updated' => '2007-03-18 10:43:31'
480 )),
481 array(
482 'Comment' => array(
483 'id' => '6',
484 'article_id' => '2',
485 'user_id' => '2',
486 'comment' => 'Second Comment for Second Article',
487 'published' => 'Y',
488 'created' => '2007-03-18 10:55:23',
489 'updated' => '2007-03-18 10:57:31'
490 ),
491 'User' => array(
492 'id' => '2',
493 'user' => 'nate',
494 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
495 'created' => '2007-03-17 01:18:23',
496 'updated' => '2007-03-17 01:20:31'
497 ),
498 'Article' => array(
499 'id' => '2',
500 'user_id' => '3',
501 'title' => 'Second Article',
502 'body' => 'Second Article Body',
503 'published' => 'Y',
504 'created' => '2007-03-18 10:41:23',
505 'updated' => '2007-03-18 10:43:31'
506 )));
507 $this->assertEqual($TestModel->Comment->find('all'), $expected);
508
509 foreach (array('User', 'Comment') as $class) {
510 $this->_fixtures[$this->_fixtureClassMap[$class]]->drop($db2);
511 }
512 }
513
514 /**
515 * testDisplayField method
516 *
517 * @access public
518 * @return void
519 */
520 function testDisplayField() {
521 $this->loadFixtures('Post', 'Comment', 'Person');
522 $Post = new Post();
523 $Comment = new Comment();
524 $Person = new Person();
525
526 $this->assertEqual($Post->displayField, 'title');
527 $this->assertEqual($Person->displayField, 'name');
528 $this->assertEqual($Comment->displayField, 'id');
529 }
530
531 /**
532 * testSchema method
533 *
534 * @access public
535 * @return void
536 */
537 function testSchema() {
538 $Post = new Post();
539
540 $result = $Post->schema();
541 $columns = array('id', 'author_id', 'title', 'body', 'published', 'created', 'updated');
542 $this->assertEqual(array_keys($result), $columns);
543
544 $types = array('integer', 'integer', 'string', 'text', 'string', 'datetime', 'datetime');
545 $this->assertEqual(Set::extract(array_values($result), '{n}.type'), $types);
546
547 $result = $Post->schema('body');
548 $this->assertEqual($result['type'], 'text');
549 $this->assertNull($Post->schema('foo'));
550
551 $this->assertEqual($Post->getColumnTypes(), array_combine($columns, $types));
552 }
553
554 /**
555 * test deconstruct() with time fields.
556 *
557 * @return void
558 */
559 function testDeconstructFieldsTime() {
560 $this->loadFixtures('Apple');
561 $TestModel =& new Apple();
562
563 $data = array();
564 $data['Apple']['mytime']['hour'] = '';
565 $data['Apple']['mytime']['min'] = '';
566 $data['Apple']['mytime']['sec'] = '';
567
568 $TestModel->data = null;
569 $TestModel->set($data);
570 $expected = array('Apple'=> array('mytime'=> ''));
571 $this->assertEqual($TestModel->data, $expected);
572
573 $data = array();
574 $data['Apple']['mytime']['hour'] = '';
575 $data['Apple']['mytime']['min'] = '';
576 $data['Apple']['mytime']['meridan'] = '';
577
578 $TestModel->data = null;
579 $TestModel->set($data);
580 $expected = array('Apple'=> array('mytime'=> ''));
581 $this->assertEqual($TestModel->data, $expected, 'Empty values are not returning properly. %s');
582
583 $data = array();
584 $data['Apple']['mytime']['hour'] = '12';
585 $data['Apple']['mytime']['min'] = '0';
586 $data['Apple']['mytime']['meridian'] = 'am';
587
588 $TestModel->data = null;
589 $TestModel->set($data);
590 $expected = array('Apple'=> array('mytime'=> '00:00:00'));
591 $this->assertEqual($TestModel->data, $expected, 'Midnight is not returning proper values. %s');
592
593 $data = array();
594 $data['Apple']['mytime']['hour'] = '00';
595 $data['Apple']['mytime']['min'] = '00';
596
597 $TestModel->data = null;
598 $TestModel->set($data);
599 $expected = array('Apple'=> array('mytime'=> '00:00:00'));
600 $this->assertEqual($TestModel->data, $expected, 'Midnight is not returning proper values. %s');
601
602 $data = array();
603 $data['Apple']['mytime']['hour'] = '03';
604 $data['Apple']['mytime']['min'] = '04';
605 $data['Apple']['mytime']['sec'] = '04';
606
607 $TestModel->data = null;
608 $TestModel->set($data);
609 $expected = array('Apple'=> array('mytime'=> '03:04:04'));
610 $this->assertEqual($TestModel->data, $expected);
611
612 $data = array();
613 $data['Apple']['mytime']['hour'] = '3';
614 $data['Apple']['mytime']['min'] = '4';
615 $data['Apple']['mytime']['sec'] = '4';
616
617 $TestModel->data = null;
618 $TestModel->set($data);
619 $expected = array('Apple' => array('mytime'=> '03:04:04'));
620 $this->assertEqual($TestModel->data, $expected);
621
622 $data = array();
623 $data['Apple']['mytime']['hour'] = '03';
624 $data['Apple']['mytime']['min'] = '4';
625 $data['Apple']['mytime']['sec'] = '4';
626
627 $TestModel->data = null;
628 $TestModel->set($data);
629 $expected = array('Apple'=> array('mytime'=> '03:04:04'));
630 $this->assertEqual($TestModel->data, $expected);
631
632 $db = ConnectionManager::getDataSource('test_suite');
633 $data = array();
634 $data['Apple']['mytime'] = $db->expression('NOW()');
635 $TestModel->data = null;
636 $TestModel->set($data);
637 $this->assertEqual($TestModel->data, $data);
638 }
639
640 /**
641 * testDeconstructFields with datetime, timestamp, and date fields
642 *
643 * @access public
644 * @return void
645 */
646 function testDeconstructFieldsDateTime() {
647 $this->loadFixtures('Apple');
648 $TestModel =& new Apple();
649
650 //test null/empty values first
651 $data['Apple']['created']['year'] = '';
652 $data['Apple']['created']['month'] = '';
653 $data['Apple']['created']['day'] = '';
654 $data['Apple']['created']['hour'] = '';
655 $data['Apple']['created']['min'] = '';
656 $data['Apple']['created']['sec'] = '';
657
658 $TestModel->data = null;
659 $TestModel->set($data);
660 $expected = array('Apple'=> array('created'=> ''));
661 $this->assertEqual($TestModel->data, $expected);
662
663 $data = array();
664 $data['Apple']['date']['year'] = '';
665 $data['Apple']['date']['month'] = '';
666 $data['Apple']['date']['day'] = '';
667
668 $TestModel->data = null;
669 $TestModel->set($data);
670 $expected = array('Apple'=> array('date'=> ''));
671 $this->assertEqual($TestModel->data, $expected);
672
673 $data = array();
674 $data['Apple']['created']['year'] = '2007';
675 $data['Apple']['created']['month'] = '08';
676 $data['Apple']['created']['day'] = '20';
677 $data['Apple']['created']['hour'] = '';
678 $data['Apple']['created']['min'] = '';
679 $data['Apple']['created']['sec'] = '';
680
681 $TestModel->data = null;
682 $TestModel->set($data);
683 $expected = array('Apple'=> array('created'=> '2007-08-20 00:00:00'));
684 $this->assertEqual($TestModel->data, $expected);
685
686 $data = array();
687 $data['Apple']['created']['year'] = '2007';
688 $data['Apple']['created']['month'] = '08';
689 $data['Apple']['created']['day'] = '20';
690 $data['Apple']['created']['hour'] = '10';
691 $data['Apple']['created']['min'] = '12';
692 $data['Apple']['created']['sec'] = '';
693
694 $TestModel->data = null;
695 $TestModel->set($data);
696 $expected = array('Apple'=> array('created'=> '2007-08-20 10:12:00'));
697 $this->assertEqual($TestModel->data, $expected);
698
699 $data = array();
700 $data['Apple']['created']['year'] = '2007';
701 $data['Apple']['created']['month'] = '';
702 $data['Apple']['created']['day'] = '12';
703 $data['Apple']['created']['hour'] = '20';
704 $data['Apple']['created']['min'] = '';
705 $data['Apple']['created']['sec'] = '';
706
707 $TestModel->data = null;
708 $TestModel->set($data);
709 $expected = array('Apple'=> array('created'=> ''));
710 $this->assertEqual($TestModel->data, $expected);
711
712 $data = array();
713 $data['Apple']['created']['hour'] = '20';
714 $data['Apple']['created']['min'] = '33';
715
716 $TestModel->data = null;
717 $TestModel->set($data);
718 $expected = array('Apple'=> array('created'=> ''));
719 $this->assertEqual($TestModel->data, $expected);
720
721 $data = array();
722 $data['Apple']['created']['hour'] = '20';
723 $data['Apple']['created']['min'] = '33';
724 $data['Apple']['created']['sec'] = '33';
725
726 $TestModel->data = null;
727 $TestModel->set($data);
728 $expected = array('Apple'=> array('created'=> ''));
729 $this->assertEqual($TestModel->data, $expected);
730
731 $data = array();
732 $data['Apple']['created']['hour'] = '13';
733 $data['Apple']['created']['min'] = '00';
734 $data['Apple']['date']['year'] = '2006';
735 $data['Apple']['date']['month'] = '12';
736 $data['Apple']['date']['day'] = '25';
737
738 $TestModel->data = null;
739 $TestModel->set($data);
740 $expected = array(
741 'Apple'=> array(
742 'created'=> '',
743 'date'=> '2006-12-25'
744 ));
745 $this->assertEqual($TestModel->data, $expected);
746
747 $data = array();
748 $data['Apple']['created']['year'] = '2007';
749 $data['Apple']['created']['month'] = '08';
750 $data['Apple']['created']['day'] = '20';
751 $data['Apple']['created']['hour'] = '10';
752 $data['Apple']['created']['min'] = '12';
753 $data['Apple']['created']['sec'] = '09';
754 $data['Apple']['date']['year'] = '2006';
755 $data['Apple']['date']['month'] = '12';
756 $data['Apple']['date']['day'] = '25';
757
758 $TestModel->data = null;
759 $TestModel->set($data);
760 $expected = array(
761 'Apple'=> array(
762 'created'=> '2007-08-20 10:12:09',
763 'date'=> '2006-12-25'
764 ));
765 $this->assertEqual($TestModel->data, $expected);
766
767 $data = array();
768 $data['Apple']['created']['year'] = '--';
769 $data['Apple']['created']['month'] = '--';
770 $data['Apple']['created']['day'] = '--';
771 $data['Apple']['created']['hour'] = '--';
772 $data['Apple']['created']['min'] = '--';
773 $data['Apple']['created']['sec'] = '--';
774 $data['Apple']['date']['year'] = '--';
775 $data['Apple']['date']['month'] = '--';
776 $data['Apple']['date']['day'] = '--';
777
778 $TestModel->data = null;
779 $TestModel->set($data);
780 $expected = array('Apple'=> array('created'=> '', 'date'=> ''));
781 $this->assertEqual($TestModel->data, $expected);
782
783 $data = array();
784 $data['Apple']['created']['year'] = '2007';
785 $data['Apple']['created']['month'] = '--';
786 $data['Apple']['created']['day'] = '20';
787 $data['Apple']['created']['hour'] = '10';
788 $data['Apple']['created']['min'] = '12';
789 $data['Apple']['created']['sec'] = '09';
790 $data['Apple']['date']['year'] = '2006';
791 $data['Apple']['date']['month'] = '12';
792 $data['Apple']['date']['day'] = '25';
793
794 $TestModel->data = null;
795 $TestModel->set($data);
796 $expected = array('Apple'=> array('created'=> '', 'date'=> '2006-12-25'));
797 $this->assertEqual($TestModel->data, $expected);
798
799 $data = array();
800 $data['Apple']['date']['year'] = '2006';
801 $data['Apple']['date']['month'] = '12';
802 $data['Apple']['date']['day'] = '25';
803
804 $TestModel->data = null;
805 $TestModel->set($data);
806 $expected = array('Apple'=> array('date'=> '2006-12-25'));
807 $this->assertEqual($TestModel->data, $expected);
808
809 $db = ConnectionManager::getDataSource('test_suite');
810 $data = array();
811 $data['Apple']['modified'] = $db->expression('NOW()');
812 $TestModel->data = null;
813 $TestModel->set($data);
814 $this->assertEqual($TestModel->data, $data);
815 }
816
817 /**
818 * testTablePrefixSwitching method
819 *
820 * @access public
821 * @return void
822 */
823 function testTablePrefixSwitching() {
824 ConnectionManager::create('database1',
825 array_merge($this->db->config, array('prefix' => 'aaa_')
826 ));
827 ConnectionManager::create('database2',
828 array_merge($this->db->config, array('prefix' => 'bbb_')
829 ));
830
831 $db1 = ConnectionManager::getDataSource('database1');
832 $db2 = ConnectionManager::getDataSource('database2');
833
834 $TestModel = new Apple();
835 $TestModel->setDataSource('database1');
836 $this->assertEqual($this->db->fullTableName($TestModel, false), 'aaa_apples');
837 $this->assertEqual($db1->fullTableName($TestModel, false), 'aaa_apples');
838 $this->assertEqual($db2->fullTableName($TestModel, false), 'aaa_apples');
839
840 $TestModel->setDataSource('database2');
841 $this->assertEqual($this->db->fullTableName($TestModel, false), 'bbb_apples');
842 $this->assertEqual($db1->fullTableName($TestModel, false), 'bbb_apples');
843 $this->assertEqual($db2->fullTableName($TestModel, false), 'bbb_apples');
844
845 $TestModel = new Apple();
846 $TestModel->tablePrefix = 'custom_';
847 $this->assertEqual($this->db->fullTableName($TestModel, false), 'custom_apples');
848 $TestModel->setDataSource('database1');
849 $this->assertEqual($this->db->fullTableName($TestModel, false), 'custom_apples');
850 $this->assertEqual($db1->fullTableName($TestModel, false), 'custom_apples');
851
852 $TestModel = new Apple();
853 $TestModel->setDataSource('database1');
854 $this->assertEqual($this->db->fullTableName($TestModel, false), 'aaa_apples');
855 $TestModel->tablePrefix = '';
856 $TestModel->setDataSource('database2');
857 $this->assertEqual($db2->fullTableName($TestModel, false), 'apples');
858 $this->assertEqual($db1->fullTableName($TestModel, false), 'apples');
859
860 $TestModel->tablePrefix = null;
861 $TestModel->setDataSource('database1');
862 $this->assertEqual($db2->fullTableName($TestModel, false), 'aaa_apples');
863 $this->assertEqual($db1->fullTableName($TestModel, false), 'aaa_apples');
864
865 $TestModel->tablePrefix = false;
866 $TestModel->setDataSource('database2');
867 $this->assertEqual($db2->fullTableName($TestModel, false), 'apples');
868 $this->assertEqual($db1->fullTableName($TestModel, false), 'apples');
869 }
870
871 /**
872 * Tests validation parameter order in custom validation methods
873 *
874 * @access public
875 * @return void
876 */
877 function testInvalidAssociation() {
878 $TestModel =& new ValidationTest1();
879 $this->assertNull($TestModel->getAssociated('Foo'));
880 }
881
882 /**
883 * testLoadModelSecondIteration method
884 *
885 * @access public
886 * @return void
887 */
888 function testLoadModelSecondIteration() {
889 $model = new ModelA();
890 $this->assertIsA($model,'ModelA');
891
892 $this->assertIsA($model->ModelB, 'ModelB');
893 $this->assertIsA($model->ModelB->ModelD, 'ModelD');
894
895 $this->assertIsA($model->ModelC, 'ModelC');
896 $this->assertIsA($model->ModelC->ModelD, 'ModelD');
897 }
898
899 /**
900 * ensure that exists() does not persist between method calls reset on create
901 *
902 * @return void
903 */
904 function testResetOfExistsOnCreate() {
905 $this->loadFixtures('Article');
906 $Article =& new Article();
907 $Article->id = 1;
908 $Article->saveField('title', 'Reset me');
909 $Article->delete();
910 $Article->id = 1;
911 $this->assertFalse($Article->exists());
912
913 $Article->create();
914 $this->assertFalse($Article->exists());
915 $Article->id = 2;
916 $Article->saveField('title', 'Staying alive');
917 $result = $Article->read(null, 2);
918 $this->assertEqual($result['Article']['title'], 'Staying alive');
919 }
920
921 /**
922 * testUseTableFalseExistsCheck method
923 *
924 * @return void
925 */
926 function testUseTableFalseExistsCheck() {
927 $this->loadFixtures('Article');
928 $Article =& new Article();
929 $Article->id = 1337;
930 $result = $Article->exists();
931 $this->assertFalse($result);
932
933 $Article->useTable = false;
934 $Article->id = null;
935 $result = $Article->exists();
936 $this->assertFalse($result);
937
938 // An article with primary key of '1' has been loaded by the fixtures.
939 $Article->useTable = false;
940 $Article->id = 1;
941 $result = $Article->exists();
942 $this->assertTrue($result);
943 }
944
945 /**
946 * testPluginAssociations method
947 *
948 * @access public
949 * @return void
950 */
951 function testPluginAssociations() {
952 $this->loadFixtures('TestPluginArticle', 'User', 'TestPluginComment');
953 $TestModel =& new TestPluginArticle();
954
955 $result = $TestModel->find('all');
956 $expected = array(
957 array(
958 'TestPluginArticle' => array(
959 'id' => 1,
960 'user_id' => 1,
961 'title' => 'First Plugin Article',
962 'body' => 'First Plugin Article Body',
963 'published' => 'Y',
964 'created' => '2008-09-24 10:39:23',
965 'updated' => '2008-09-24 10:41:31'
966 ),
967 'User' => array(
968 'id' => 1,
969 'user' => 'mariano',
970 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
971 'created' => '2007-03-17 01:16:23',
972 'updated' => '2007-03-17 01:18:31'
973 ),
974 'TestPluginComment' => array(
975 array(
976 'id' => 1,
977 'article_id' => 1,
978 'user_id' => 2,
979 'comment' => 'First Comment for First Plugin Article',
980 'published' => 'Y',
981 'created' => '2008-09-24 10:45:23',
982 'updated' => '2008-09-24 10:47:31'
983 ),
984 array(
985 'id' => 2,
986 'article_id' => 1,
987 'user_id' => 4,
988 'comment' => 'Second Comment for First Plugin Article',
989 'published' => 'Y',
990 'created' => '2008-09-24 10:47:23',
991 'updated' => '2008-09-24 10:49:31'
992 ),
993 array(
994 'id' => 3,
995 'article_id' => 1,
996 'user_id' => 1,
997 'comment' => 'Third Comment for First Plugin Article',
998 'published' => 'Y',
999 'created' => '2008-09-24 10:49:23',
1000 'updated' => '2008-09-24 10:51:31'
1001 ),
1002 array(
1003 'id' => 4,
1004 'article_id' => 1,
1005 'user_id' => 1,
1006 'comment' => 'Fourth Comment for First Plugin Article',
1007 'published' => 'N',
1008 'created' => '2008-09-24 10:51:23',
1009 'updated' => '2008-09-24 10:53:31'
1010 ))),
1011 array(
1012 'TestPluginArticle' => array(
1013 'id' => 2,
1014 'user_id' => 3,
1015 'title' => 'Second Plugin Article',
1016 'body' => 'Second Plugin Article Body',
1017 'published' => 'Y',
1018 'created' => '2008-09-24 10:41:23',
1019 'updated' => '2008-09-24 10:43:31'
1020 ),
1021 'User' => array(
1022 'id' => 3,
1023 'user' => 'larry',
1024 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
1025 'created' => '2007-03-17 01:20:23',
1026 'updated' => '2007-03-17 01:22:31'
1027 ),
1028 'TestPluginComment' => array(
1029 array(
1030 'id' => 5,
1031 'article_id' => 2,
1032 'user_id' => 1,
1033 'comment' => 'First Comment for Second Plugin Article',
1034 'published' => 'Y',
1035 'created' => '2008-09-24 10:53:23',
1036 'updated' => '2008-09-24 10:55:31'
1037 ),
1038 array(
1039 'id' => 6,
1040 'article_id' => 2,
1041 'user_id' => 2,
1042 'comment' => 'Second Comment for Second Plugin Article',
1043 'published' => 'Y',
1044 'created' => '2008-09-24 10:55:23',
1045 'updated' => '2008-09-24 10:57:31'
1046 ))),
1047 array(
1048 'TestPluginArticle' => array(
1049 'id' => 3,
1050 'user_id' => 1,
1051 'title' => 'Third Plugin Article',
1052 'body' => 'Third Plugin Article Body',
1053 'published' => 'Y',
1054 'created' => '2008-09-24 10:43:23',
1055 'updated' => '2008-09-24 10:45:31'
1056 ),
1057 'User' => array(
1058 'id' => 1,
1059 'user' => 'mariano',
1060 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
1061 'created' => '2007-03-17 01:16:23',
1062 'updated' => '2007-03-17 01:18:31'
1063 ),
1064 'TestPluginComment' => array()
1065 ));
1066
1067 $this->assertEqual($result, $expected);
1068 }
1069
1070 /**
1071 * Tests getAssociated method
1072 *
1073 * @access public
1074 * @return void
1075 */
1076 function testGetAssociated() {
1077 $this->loadFixtures('Article');
1078 $Article = ClassRegistry::init('Article');
1079
1080 $assocTypes = array('hasMany', 'hasOne', 'belongsTo', 'hasAndBelongsToMany');
1081 foreach ($assocTypes as $type) {
1082 $this->assertEqual($Article->getAssociated($type), array_keys($Article->{$type}));
1083 }
1084
1085 $Article->bindModel(array('hasMany' => array('Category')));
1086 $this->assertEqual($Article->getAssociated('hasMany'), array('Comment', 'Category'));
1087
1088 $results = $Article->getAssociated();
1089 $this->assertEqual(sort(array_keys($results)), array('Category', 'Comment', 'Tag'));
1090
1091 $Article->unbindModel(array('hasAndBelongsToMany' => array('Tag')));
1092 $this->assertEqual($Article->getAssociated('hasAndBelongsToMany'), array());
1093
1094 $result = $Article->getAssociated('Category');
1095 $expected = array(
1096 'className' => 'Category',
1097 'foreignKey' => 'article_id',
1098 'conditions' => '',
1099 'fields' => '',
1100 'order' => '',
1101 'limit' => '',
1102 'offset' => '',
1103 'dependent' => '',
1104 'exclusive' => '',
1105 'finderQuery' => '',
1106 'counterQuery' => '',
1107 'association' => 'hasMany',
1108 );
1109 $this->assertEqual($result, $expected);
1110 }
1111
1112 /**
1113 * testAutoConstructAssociations method
1114 *
1115 * @access public
1116 * @return void
1117 */
1118 function testAutoConstructAssociations() {
1119 $this->loadFixtures('User', 'ArticleFeatured');
1120 $TestModel =& new AssociationTest1();
1121
1122 $result = $TestModel->hasAndBelongsToMany;
1123 $expected = array('AssociationTest2' => array(
1124 'unique' => false,
1125 'joinTable' => 'join_as_join_bs',
1126 'foreignKey' => false,
1127 'className' => 'AssociationTest2',
1128 'with' => 'JoinAsJoinB',
1129 'associationForeignKey' => 'join_b_id',
1130 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '',
1131 'finderQuery' => '', 'deleteQuery' => '', 'insertQuery' => ''
1132 ));
1133 $this->assertEqual($result, $expected);
1134
1135 // Tests related to ticket https://trac.cakephp.org/ticket/5594
1136 $TestModel =& new ArticleFeatured();
1137 $TestFakeModel =& new ArticleFeatured(array('table' => false));
1138
1139 $expected = array(
1140 'User' => array(
1141 'className' => 'User', 'foreignKey' => 'user_id',
1142 'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => ''
1143 ),
1144 'Category' => array(
1145 'className' => 'Category', 'foreignKey' => 'category_id',
1146 'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => ''
1147 )
1148 );
1149 $this->assertIdentical($TestModel->belongsTo, $expected);
1150 $this->assertIdentical($TestFakeModel->belongsTo, $expected);
1151
1152 $this->assertEqual($TestModel->User->name, 'User');
1153 $this->assertEqual($TestFakeModel->User->name, 'User');
1154 $this->assertEqual($TestModel->Category->name, 'Category');
1155 $this->assertEqual($TestFakeModel->Category->name, 'Category');
1156
1157 $expected = array(
1158 'Featured' => array(
1159 'className' => 'Featured',
1160 'foreignKey' => 'article_featured_id',
1161 'conditions' => '',
1162 'fields' => '',
1163 'order' => '',
1164 'dependent' => ''
1165 ));
1166
1167 $this->assertIdentical($TestModel->hasOne, $expected);
1168 $this->assertIdentical($TestFakeModel->hasOne, $expected);
1169
1170 $this->assertEqual($TestModel->Featured->name, 'Featured');
1171 $this->assertEqual($TestFakeModel->Featured->name, 'Featured');
1172
1173 $expected = array(
1174 'Comment' => array(
1175 'className' => 'Comment',
1176 'dependent' => true,
1177 'foreignKey' => 'article_featured_id',
1178 'conditions' => '',
1179 'fields' => '',
1180 'order' => '',
1181 'limit' => '',
1182 'offset' => '',
1183 'exclusive' => '',
1184 'finderQuery' => '',
1185 'counterQuery' => ''
1186 ));
1187
1188 $this->assertIdentical($TestModel->hasMany, $expected);
1189 $this->assertIdentical($TestFakeModel->hasMany, $expected);
1190
1191 $this->assertEqual($TestModel->Comment->name, 'Comment');
1192 $this->assertEqual($TestFakeModel->Comment->name, 'Comment');
1193
1194 $expected = array(
1195 'Tag' => array(
1196 'className' => 'Tag',
1197 'joinTable' => 'article_featureds_tags',
1198 'with' => 'ArticleFeaturedsTag',
1199 'foreignKey' => 'article_featured_id',
1200 'associationForeignKey' => 'tag_id',
1201 'conditions' => '',
1202 'fields' => '',
1203 'order' => '',
1204 'limit' => '',
1205 'offset' => '',
1206 'unique' => true,
1207 'finderQuery' => '',
1208 'deleteQuery' => '',
1209 'insertQuery' => ''
1210 ));
1211
1212 $this->assertIdentical($TestModel->hasAndBelongsToMany, $expected);
1213 $this->assertIdentical($TestFakeModel->hasAndBelongsToMany, $expected);
1214
1215 $this->assertEqual($TestModel->Tag->name, 'Tag');
1216 $this->assertEqual($TestFakeModel->Tag->name, 'Tag');
1217 }
1218
1219 /**
1220 * test Model::__construct
1221 *
1222 * ensure that $actsAS and $_findMethods are merged.
1223 *
1224 * @return void
1225 */
1226 function testConstruct() {
1227 $this->loadFixtures('Post', 'Comment');
1228
1229 $TestModel =& ClassRegistry::init('MergeVarPluginPost');
1230 $this->assertEqual($TestModel->actsAs, array('Containable', 'Tree'));
1231 $this->assertTrue(isset($TestModel->Behaviors->Containable));
1232 $this->assertTrue(isset($TestModel->Behaviors->Tree));
1233
1234 $TestModel =& ClassRegistry::init('MergeVarPluginComment');
1235 $expected = array('Containable', 'Containable' => array('some_settings'));
1236 $this->assertEqual($TestModel->actsAs, $expected);
1237 $this->assertTrue(isset($TestModel->Behaviors->Containable));
1238 }
1239
1240 /**
1241 * test Model::__construct
1242 *
1243 * ensure that $actsAS and $_findMethods are merged.
1244 *
1245 * @return void
1246 */
1247 function testConstructWithAlternateDataSource() {
1248 $TestModel =& ClassRegistry::init(array(
1249 'class' => 'DoesntMatter', 'ds' => 'test_suite', 'table' => false
1250 ));
1251 $this->assertEqual('test_suite', $TestModel->useDbConfig);
1252
1253 //deprecated but test it anyway
1254 $NewVoid =& new TheVoid(null, false, 'other');
1255 $this->assertEqual('other', $NewVoid->useDbConfig);
1256 }
1257
1258 /**
1259 * testColumnTypeFetching method
1260 *
1261 * @access public
1262 * @return void
1263 */
1264 function testColumnTypeFetching() {
1265 $model =& new Test();
1266 $this->assertEqual($model->getColumnType('id'), 'integer');
1267 $this->assertEqual($model->getColumnType('notes'), 'text');
1268 $this->assertEqual($model->getColumnType('updated'), 'datetime');
1269 $this->assertEqual($model->getColumnType('unknown'), null);
1270
1271 $model =& new Article();
1272 $this->assertEqual($model->getColumnType('User.created'), 'datetime');
1273 $this->assertEqual($model->getColumnType('Tag.id'), 'integer');
1274 $this->assertEqual($model->getColumnType('Article.id'), 'integer');
1275 }
1276
1277 /**
1278 * testHabtmUniqueKey method
1279 *
1280 * @access public
1281 * @return void
1282 */
1283 function testHabtmUniqueKey() {
1284 $model =& new Item();
1285 $this->assertFalse($model->hasAndBelongsToMany['Portfolio']['unique']);
1286 }
1287
1288 /**
1289 * testIdentity method
1290 *
1291 * @access public
1292 * @return void
1293 */
1294 function testIdentity() {
1295 $TestModel =& new Test();
1296 $result = $TestModel->alias;
1297 $expected = 'Test';
1298 $this->assertEqual($result, $expected);
1299
1300 $TestModel =& new TestAlias();
1301 $result = $TestModel->alias;
1302 $expected = 'TestAlias';
1303 $this->assertEqual($result, $expected);
1304
1305 $TestModel =& new Test(array('alias' => 'AnotherTest'));
1306 $result = $TestModel->alias;
1307 $expected = 'AnotherTest';
1308 $this->assertEqual($result, $expected);
1309 }
1310
1311 /**
1312 * testWithAssociation method
1313 *
1314 * @access public
1315 * @return void
1316 */
1317 function testWithAssociation() {
1318 $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
1319 $TestModel =& new Something();
1320 $result = $TestModel->SomethingElse->find('all');
1321
1322 $expected = array(
1323 array(
1324 'SomethingElse' => array(
1325 'id' => '1',
1326 'title' => 'First Post',
1327 'body' => 'First Post Body',
1328 'published' => 'Y',
1329 'created' => '2007-03-18 10:39:23',
1330 'updated' => '2007-03-18 10:41:31'
1331 ),
1332 'Something' => array(
1333 array(
1334 'id' => '3',
1335 'title' => 'Third Post',
1336 'body' => 'Third Post Body',
1337 'published' => 'Y',
1338 'created' => '2007-03-18 10:43:23',
1339 'updated' => '2007-03-18 10:45:31',
1340 'JoinThing' => array(
1341 'id' => '3',
1342 'something_id' => '3',
1343 'something_else_id' => '1',
1344 'doomed' => '1',
1345 'created' => '2007-03-18 10:43:23',
1346 'updated' => '2007-03-18 10:45:31'
1347 )))),
1348 array(
1349 'SomethingElse' => array(
1350 'id' => '2',
1351 'title' => 'Second Post',
1352 'body' => 'Second Post Body',
1353 'published' => 'Y',
1354 'created' => '2007-03-18 10:41:23',
1355 'updated' => '2007-03-18 10:43:31'
1356 ),
1357 'Something' => array(
1358 array(
1359 'id' => '1',
1360 'title' => 'First Post',
1361 'body' => 'First Post Body',
1362 'published' => 'Y',
1363 'created' => '2007-03-18 10:39:23',
1364 'updated' => '2007-03-18 10:41:31',
1365 'JoinThing' => array(
1366 'id' => '1',
1367 'something_id' => '1',
1368 'something_else_id' => '2',
1369 'doomed' => '1',
1370 'created' => '2007-03-18 10:39:23',
1371 'updated' => '2007-03-18 10:41:31'
1372 )))),
1373 array(
1374 'SomethingElse' => array(
1375 'id' => '3',
1376 'title' => 'Third Post',
1377 'body' => 'Third Post Body',
1378 'published' => 'Y',
1379 'created' => '2007-03-18 10:43:23',
1380 'updated' => '2007-03-18 10:45:31'
1381 ),
1382 'Something' => array(
1383 array(
1384 'id' => '2',
1385 'title' => 'Second Post',
1386 'body' => 'Second Post Body',
1387 'published' => 'Y',
1388 'created' => '2007-03-18 10:41:23',
1389 'updated' => '2007-03-18 10:43:31',
1390 'JoinThing' => array(
1391 'id' => '2',
1392 'something_id' => '2',
1393 'something_else_id' => '3',
1394 'doomed' => '0',
1395 'created' => '2007-03-18 10:41:23',
1396 'updated' => '2007-03-18 10:43:31'
1397 )))));
1398 $this->assertEqual($result, $expected);
1399
1400 $result = $TestModel->find('all');
1401 $expected = array(
1402 array(
1403 'Something' => array(
1404 'id' => '1',
1405 'title' => 'First Post',
1406 'body' => 'First Post Body',
1407 'published' => 'Y',
1408 'created' => '2007-03-18 10:39:23',
1409 'updated' => '2007-03-18 10:41:31'
1410 ),
1411 'SomethingElse' => array(
1412 array(
1413 'id' => '2',
1414 'title' => 'Second Post',
1415 'body' => 'Second Post Body',
1416 'published' => 'Y',
1417 'created' => '2007-03-18 10:41:23',
1418 'updated' => '2007-03-18 10:43:31',
1419 'JoinThing' => array(
1420 'doomed' => '1',
1421 'something_id' => '1',
1422 'something_else_id' => '2'
1423 )))),
1424 array(
1425 'Something' => array(
1426 'id' => '2',
1427 'title' => 'Second Post',
1428 'body' => 'Second Post Body',
1429 'published' => 'Y',
1430 'created' => '2007-03-18 10:41:23',
1431 'updated' => '2007-03-18 10:43:31'
1432 ),
1433 'SomethingElse' => array(
1434 array(
1435 'id' => '3',
1436 'title' => 'Third Post',
1437 'body' => 'Third Post Body',
1438 'published' => 'Y',
1439 'created' => '2007-03-18 10:43:23',
1440 'updated' => '2007-03-18 10:45:31',
1441 'JoinThing' => array(
1442 'doomed' => '0',
1443 'something_id' => '2',
1444 'something_else_id' => '3'
1445 )))),
1446 array(
1447 'Something' => array(
1448 'id' => '3',
1449 'title' => 'Third Post',
1450 'body' => 'Third Post Body',
1451 'published' => 'Y',
1452 'created' => '2007-03-18 10:43:23',
1453 'updated' => '2007-03-18 10:45:31'
1454 ),
1455 'SomethingElse' => array(
1456 array(
1457 'id' => '1',
1458 'title' => 'First Post',
1459 'body' => 'First Post Body',
1460 'published' => 'Y',
1461 'created' => '2007-03-18 10:39:23',
1462 'updated' => '2007-03-18 10:41:31',
1463 'JoinThing' => array(
1464 'doomed' => '1',
1465 'something_id' => '3',
1466 'something_else_id' => '1'
1467 )))));
1468 $this->assertEqual($result, $expected);
1469
1470 $result = $TestModel->findById(1);
1471 $expected = array(
1472 'Something' => array(
1473 'id' => '1',
1474 'title' => 'First Post',
1475 'body' => 'First Post Body',
1476 'published' => 'Y',
1477 'created' => '2007-03-18 10:39:23',
1478 'updated' => '2007-03-18 10:41:31'
1479 ),
1480 'SomethingElse' => array(
1481 array(
1482 'id' => '2',
1483 'title' => 'Second Post',
1484 'body' => 'Second Post Body',
1485 'published' => 'Y',
1486 'created' => '2007-03-18 10:41:23',
1487 'updated' => '2007-03-18 10:43:31',
1488 'JoinThing' => array(
1489 'doomed' => '1',
1490 'something_id' => '1',
1491 'something_else_id' => '2'
1492 ))));
1493 $this->assertEqual($result, $expected);
1494
1495 $expected = $TestModel->findById(1);
1496 $TestModel->set($expected);
1497 $TestModel->save();
1498 $result = $TestModel->findById(1);
1499 $this->assertEqual($result, $expected);
1500
1501 $TestModel->hasAndBelongsToMany['SomethingElse']['unique'] = false;
1502 $TestModel->create(array(
1503 'Something' => array('id' => 1),
1504 'SomethingElse' => array(3, array(
1505 'something_else_id' => 1,
1506 'doomed' => '1'
1507 ))));
1508
1509 $ts = date('Y-m-d H:i:s');
1510 $TestModel->save();
1511
1512 $TestModel->hasAndBelongsToMany['SomethingElse']['order'] = 'SomethingElse.id ASC';
1513 $result = $TestModel->findById(1);
1514 $expected = array(
1515 'Something' => array(
1516 'id' => '1',
1517 'title' => 'First Post',
1518 'body' => 'First Post Body',
1519 'published' => 'Y',
1520 'created' => '2007-03-18 10:39:23',
1521 'updated' => $ts),
1522 'SomethingElse' => array(
1523 array(
1524 'id' => '1',
1525 'title' => 'First Post',
1526 'body' => 'First Post Body',
1527 'published' => 'Y',
1528 'created' => '2007-03-18 10:39:23',
1529 'updated' => '2007-03-18 10:41:31',
1530 'JoinThing' => array(
1531 'doomed' => '1',
1532 'something_id' => '1',
1533 'something_else_id' => '1'
1534 )),
1535 array(
1536 'id' => '2',
1537 'title' => 'Second Post',
1538 'body' => 'Second Post Body',
1539 'published' => 'Y',
1540 'created' => '2007-03-18 10:41:23',
1541 'updated' => '2007-03-18 10:43:31',
1542 'JoinThing' => array(
1543 'doomed' => '1',
1544 'something_id' => '1',
1545 'something_else_id' => '2'
1546 )),
1547 array(
1548 'id' => '3',
1549 'title' => 'Third Post',
1550 'body' => 'Third Post Body',
1551 'published' => 'Y',
1552 'created' => '2007-03-18 10:43:23',
1553 'updated' => '2007-03-18 10:45:31',
1554 'JoinThing' => array(
1555 'doomed' => '0',
1556 'something_id' => '1',
1557 'something_else_id' => '3'
1558 ))));
1559
1560 $this->assertEqual($result, $expected);
1561 }
1562
1563 /**
1564 * testFindSelfAssociations method
1565 *
1566 * @access public
1567 * @return void
1568 */
1569 function testFindSelfAssociations() {
1570 $this->loadFixtures('Person');
1571
1572 $TestModel =& new Person();
1573 $TestModel->recursive = 2;
1574 $result = $TestModel->read(null, 1);
1575 $expected = array(
1576 'Person' => array(
1577 'id' => 1,
1578 'name' => 'person',
1579 'mother_id' => 2,
1580 'father_id' => 3
1581 ),
1582 'Mother' => array(
1583 'id' => 2,
1584 'name' => 'mother',
1585 'mother_id' => 4,
1586 'father_id' => 5,
1587 'Mother' => array(
1588 'id' => 4,
1589 'name' => 'mother - grand mother',
1590 'mother_id' => 0,
1591 'father_id' => 0
1592 ),
1593 'Father' => array(
1594 'id' => 5,
1595 'name' => 'mother - grand father',
1596 'mother_id' => 0,
1597 'father_id' => 0
1598 )),
1599 'Father' => array(
1600 'id' => 3,
1601 'name' => 'father',
1602 'mother_id' => 6,
1603 'father_id' => 7,
1604 'Father' => array(
1605 'id' => 7,
1606 'name' => 'father - grand father',
1607 'mother_id' => 0,
1608 'father_id' => 0
1609 ),
1610 'Mother' => array(
1611 'id' => 6,
1612 'name' => 'father - grand mother',
1613 'mother_id' => 0,
1614 'father_id' => 0
1615 )));
1616
1617 $this->assertEqual($result, $expected);
1618
1619 $TestModel->recursive = 3;
1620 $result = $TestModel->read(null, 1);
1621 $expected = array(
1622 'Person' => array(
1623 'id' => 1,
1624 'name' => 'person',
1625 'mother_id' => 2,
1626 'father_id' => 3
1627 ),
1628 'Mother' => array(
1629 'id' => 2,
1630 'name' => 'mother',
1631 'mother_id' => 4,
1632 'father_id' => 5,
1633 'Mother' => array(
1634 'id' => 4,
1635 'name' => 'mother - grand mother',
1636 'mother_id' => 0,
1637 'father_id' => 0,
1638 'Mother' => array(),
1639 'Father' => array()),
1640 'Father' => array(
1641 'id' => 5,
1642 'name' => 'mother - grand father',
1643 'mother_id' => 0,
1644 'father_id' => 0,
1645 'Father' => array(),
1646 'Mother' => array()
1647 )),
1648 'Father' => array(
1649 'id' => 3,
1650 'name' => 'father',
1651 'mother_id' => 6,
1652 'father_id' => 7,
1653 'Father' => array(
1654 'id' => 7,
1655 'name' => 'father - grand father',
1656 'mother_id' => 0,
1657 'father_id' => 0,
1658 'Father' => array(),
1659 'Mother' => array()
1660 ),
1661 'Mother' => array(
1662 'id' => 6,
1663 'name' => 'father - grand mother',
1664 'mother_id' => 0,
1665 'father_id' => 0,
1666 'Mother' => array(),
1667 'Father' => array()
1668 )));
1669
1670 $this->assertEqual($result, $expected);
1671 }
1672
1673 /**
1674 * testDynamicAssociations method
1675 *
1676 * @access public
1677 * @return void
1678 */
1679 function testDynamicAssociations() {
1680 $this->loadFixtures('Article', 'Comment');
1681 $TestModel =& new Article();
1682
1683 $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->hasOne = array();
1684 $TestModel->hasMany['Comment'] = array_merge($TestModel->hasMany['Comment'], array(
1685 'foreignKey' => false,
1686 'conditions' => array('Comment.user_id =' => '2')
1687 ));
1688 $result = $TestModel->find('all');
1689 $expected = array(
1690 array(
1691 'Article' => array(
1692 'id' => '1',
1693 'user_id' => '1',
1694 'title' => 'First Article',
1695 'body' => 'First Article Body',
1696 'published' => 'Y',
1697 'created' => '2007-03-18 10:39:23',
1698 'updated' => '2007-03-18 10:41:31'
1699 ),
1700 'Comment' => array(
1701 array(
1702 'id' => '1',
1703 'article_id' => '1',
1704 'user_id' => '2',
1705 'comment' => 'First Comment for First Article',
1706 'published' => 'Y',
1707 'created' => '2007-03-18 10:45:23',
1708 'updated' => '2007-03-18 10:47:31'
1709 ),
1710 array(
1711 'id' => '6',
1712 'article_id' => '2',
1713 'user_id' => '2',
1714 'comment' => 'Second Comment for Second Article',
1715 'published' => 'Y',
1716 'created' => '2007-03-18 10:55:23',
1717 'updated' => '2007-03-18 10:57:31'
1718 ))),
1719 array(
1720 'Article' => array(
1721 'id' => '2',
1722 'user_id' => '3',
1723 'title' => 'Second Article',
1724 'body' => 'Second Article Body',
1725 'published' => 'Y',
1726 'created' => '2007-03-18 10:41:23',
1727 'updated' => '2007-03-18 10:43:31'
1728 ),
1729 'Comment' => array(
1730 array(
1731 'id' => '1',
1732 'article_id' => '1',
1733 'user_id' => '2',
1734 'comment' => 'First Comment for First Article',
1735 'published' => 'Y',
1736 'created' => '2007-03-18 10:45:23',
1737 'updated' => '2007-03-18 10:47:31'
1738 ),
1739 array(
1740 'id' => '6',
1741 'article_id' => '2',
1742 'user_id' => '2',
1743 'comment' => 'Second Comment for Second Article',
1744 'published' => 'Y',
1745 'created' => '2007-03-18 10:55:23',
1746 'updated' => '2007-03-18 10:57:31'
1747 ))),
1748 array(
1749 'Article' => array(
1750 'id' => '3',
1751 'user_id' => '1',
1752 'title' => 'Third Article',
1753 'body' => 'Third Article Body',
1754 'published' => 'Y',
1755 'created' => '2007-03-18 10:43:23',
1756 'updated' => '2007-03-18 10:45:31'
1757 ),
1758 'Comment' => array(
1759 array(
1760 'id' => '1',
1761 'article_id' => '1',
1762 'user_id' => '2',
1763 'comment' => 'First Comment for First Article',
1764 'published' => 'Y',
1765 'created' => '2007-03-18 10:45:23',
1766 'updated' => '2007-03-18 10:47:31'
1767 ),
1768 array(
1769 'id' => '6',
1770 'article_id' => '2',
1771 'user_id' => '2',
1772 'comment' => 'Second Comment for Second Article',
1773 'published' => 'Y',
1774 'created' => '2007-03-18 10:55:23',
1775 'updated' => '2007-03-18 10:57:31'
1776 ))));
1777
1778 $this->assertEqual($result, $expected);
1779 }
1780
1781 /**
1782 * testCreation method
1783 *
1784 * @access public
1785 * @return void
1786 */
1787 function testCreation() {
1788 $this->loadFixtures('Article');
1789 $TestModel =& new Test();
1790 $result = $TestModel->create();
1791 $expected = array('Test' => array('notes' => 'write some notes here'));
1792 $this->assertEqual($result, $expected);
1793 $TestModel =& new User();
1794 $result = $TestModel->schema();
1795
1796 if (isset($this->db->columns['primary_key']['length'])) {
1797 $intLength = $this->db->columns['primary_key']['length'];
1798 } elseif (isset($this->db->columns['integer']['length'])) {
1799 $intLength = $this->db->columns['integer']['length'];
1800 } else {
1801 $intLength = 11;
1802 }
1803 foreach (array('collate', 'charset') as $type) {
1804 unset($result['user'][$type]);
1805 unset($result['password'][$type]);
1806 }
1807
1808 $expected = array(
1809 'id' => array(
1810 'type' => 'integer',
1811 'null' => false,
1812 'default' => null,
1813 'length' => $intLength,
1814 'key' => 'primary'
1815 ),
1816 'user' => array(
1817 'type' => 'string',
1818 'null' => false,
1819 'default' => '',
1820 'length' => 255
1821 ),
1822 'password' => array(
1823 'type' => 'string',
1824 'null' => false,
1825 'default' => '',
1826 'length' => 255
1827 ),
1828 'created' => array(
1829 'type' => 'datetime',
1830 'null' => true,
1831 'default' => null,
1832 'length' => null
1833 ),
1834 'updated'=> array(
1835 'type' => 'datetime',
1836 'null' => true,
1837 'default' => null,
1838 'length' => null
1839 ));
1840
1841 $this->assertEqual($result, $expected);
1842
1843 $TestModel =& new Article();
1844 $result = $TestModel->create();
1845 $expected = array('Article' => array('published' => 'N'));
1846 $this->assertEqual($result, $expected);
1847
1848 $FeaturedModel =& new Featured();
1849 $data = array(
1850 'article_featured_id' => 1,
1851 'category_id' => 1,
1852 'published_date' => array(
1853 'year' => 2008,
1854 'month' => 06,
1855 'day' => 11
1856 ),
1857 'end_date' => array(
1858 'year' => 2008,
1859 'month' => 06,
1860 'day' => 20
1861 ));
1862
1863 $expected = array(
1864 'Featured' => array(
1865 'article_featured_id' => 1,
1866 'category_id' => 1,
1867 'published_date' => '2008-6-11 00:00:00',
1868 'end_date' => '2008-6-20 00:00:00'
1869 ));
1870
1871 $this->assertEqual($FeaturedModel->create($data), $expected);
1872
1873 $data = array(
1874 'published_date' => array(
1875 'year' => 2008,
1876 'month' => 06,
1877 'day' => 11
1878 ),
1879 'end_date' => array(
1880 'year' => 2008,
1881 'month' => 06,
1882 'day' => 20
1883 ),
1884 'article_featured_id' => 1,
1885 'category_id' => 1
1886 );
1887
1888 $expected = array(
1889 'Featured' => array(
1890 'published_date' => '2008-6-11 00:00:00',
1891 'end_date' => '2008-6-20 00:00:00',
1892 'article_featured_id' => 1,
1893 'category_id' => 1
1894 ));
1895
1896 $this->assertEqual($FeaturedModel->create($data), $expected);
1897 }
1898
1899 /**
1900 * testEscapeField to prove it escapes the field well even when it has part of the alias on it
1901 * @see ttp://cakephp.lighthouseapp.com/projects/42648-cakephp-1x/tickets/473-escapefield-doesnt-consistently-prepend-modelname
1902 *
1903 * @access public
1904 * @return void
1905 */
1906 function testEscapeField() {
1907 $TestModel =& new Test();
1908 $db =& $TestModel->getDataSource();
1909
1910 $result = $TestModel->escapeField('test_field');
1911 $expected = $db->name('Test.test_field');
1912 $this->assertEqual($result, $expected);
1913
1914 $result = $TestModel->escapeField('TestField');
1915 $expected = $db->name('Test.TestField');
1916 $this->assertEqual($result, $expected);
1917
1918 $result = $TestModel->escapeField('DomainHandle', 'Domain');
1919 $expected = $db->name('Domain.DomainHandle');
1920 $this->assertEqual($result, $expected);
1921
1922 ConnectionManager::create('mock', array('driver' => 'mock'));
1923 $TestModel->setDataSource('mock');
1924 $db =& $TestModel->getDataSource();
1925
1926 $result = $TestModel->escapeField('DomainHandle', 'Domain');
1927 $expected = $db->name('Domain.DomainHandle');
1928 $this->assertEqual($result, $expected);
1929 }
1930 }