1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ModelIntegrationTest file |
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.4206 |
16
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
require_once dirname(__FILE__) . DS . 'ModelTestBase.php'; |
20
|
|
|
|
21
|
|
|
App::uses('DboSource', 'Model/Datasource'); |
22
|
|
|
App::uses('DboMock', 'Model/Datasource'); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* DboMock class |
26
|
|
|
* A Dbo Source driver to mock a connection and a identity name() method |
27
|
|
|
*/ |
28
|
|
|
class DboMock extends DboSource { |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Returns the $field without modifications |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
public function name($field) { |
36
|
|
|
return $field; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns true to fake a database connection |
41
|
|
|
* |
42
|
|
|
* @return boolean true |
43
|
|
|
*/ |
44
|
|
|
public function connect() { |
45
|
|
|
return true; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* ModelIntegrationTest |
52
|
|
|
* |
53
|
|
|
* @package Cake.Test.Case.Model |
54
|
|
|
*/ |
55
|
|
|
class ModelIntegrationTest extends BaseModelTest { |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* testAssociationLazyLoading |
59
|
|
|
* |
60
|
|
|
* @group lazyloading |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
public function testAssociationLazyLoading() { |
64
|
|
|
$this->loadFixtures('ArticleFeaturedsTags'); |
65
|
|
|
$Article = new ArticleFeatured(); |
66
|
|
|
$this->assertTrue(isset($Article->belongsTo['User'])); |
67
|
|
|
$this->assertFalse(property_exists($Article, 'User')); |
68
|
|
|
$this->assertInstanceOf('User', $Article->User); |
69
|
|
|
|
70
|
|
|
$this->assertTrue(isset($Article->belongsTo['Category'])); |
71
|
|
|
$this->assertFalse(property_exists($Article, 'Category')); |
72
|
|
|
$this->assertTrue(isset($Article->Category)); |
73
|
|
|
$this->assertInstanceOf('Category', $Article->Category); |
74
|
|
|
|
75
|
|
|
$this->assertTrue(isset($Article->hasMany['Comment'])); |
76
|
|
|
$this->assertFalse(property_exists($Article, 'Comment')); |
77
|
|
|
$this->assertTrue(isset($Article->Comment)); |
78
|
|
|
$this->assertInstanceOf('Comment', $Article->Comment); |
79
|
|
|
|
80
|
|
|
$this->assertTrue(isset($Article->hasAndBelongsToMany['Tag'])); |
81
|
|
|
//There was not enough information to setup the association (joinTable and associationForeignKey) |
82
|
|
|
//so the model was not lazy loaded |
83
|
|
|
$this->assertTrue(property_exists($Article, 'Tag')); |
84
|
|
|
$this->assertTrue(isset($Article->Tag)); |
85
|
|
|
$this->assertInstanceOf('Tag', $Article->Tag); |
86
|
|
|
|
87
|
|
|
$this->assertFalse(property_exists($Article, 'ArticleFeaturedsTag')); |
88
|
|
|
$this->assertInstanceOf('AppModel', $Article->ArticleFeaturedsTag); |
89
|
|
|
$this->assertEquals('article_featureds_tags', $Article->hasAndBelongsToMany['Tag']['joinTable']); |
90
|
|
|
$this->assertEquals('tag_id', $Article->hasAndBelongsToMany['Tag']['associationForeignKey']); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* testAssociationLazyLoadWithHABTM |
95
|
|
|
* |
96
|
|
|
* @group lazyloading |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
public function testAssociationLazyLoadWithHABTM() { |
100
|
|
|
$this->loadFixtures('FruitsUuidTag', 'ArticlesTag'); |
101
|
|
|
$this->db->cacheSources = false; |
102
|
|
|
$Article = new ArticleB(); |
103
|
|
|
$this->assertTrue(isset($Article->hasAndBelongsToMany['TagB'])); |
104
|
|
|
$this->assertFalse(property_exists($Article, 'TagB')); |
105
|
|
|
$this->assertInstanceOf('TagB', $Article->TagB); |
106
|
|
|
|
107
|
|
|
$this->assertFalse(property_exists($Article, 'ArticlesTag')); |
108
|
|
|
$this->assertInstanceOf('AppModel', $Article->ArticlesTag); |
109
|
|
|
|
110
|
|
|
$UuidTag = new UuidTag(); |
111
|
|
|
$this->assertTrue(isset($UuidTag->hasAndBelongsToMany['Fruit'])); |
112
|
|
|
$this->assertFalse(property_exists($UuidTag, 'Fruit')); |
113
|
|
|
$this->assertFalse(property_exists($UuidTag, 'FruitsUuidTag')); |
114
|
|
|
$this->assertTrue(isset($UuidTag->Fruit)); |
115
|
|
|
|
116
|
|
|
$this->assertFalse(property_exists($UuidTag, 'FruitsUuidTag')); |
117
|
|
|
$this->assertTrue(isset($UuidTag->FruitsUuidTag)); |
118
|
|
|
$this->assertInstanceOf('FruitsUuidTag', $UuidTag->FruitsUuidTag); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* testAssociationLazyLoadWithBindModel |
123
|
|
|
* |
124
|
|
|
* @group lazyloading |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
public function testAssociationLazyLoadWithBindModel() { |
128
|
|
|
$this->loadFixtures('Article', 'User'); |
129
|
|
|
$Article = new ArticleB(); |
130
|
|
|
|
131
|
|
|
$this->assertFalse(isset($Article->belongsTo['User'])); |
132
|
|
|
$this->assertFalse(property_exists($Article, 'User')); |
133
|
|
|
|
134
|
|
|
$Article->bindModel(array('belongsTo' => array('User'))); |
135
|
|
|
$this->assertTrue(isset($Article->belongsTo['User'])); |
136
|
|
|
$this->assertFalse(property_exists($Article, 'User')); |
137
|
|
|
$this->assertInstanceOf('User', $Article->User); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Tests that creating a model with no existent database table associated will throw an exception |
142
|
|
|
* |
143
|
|
|
* @expectedException MissingTableException |
144
|
|
|
* @return void |
145
|
|
|
*/ |
146
|
|
|
public function testMissingTable() { |
147
|
|
|
$Article = new ArticleB(false, uniqid()); |
148
|
|
|
$Article->schema(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* testPkInHAbtmLinkModelArticleB |
153
|
|
|
* |
154
|
|
|
* @return void |
155
|
|
|
*/ |
156
|
|
|
public function testPkInHabtmLinkModelArticleB() { |
157
|
|
|
$this->loadFixtures('Article', 'Tag', 'ArticlesTag'); |
158
|
|
|
$TestModel = new ArticleB(); |
159
|
|
|
$this->assertEquals('article_id', $TestModel->ArticlesTag->primaryKey); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Tests that $cacheSources is restored despite the settings on the model. |
164
|
|
|
* |
165
|
|
|
* @return void |
166
|
|
|
*/ |
167
|
|
|
public function testCacheSourcesRestored() { |
168
|
|
|
$this->loadFixtures('JoinA', 'JoinB', 'JoinAB', 'JoinC', 'JoinAC'); |
169
|
|
|
$this->db->cacheSources = true; |
170
|
|
|
$TestModel = new JoinA(); |
171
|
|
|
$TestModel->cacheSources = false; |
172
|
|
|
$TestModel->setSource('join_as'); |
173
|
|
|
$this->assertTrue($this->db->cacheSources); |
174
|
|
|
|
175
|
|
|
$this->db->cacheSources = false; |
176
|
|
|
$TestModel = new JoinA(); |
177
|
|
|
$TestModel->cacheSources = true; |
178
|
|
|
$TestModel->setSource('join_as'); |
179
|
|
|
$this->assertFalse($this->db->cacheSources); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* testPkInHabtmLinkModel method |
184
|
|
|
* |
185
|
|
|
* @return void |
186
|
|
|
*/ |
187
|
|
|
public function testPkInHabtmLinkModel() { |
188
|
|
|
//Test Nonconformant Models |
189
|
|
|
$this->loadFixtures('Content', 'ContentAccount', 'Account', 'JoinC', 'JoinAC', 'ItemsPortfolio'); |
190
|
|
|
$TestModel = new Content(); |
191
|
|
|
$this->assertEquals('iContentAccountsId', $TestModel->ContentAccount->primaryKey); |
192
|
|
|
|
193
|
|
|
//test conformant models with no PK in the join table |
194
|
|
|
$this->loadFixtures('Article', 'Tag'); |
195
|
|
|
$TestModel = new Article(); |
196
|
|
|
$this->assertEquals('article_id', $TestModel->ArticlesTag->primaryKey); |
197
|
|
|
|
198
|
|
|
//test conformant models with PK in join table |
199
|
|
|
$TestModel = new Portfolio(); |
200
|
|
|
$this->assertEquals('id', $TestModel->ItemsPortfolio->primaryKey); |
201
|
|
|
|
202
|
|
|
//test conformant models with PK in join table - join table contains extra field |
203
|
|
|
$this->loadFixtures('JoinA', 'JoinB', 'JoinAB'); |
204
|
|
|
$TestModel = new JoinA(); |
205
|
|
|
$this->assertEquals('id', $TestModel->JoinAsJoinB->primaryKey); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* testDynamicBehaviorAttachment method |
210
|
|
|
* |
211
|
|
|
* @return void |
212
|
|
|
*/ |
213
|
|
|
public function testDynamicBehaviorAttachment() { |
214
|
|
|
$this->loadFixtures('Apple', 'Sample', 'Author'); |
215
|
|
|
$TestModel = new Apple(); |
216
|
|
|
$this->assertEquals(array(), $TestModel->Behaviors->loaded()); |
217
|
|
|
|
218
|
|
|
$TestModel->Behaviors->load('Tree', array('left' => 'left_field', 'right' => 'right_field')); |
219
|
|
|
$this->assertTrue(is_object($TestModel->Behaviors->Tree)); |
220
|
|
|
$this->assertEquals(array('Tree'), $TestModel->Behaviors->loaded()); |
221
|
|
|
|
222
|
|
|
$expected = array( |
223
|
|
|
'parent' => 'parent_id', |
224
|
|
|
'left' => 'left_field', |
225
|
|
|
'right' => 'right_field', |
226
|
|
|
'scope' => '1 = 1', |
227
|
|
|
'type' => 'nested', |
228
|
|
|
'__parentChange' => false, |
229
|
|
|
'recursive' => -1 |
230
|
|
|
); |
231
|
|
|
$this->assertEquals($expected, $TestModel->Behaviors->Tree->settings['Apple']); |
232
|
|
|
|
233
|
|
|
$TestModel->Behaviors->load('Tree', array('enabled' => false)); |
234
|
|
|
$this->assertEquals($expected, $TestModel->Behaviors->Tree->settings['Apple']); |
235
|
|
|
$this->assertEquals(array('Tree'), $TestModel->Behaviors->loaded()); |
236
|
|
|
|
237
|
|
|
$TestModel->Behaviors->unload('Tree'); |
238
|
|
|
$this->assertEquals(array(), $TestModel->Behaviors->loaded()); |
239
|
|
|
$this->assertFalse(isset($TestModel->Behaviors->Tree)); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* testFindWithJoinsOption method |
244
|
|
|
* |
245
|
|
|
* @return void |
246
|
|
|
*/ |
247
|
|
|
public function testFindWithJoinsOption() { |
248
|
|
|
$this->loadFixtures('Article', 'User'); |
249
|
|
|
$TestUser = new User(); |
250
|
|
|
|
251
|
|
|
$options = array( |
252
|
|
|
'fields' => array( |
253
|
|
|
'user', |
254
|
|
|
'Article.published', |
255
|
|
|
), |
256
|
|
|
'joins' => array( |
257
|
|
|
array( |
258
|
|
|
'table' => 'articles', |
259
|
|
|
'alias' => 'Article', |
260
|
|
|
'type' => 'LEFT', |
261
|
|
|
'conditions' => array( |
262
|
|
|
'User.id = Article.user_id', |
263
|
|
|
), |
264
|
|
|
), |
265
|
|
|
), |
266
|
|
|
'group' => array('User.user', 'Article.published'), |
267
|
|
|
'recursive' => -1, |
268
|
|
|
'order' => array('User.user') |
269
|
|
|
); |
270
|
|
|
$result = $TestUser->find('all', $options); |
271
|
|
|
$expected = array( |
272
|
|
|
array('User' => array('user' => 'garrett'), 'Article' => array('published' => '')), |
273
|
|
|
array('User' => array('user' => 'larry'), 'Article' => array('published' => 'Y')), |
274
|
|
|
array('User' => array('user' => 'mariano'), 'Article' => array('published' => 'Y')), |
275
|
|
|
array('User' => array('user' => 'nate'), 'Article' => array('published' => '')) |
276
|
|
|
); |
277
|
|
|
$this->assertEquals($expected, $result); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Tests cross database joins. Requires $test and $test2 to both be set in DATABASE_CONFIG |
282
|
|
|
* NOTE: When testing on MySQL, you must set 'persistent' => false on *both* database connections, |
283
|
|
|
* or one connection will step on the other. |
284
|
|
|
* |
285
|
|
|
* @return void |
286
|
|
|
*/ |
287
|
|
|
public function testCrossDatabaseJoins() { |
288
|
|
|
$config = ConnectionManager::enumConnectionObjects(); |
289
|
|
|
|
290
|
|
|
$skip = (!isset($config['test']) || !isset($config['test2'])); |
291
|
|
|
if ($skip) { |
292
|
|
|
$this->markTestSkipped('Primary and secondary test databases not configured, skipping cross-database |
293
|
|
|
join tests. To run theses tests defined $test and $test2 in your database configuration.' |
294
|
|
|
); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
$this->loadFixtures('Article', 'Tag', 'ArticlesTag', 'User', 'Comment'); |
298
|
|
|
$TestModel = new Article(); |
299
|
|
|
|
300
|
|
|
$expected = array( |
301
|
|
|
array( |
302
|
|
|
'Article' => array( |
303
|
|
|
'id' => '1', |
304
|
|
|
'user_id' => '1', |
305
|
|
|
'title' => 'First Article', |
306
|
|
|
'body' => 'First Article Body', |
307
|
|
|
'published' => 'Y', |
308
|
|
|
'created' => '2007-03-18 10:39:23', |
309
|
|
|
'updated' => '2007-03-18 10:41:31' |
310
|
|
|
), |
311
|
|
|
'User' => array( |
312
|
|
|
'id' => '1', |
313
|
|
|
'user' => 'mariano', |
314
|
|
|
'password' => '5f4dcc3b5aa765d61d8327deb882cf99', |
315
|
|
|
'created' => '2007-03-17 01:16:23', |
316
|
|
|
'updated' => '2007-03-17 01:18:31' |
317
|
|
|
), |
318
|
|
|
'Comment' => array( |
319
|
|
|
array( |
320
|
|
|
'id' => '1', |
321
|
|
|
'article_id' => '1', |
322
|
|
|
'user_id' => '2', |
323
|
|
|
'comment' => 'First Comment for First Article', |
324
|
|
|
'published' => 'Y', |
325
|
|
|
'created' => '2007-03-18 10:45:23', |
326
|
|
|
'updated' => '2007-03-18 10:47:31' |
327
|
|
|
), |
328
|
|
|
array( |
329
|
|
|
'id' => '2', |
330
|
|
|
'article_id' => '1', |
331
|
|
|
'user_id' => '4', |
332
|
|
|
'comment' => 'Second Comment for First Article', |
333
|
|
|
'published' => 'Y', |
334
|
|
|
'created' => '2007-03-18 10:47:23', |
335
|
|
|
'updated' => '2007-03-18 10:49:31' |
336
|
|
|
), |
337
|
|
|
array( |
338
|
|
|
'id' => '3', |
339
|
|
|
'article_id' => '1', |
340
|
|
|
'user_id' => '1', |
341
|
|
|
'comment' => 'Third Comment for First Article', |
342
|
|
|
'published' => 'Y', |
343
|
|
|
'created' => '2007-03-18 10:49:23', |
344
|
|
|
'updated' => '2007-03-18 10:51:31' |
345
|
|
|
), |
346
|
|
|
array( |
347
|
|
|
'id' => '4', |
348
|
|
|
'article_id' => '1', |
349
|
|
|
'user_id' => '1', |
350
|
|
|
'comment' => 'Fourth Comment for First Article', |
351
|
|
|
'published' => 'N', |
352
|
|
|
'created' => '2007-03-18 10:51:23', |
353
|
|
|
'updated' => '2007-03-18 10:53:31' |
354
|
|
|
)), |
355
|
|
|
'Tag' => array( |
356
|
|
|
array( |
357
|
|
|
'id' => '1', |
358
|
|
|
'tag' => 'tag1', |
359
|
|
|
'created' => '2007-03-18 12:22:23', |
360
|
|
|
'updated' => '2007-03-18 12:24:31' |
361
|
|
|
), |
362
|
|
|
array( |
363
|
|
|
'id' => '2', |
364
|
|
|
'tag' => 'tag2', |
365
|
|
|
'created' => '2007-03-18 12:24:23', |
366
|
|
|
'updated' => '2007-03-18 12:26:31' |
367
|
|
|
))), |
368
|
|
|
array( |
369
|
|
|
'Article' => array( |
370
|
|
|
'id' => '2', |
371
|
|
|
'user_id' => '3', |
372
|
|
|
'title' => 'Second Article', |
373
|
|
|
'body' => 'Second Article Body', |
374
|
|
|
'published' => 'Y', |
375
|
|
|
'created' => '2007-03-18 10:41:23', |
376
|
|
|
'updated' => '2007-03-18 10:43:31' |
377
|
|
|
), |
378
|
|
|
'User' => array( |
379
|
|
|
'id' => '3', |
380
|
|
|
'user' => 'larry', |
381
|
|
|
'password' => '5f4dcc3b5aa765d61d8327deb882cf99', |
382
|
|
|
'created' => '2007-03-17 01:20:23', |
383
|
|
|
'updated' => '2007-03-17 01:22:31' |
384
|
|
|
), |
385
|
|
|
'Comment' => array( |
386
|
|
|
array( |
387
|
|
|
'id' => '5', |
388
|
|
|
'article_id' => '2', |
389
|
|
|
'user_id' => '1', |
390
|
|
|
'comment' => 'First Comment for Second Article', |
391
|
|
|
'published' => 'Y', |
392
|
|
|
'created' => '2007-03-18 10:53:23', |
393
|
|
|
'updated' => '2007-03-18 10:55:31' |
394
|
|
|
), |
395
|
|
|
array( |
396
|
|
|
'id' => '6', |
397
|
|
|
'article_id' => '2', |
398
|
|
|
'user_id' => '2', |
399
|
|
|
'comment' => 'Second Comment for Second Article', |
400
|
|
|
'published' => 'Y', |
401
|
|
|
'created' => '2007-03-18 10:55:23', |
402
|
|
|
'updated' => '2007-03-18 10:57:31' |
403
|
|
|
)), |
404
|
|
|
'Tag' => array( |
405
|
|
|
array( |
406
|
|
|
'id' => '1', |
407
|
|
|
'tag' => 'tag1', |
408
|
|
|
'created' => '2007-03-18 12:22:23', |
409
|
|
|
'updated' => '2007-03-18 12:24:31' |
410
|
|
|
), |
411
|
|
|
array( |
412
|
|
|
'id' => '3', |
413
|
|
|
'tag' => 'tag3', |
414
|
|
|
'created' => '2007-03-18 12:26:23', |
415
|
|
|
'updated' => '2007-03-18 12:28:31' |
416
|
|
|
))), |
417
|
|
|
array( |
418
|
|
|
'Article' => array( |
419
|
|
|
'id' => '3', |
420
|
|
|
'user_id' => '1', |
421
|
|
|
'title' => 'Third Article', |
422
|
|
|
'body' => 'Third Article Body', |
423
|
|
|
'published' => 'Y', |
424
|
|
|
'created' => '2007-03-18 10:43:23', |
425
|
|
|
'updated' => '2007-03-18 10:45:31' |
426
|
|
|
), |
427
|
|
|
'User' => array( |
428
|
|
|
'id' => '1', |
429
|
|
|
'user' => 'mariano', |
430
|
|
|
'password' => '5f4dcc3b5aa765d61d8327deb882cf99', |
431
|
|
|
'created' => '2007-03-17 01:16:23', |
432
|
|
|
'updated' => '2007-03-17 01:18:31' |
433
|
|
|
), |
434
|
|
|
'Comment' => array(), |
435
|
|
|
'Tag' => array() |
436
|
|
|
)); |
437
|
|
|
$this->assertEquals($expected, $TestModel->find('all')); |
438
|
|
|
|
439
|
|
|
$db2 = ConnectionManager::getDataSource('test2'); |
440
|
|
|
$this->fixtureManager->loadSingle('User', $db2); |
441
|
|
|
$this->fixtureManager->loadSingle('Comment', $db2); |
442
|
|
|
$this->assertEquals(3, $TestModel->find('count')); |
443
|
|
|
|
444
|
|
|
$TestModel->User->setDataSource('test2'); |
445
|
|
|
$TestModel->Comment->setDataSource('test2'); |
446
|
|
|
|
447
|
|
View Code Duplication |
foreach ($expected as $key => $value) { |
448
|
|
|
unset($value['Comment'], $value['Tag']); |
449
|
|
|
$expected[$key] = $value; |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
$TestModel->recursive = 0; |
453
|
|
|
$result = $TestModel->find('all'); |
454
|
|
|
$this->assertEquals($expected, $result); |
455
|
|
|
|
456
|
|
View Code Duplication |
foreach ($expected as $key => $value) { |
457
|
|
|
unset($value['Comment'], $value['Tag']); |
458
|
|
|
$expected[$key] = $value; |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
$TestModel->recursive = 0; |
462
|
|
|
$result = $TestModel->find('all'); |
463
|
|
|
$this->assertEquals($expected, $result); |
464
|
|
|
|
465
|
|
|
$result = Hash::extract($TestModel->User->find('all'), '{n}.User.id'); |
466
|
|
|
$this->assertEquals(array('1', '2', '3', '4'), $result); |
467
|
|
|
$this->assertEquals($expected, $TestModel->find('all')); |
468
|
|
|
|
469
|
|
|
$TestModel->Comment->unbindModel(array('hasOne' => array('Attachment'))); |
470
|
|
|
$expected = array( |
471
|
|
|
array( |
472
|
|
|
'Comment' => array( |
473
|
|
|
'id' => '1', |
474
|
|
|
'article_id' => '1', |
475
|
|
|
'user_id' => '2', |
476
|
|
|
'comment' => 'First Comment for First Article', |
477
|
|
|
'published' => 'Y', |
478
|
|
|
'created' => '2007-03-18 10:45:23', |
479
|
|
|
'updated' => '2007-03-18 10:47:31' |
480
|
|
|
), |
481
|
|
|
'User' => array( |
482
|
|
|
'id' => '2', |
483
|
|
|
'user' => 'nate', |
484
|
|
|
'password' => '5f4dcc3b5aa765d61d8327deb882cf99', |
485
|
|
|
'created' => '2007-03-17 01:18:23', |
486
|
|
|
'updated' => '2007-03-17 01:20:31' |
487
|
|
|
), |
488
|
|
|
'Article' => array( |
489
|
|
|
'id' => '1', |
490
|
|
|
'user_id' => '1', |
491
|
|
|
'title' => 'First Article', |
492
|
|
|
'body' => 'First Article Body', |
493
|
|
|
'published' => 'Y', |
494
|
|
|
'created' => '2007-03-18 10:39:23', |
495
|
|
|
'updated' => '2007-03-18 10:41:31' |
496
|
|
|
)), |
497
|
|
|
array( |
498
|
|
|
'Comment' => array( |
499
|
|
|
'id' => '2', |
500
|
|
|
'article_id' => '1', |
501
|
|
|
'user_id' => '4', |
502
|
|
|
'comment' => 'Second Comment for First Article', |
503
|
|
|
'published' => 'Y', |
504
|
|
|
'created' => '2007-03-18 10:47:23', |
505
|
|
|
'updated' => '2007-03-18 10:49:31' |
506
|
|
|
), |
507
|
|
|
'User' => array( |
508
|
|
|
'id' => '4', |
509
|
|
|
'user' => 'garrett', |
510
|
|
|
'password' => '5f4dcc3b5aa765d61d8327deb882cf99', |
511
|
|
|
'created' => '2007-03-17 01:22:23', |
512
|
|
|
'updated' => '2007-03-17 01:24:31' |
513
|
|
|
), |
514
|
|
|
'Article' => array( |
515
|
|
|
'id' => '1', |
516
|
|
|
'user_id' => '1', |
517
|
|
|
'title' => 'First Article', |
518
|
|
|
'body' => 'First Article Body', |
519
|
|
|
'published' => 'Y', |
520
|
|
|
'created' => '2007-03-18 10:39:23', |
521
|
|
|
'updated' => '2007-03-18 10:41:31' |
522
|
|
|
)), |
523
|
|
|
array( |
524
|
|
|
'Comment' => array( |
525
|
|
|
'id' => '3', |
526
|
|
|
'article_id' => '1', |
527
|
|
|
'user_id' => '1', |
528
|
|
|
'comment' => 'Third Comment for First Article', |
529
|
|
|
'published' => 'Y', |
530
|
|
|
'created' => '2007-03-18 10:49:23', |
531
|
|
|
'updated' => '2007-03-18 10:51:31' |
532
|
|
|
), |
533
|
|
|
'User' => array( |
534
|
|
|
'id' => '1', |
535
|
|
|
'user' => 'mariano', |
536
|
|
|
'password' => '5f4dcc3b5aa765d61d8327deb882cf99', |
537
|
|
|
'created' => '2007-03-17 01:16:23', |
538
|
|
|
'updated' => '2007-03-17 01:18:31' |
539
|
|
|
), |
540
|
|
|
'Article' => array( |
541
|
|
|
'id' => '1', |
542
|
|
|
'user_id' => '1', |
543
|
|
|
'title' => 'First Article', |
544
|
|
|
'body' => 'First Article Body', |
545
|
|
|
'published' => 'Y', |
546
|
|
|
'created' => '2007-03-18 10:39:23', |
547
|
|
|
'updated' => '2007-03-18 10:41:31' |
548
|
|
|
)), |
549
|
|
|
array( |
550
|
|
|
'Comment' => array( |
551
|
|
|
'id' => '4', |
552
|
|
|
'article_id' => '1', |
553
|
|
|
'user_id' => '1', |
554
|
|
|
'comment' => 'Fourth Comment for First Article', |
555
|
|
|
'published' => 'N', |
556
|
|
|
'created' => '2007-03-18 10:51:23', |
557
|
|
|
'updated' => '2007-03-18 10:53:31' |
558
|
|
|
), |
559
|
|
|
'User' => array( |
560
|
|
|
'id' => '1', |
561
|
|
|
'user' => 'mariano', |
562
|
|
|
'password' => '5f4dcc3b5aa765d61d8327deb882cf99', |
563
|
|
|
'created' => '2007-03-17 01:16:23', |
564
|
|
|
'updated' => '2007-03-17 01:18:31' |
565
|
|
|
), |
566
|
|
|
'Article' => array( |
567
|
|
|
'id' => '1', |
568
|
|
|
'user_id' => '1', |
569
|
|
|
'title' => 'First Article', |
570
|
|
|
'body' => 'First Article Body', |
571
|
|
|
'published' => 'Y', |
572
|
|
|
'created' => '2007-03-18 10:39:23', |
573
|
|
|
'updated' => '2007-03-18 10:41:31' |
574
|
|
|
)), |
575
|
|
|
array( |
576
|
|
|
'Comment' => array( |
577
|
|
|
'id' => '5', |
578
|
|
|
'article_id' => '2', |
579
|
|
|
'user_id' => '1', |
580
|
|
|
'comment' => 'First Comment for Second Article', |
581
|
|
|
'published' => 'Y', |
582
|
|
|
'created' => '2007-03-18 10:53:23', |
583
|
|
|
'updated' => '2007-03-18 10:55:31' |
584
|
|
|
), |
585
|
|
|
'User' => array( |
586
|
|
|
'id' => '1', |
587
|
|
|
'user' => 'mariano', |
588
|
|
|
'password' => '5f4dcc3b5aa765d61d8327deb882cf99', |
589
|
|
|
'created' => '2007-03-17 01:16:23', |
590
|
|
|
'updated' => '2007-03-17 01:18:31' |
591
|
|
|
), |
592
|
|
|
'Article' => array( |
593
|
|
|
'id' => '2', |
594
|
|
|
'user_id' => '3', |
595
|
|
|
'title' => 'Second Article', |
596
|
|
|
'body' => 'Second Article Body', |
597
|
|
|
'published' => 'Y', |
598
|
|
|
'created' => '2007-03-18 10:41:23', |
599
|
|
|
'updated' => '2007-03-18 10:43:31' |
600
|
|
|
)), |
601
|
|
|
array( |
602
|
|
|
'Comment' => array( |
603
|
|
|
'id' => '6', |
604
|
|
|
'article_id' => '2', |
605
|
|
|
'user_id' => '2', |
606
|
|
|
'comment' => 'Second Comment for Second Article', |
607
|
|
|
'published' => 'Y', |
608
|
|
|
'created' => '2007-03-18 10:55:23', |
609
|
|
|
'updated' => '2007-03-18 10:57:31' |
610
|
|
|
), |
611
|
|
|
'User' => array( |
612
|
|
|
'id' => '2', |
613
|
|
|
'user' => 'nate', |
614
|
|
|
'password' => '5f4dcc3b5aa765d61d8327deb882cf99', |
615
|
|
|
'created' => '2007-03-17 01:18:23', |
616
|
|
|
'updated' => '2007-03-17 01:20:31' |
617
|
|
|
), |
618
|
|
|
'Article' => array( |
619
|
|
|
'id' => '2', |
620
|
|
|
'user_id' => '3', |
621
|
|
|
'title' => 'Second Article', |
622
|
|
|
'body' => 'Second Article Body', |
623
|
|
|
'published' => 'Y', |
624
|
|
|
'created' => '2007-03-18 10:41:23', |
625
|
|
|
'updated' => '2007-03-18 10:43:31' |
626
|
|
|
))); |
627
|
|
|
$this->assertEquals($expected, $TestModel->Comment->find('all')); |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
/** |
631
|
|
|
* test HABM operations without clobbering existing records #275 |
632
|
|
|
* |
633
|
|
|
* @return void |
634
|
|
|
*/ |
635
|
|
|
public function testHABTMKeepExisting() { |
636
|
|
|
$this->loadFixtures('Site', 'Domain', 'DomainsSite'); |
637
|
|
|
|
638
|
|
|
$Site = new Site(); |
639
|
|
|
$results = $Site->find('count'); |
640
|
|
|
$expected = 3; |
641
|
|
|
$this->assertEquals($expected, $results); |
642
|
|
|
|
643
|
|
|
$data = $Site->findById(1); |
|
|
|
|
644
|
|
|
|
645
|
|
|
// include api.cakephp.org |
646
|
|
|
$data['Domain'] = array('Domain' => array(1, 2, 3)); |
647
|
|
|
$Site->save($data); |
648
|
|
|
|
649
|
|
|
$Site->id = 1; |
650
|
|
|
$results = $Site->read(); |
651
|
|
|
$expected = 3; // 3 domains belonging to cakephp |
652
|
|
|
$this->assertEquals($expected, count($results['Domain'])); |
653
|
|
|
|
654
|
|
|
$Site->id = 2; |
655
|
|
|
$results = $Site->read(); |
656
|
|
|
$expected = 2; // 2 domains belonging to markstory |
657
|
|
|
$this->assertEquals($expected, count($results['Domain'])); |
658
|
|
|
|
659
|
|
|
$Site->id = 3; |
660
|
|
|
$results = $Site->read(); |
661
|
|
|
$expected = 2; |
662
|
|
|
$this->assertEquals($expected, count($results['Domain'])); |
663
|
|
|
$results['Domain'] = array('Domain' => array(7)); |
664
|
|
|
$Site->save($results); // remove association from domain 6 |
|
|
|
|
665
|
|
|
$results = $Site->read(); |
666
|
|
|
$expected = 1; // only 1 domain left belonging to rchavik |
667
|
|
|
$this->assertEquals($expected, count($results['Domain'])); |
668
|
|
|
|
669
|
|
|
// add deleted domain back |
670
|
|
|
$results['Domain'] = array('Domain' => array(6, 7)); |
671
|
|
|
$Site->save($results); |
|
|
|
|
672
|
|
|
$results = $Site->read(); |
673
|
|
|
$expected = 2; // 2 domains belonging to rchavik |
674
|
|
|
$this->assertEquals($expected, count($results['Domain'])); |
675
|
|
|
|
676
|
|
|
$Site->DomainsSite->id = $results['Domain'][0]['DomainsSite']['id']; |
677
|
|
|
$Site->DomainsSite->saveField('active', true); |
678
|
|
|
|
679
|
|
|
$results = $Site->Domain->DomainsSite->find('count', array( |
680
|
|
|
'conditions' => array( |
681
|
|
|
'DomainsSite.active' => true, |
682
|
|
|
), |
683
|
|
|
)); |
684
|
|
|
$expected = 5; |
685
|
|
|
$this->assertEquals($expected, $results); |
686
|
|
|
|
687
|
|
|
// activate api.cakephp.org |
688
|
|
|
$activated = $Site->DomainsSite->findByDomainId(3); |
689
|
|
|
$activated['DomainsSite']['active'] = true; |
690
|
|
|
$Site->DomainsSite->save($activated); |
691
|
|
|
|
692
|
|
|
$results = $Site->DomainsSite->find('count', array( |
693
|
|
|
'conditions' => array( |
694
|
|
|
'DomainsSite.active' => true, |
695
|
|
|
), |
696
|
|
|
)); |
697
|
|
|
$expected = 6; |
698
|
|
|
$this->assertEquals($expected, $results); |
699
|
|
|
|
700
|
|
|
// remove 2 previously active domains, and leave $activated alone |
701
|
|
|
$data = array( |
702
|
|
|
'Site' => array('id' => 1, 'name' => 'cakephp (modified)'), |
703
|
|
|
'Domain' => array( |
704
|
|
|
'Domain' => array(3), |
705
|
|
|
) |
706
|
|
|
); |
707
|
|
|
$Site->create($data); |
708
|
|
|
$Site->save($data); |
709
|
|
|
|
710
|
|
|
// tests that record is still identical prior to removal |
711
|
|
|
$Site->id = 1; |
712
|
|
|
$results = $Site->read(); |
713
|
|
|
unset($results['Domain'][0]['DomainsSite']['updated']); |
714
|
|
|
unset($activated['DomainsSite']['updated']); |
715
|
|
|
$this->assertEquals($activated['DomainsSite'], $results['Domain'][0]['DomainsSite']); |
716
|
|
|
} |
717
|
|
|
|
718
|
|
|
/** |
719
|
|
|
* testHABTMKeepExistingAlternateDataFormat |
720
|
|
|
* |
721
|
|
|
* @return void |
722
|
|
|
*/ |
723
|
|
|
public function testHABTMKeepExistingAlternateDataFormat() { |
724
|
|
|
$this->loadFixtures('Site', 'Domain', 'DomainsSite'); |
725
|
|
|
|
726
|
|
|
$Site = new Site(); |
727
|
|
|
|
728
|
|
|
$expected = array( |
729
|
|
|
array( |
730
|
|
|
'DomainsSite' => array( |
731
|
|
|
'id' => 1, |
732
|
|
|
'site_id' => 1, |
733
|
|
|
'domain_id' => 1, |
734
|
|
|
'active' => true, |
735
|
|
|
'created' => '2007-03-17 01:16:23' |
736
|
|
|
) |
737
|
|
|
), |
738
|
|
|
array( |
739
|
|
|
'DomainsSite' => array( |
740
|
|
|
'id' => 2, |
741
|
|
|
'site_id' => 1, |
742
|
|
|
'domain_id' => 2, |
743
|
|
|
'active' => true, |
744
|
|
|
'created' => '2007-03-17 01:16:23' |
745
|
|
|
) |
746
|
|
|
) |
747
|
|
|
); |
748
|
|
|
$result = $Site->DomainsSite->find('all', array( |
749
|
|
|
'conditions' => array('DomainsSite.site_id' => 1), |
750
|
|
|
'fields' => array( |
751
|
|
|
'DomainsSite.id', |
752
|
|
|
'DomainsSite.site_id', |
753
|
|
|
'DomainsSite.domain_id', |
754
|
|
|
'DomainsSite.active', |
755
|
|
|
'DomainsSite.created' |
756
|
|
|
), |
757
|
|
|
'order' => 'DomainsSite.id' |
758
|
|
|
)); |
759
|
|
|
$this->assertEquals($expected, $result); |
760
|
|
|
|
761
|
|
|
$time = date('Y-m-d H:i:s'); |
762
|
|
|
$data = array( |
763
|
|
|
'Site' => array( |
764
|
|
|
'id' => 1 |
765
|
|
|
), |
766
|
|
|
'Domain' => array( |
767
|
|
|
array( |
768
|
|
|
'site_id' => 1, |
769
|
|
|
'domain_id' => 3, |
770
|
|
|
'created' => $time, |
771
|
|
|
), |
772
|
|
|
array( |
773
|
|
|
'id' => 2, |
774
|
|
|
'site_id' => 1, |
775
|
|
|
'domain_id' => 2 |
776
|
|
|
), |
777
|
|
|
) |
778
|
|
|
); |
779
|
|
|
$Site->save($data); |
780
|
|
|
$expected = array( |
781
|
|
|
array( |
782
|
|
|
'DomainsSite' => array( |
783
|
|
|
'id' => 2, |
784
|
|
|
'site_id' => 1, |
785
|
|
|
'domain_id' => 2, |
786
|
|
|
'active' => true, |
787
|
|
|
'created' => '2007-03-17 01:16:23' |
788
|
|
|
) |
789
|
|
|
), |
790
|
|
|
array( |
791
|
|
|
'DomainsSite' => array( |
792
|
|
|
'id' => 7, |
793
|
|
|
'site_id' => 1, |
794
|
|
|
'domain_id' => 3, |
795
|
|
|
'active' => false, |
796
|
|
|
'created' => $time |
797
|
|
|
) |
798
|
|
|
) |
799
|
|
|
); |
800
|
|
|
$result = $Site->DomainsSite->find('all', array( |
801
|
|
|
'conditions' => array('DomainsSite.site_id' => 1), |
802
|
|
|
'fields' => array( |
803
|
|
|
'DomainsSite.id', |
804
|
|
|
'DomainsSite.site_id', |
805
|
|
|
'DomainsSite.domain_id', |
806
|
|
|
'DomainsSite.active', |
807
|
|
|
'DomainsSite.created' |
808
|
|
|
), |
809
|
|
|
'order' => 'DomainsSite.id' |
810
|
|
|
)); |
811
|
|
|
$this->assertEquals($expected, $result); |
812
|
|
|
} |
813
|
|
|
|
814
|
|
|
/** |
815
|
|
|
* test HABM operations without clobbering existing records #275 |
816
|
|
|
* |
817
|
|
|
* @return void |
818
|
|
|
*/ |
819
|
|
|
public function testHABTMKeepExistingWithThreeDbs() { |
820
|
|
|
$config = ConnectionManager::enumConnectionObjects(); |
821
|
|
|
$this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with Sqlite.'); |
822
|
|
|
$this->skipIf( |
823
|
|
|
!isset($config['test']) || !isset($config['test2']) || !isset($config['test_database_three']), |
824
|
|
|
'Primary, secondary, and tertiary test databases not configured, skipping test. To run this test define $test, $test2, and $test_database_three in your database configuration.' |
825
|
|
|
); |
826
|
|
|
|
827
|
|
|
$this->loadFixtures('Player', 'Guild', 'GuildsPlayer', 'Armor', 'ArmorsPlayer'); |
828
|
|
|
$Player = ClassRegistry::init('Player'); |
829
|
|
|
$Player->bindModel(array( |
830
|
|
|
'hasAndBelongsToMany' => array( |
831
|
|
|
'Armor' => array( |
832
|
|
|
'with' => 'ArmorsPlayer', |
833
|
|
|
'unique' => 'keepExisting', |
834
|
|
|
), |
835
|
|
|
), |
836
|
|
|
), false); |
837
|
|
|
$this->assertEquals('test', $Player->useDbConfig); |
838
|
|
|
$this->assertEquals('test', $Player->Guild->useDbConfig); |
839
|
|
|
$this->assertEquals('test2', $Player->Guild->GuildsPlayer->useDbConfig); |
840
|
|
|
$this->assertEquals('test2', $Player->Armor->useDbConfig); |
841
|
|
|
$this->assertEquals('test_database_three', $Player->ArmorsPlayer->useDbConfig); |
842
|
|
|
|
843
|
|
|
$players = $Player->find('all'); |
844
|
|
|
$this->assertEquals(4, count($players)); |
845
|
|
|
$playersGuilds = Hash::extract($players, '{n}.Guild.{n}.GuildsPlayer'); |
846
|
|
|
$this->assertEquals(3, count($playersGuilds)); |
847
|
|
|
$playersArmors = Hash::extract($players, '{n}.Armor.{n}.ArmorsPlayer'); |
848
|
|
|
$this->assertEquals(3, count($playersArmors)); |
849
|
|
|
unset($players); |
850
|
|
|
|
851
|
|
|
$larry = $Player->findByName('larry'); |
852
|
|
|
$larrysArmor = Hash::extract($larry, 'Armor.{n}.ArmorsPlayer'); |
853
|
|
|
$this->assertEquals(1, count($larrysArmor)); |
854
|
|
|
|
855
|
|
|
$larry['Guild']['Guild'] = array(1, 3); // larry joins another guild |
856
|
|
|
$larry['Armor']['Armor'] = array(2, 3); // purchases chainmail |
857
|
|
|
$Player->save($larry); |
858
|
|
|
unset($larry); |
859
|
|
|
|
860
|
|
|
$larry = $Player->findByName('larry'); |
861
|
|
|
$larrysGuild = Hash::extract($larry, 'Guild.{n}.GuildsPlayer'); |
862
|
|
|
$this->assertEquals(2, count($larrysGuild)); |
863
|
|
|
$larrysArmor = Hash::extract($larry, 'Armor.{n}.ArmorsPlayer'); |
864
|
|
|
$this->assertEquals(2, count($larrysArmor)); |
865
|
|
|
|
866
|
|
|
$Player->ArmorsPlayer->id = 3; |
867
|
|
|
$Player->ArmorsPlayer->saveField('broken', true); // larry's cloak broke |
868
|
|
|
|
869
|
|
|
$larry = $Player->findByName('larry'); |
870
|
|
|
$larrysCloak = Hash::extract($larry, 'Armor.{n}.ArmorsPlayer[armor_id=3]', $larry); |
871
|
|
|
$this->assertNotEmpty($larrysCloak); |
872
|
|
|
$this->assertTrue($larrysCloak[0]['broken']); // still broken |
873
|
|
|
} |
874
|
|
|
|
875
|
|
|
/** |
876
|
|
|
* testDisplayField method |
877
|
|
|
* |
878
|
|
|
* @return void |
879
|
|
|
*/ |
880
|
|
|
public function testDisplayField() { |
881
|
|
|
$this->loadFixtures('Post', 'Comment', 'Person', 'User'); |
882
|
|
|
$Post = new Post(); |
883
|
|
|
$Comment = new Comment(); |
884
|
|
|
$Person = new Person(); |
885
|
|
|
|
886
|
|
|
$this->assertEquals('title', $Post->displayField); |
887
|
|
|
$this->assertEquals('name', $Person->displayField); |
888
|
|
|
$this->assertEquals('id', $Comment->displayField); |
889
|
|
|
} |
890
|
|
|
|
891
|
|
|
/** |
892
|
|
|
* testSchema method |
893
|
|
|
* |
894
|
|
|
* @return void |
895
|
|
|
*/ |
896
|
|
|
public function testSchema() { |
897
|
|
|
$Post = new Post(); |
898
|
|
|
|
899
|
|
|
$result = $Post->schema(); |
900
|
|
|
$columns = array('id', 'author_id', 'title', 'body', 'published', 'created', 'updated'); |
901
|
|
|
$this->assertEquals($columns, array_keys($result)); |
902
|
|
|
|
903
|
|
|
$types = array('integer', 'integer', 'string', 'text', 'string', 'datetime', 'datetime'); |
904
|
|
|
$this->assertEquals(Hash::extract(array_values($result), '{n}.type'), $types); |
905
|
|
|
|
906
|
|
|
$result = $Post->schema('body'); |
907
|
|
|
$this->assertEquals('text', $result['type']); |
908
|
|
|
$this->assertNull($Post->schema('foo')); |
909
|
|
|
|
910
|
|
|
$this->assertEquals($Post->getColumnTypes(), array_combine($columns, $types)); |
911
|
|
|
} |
912
|
|
|
|
913
|
|
|
/** |
914
|
|
|
* Check schema() on a model with useTable = false; |
915
|
|
|
* |
916
|
|
|
* @return void |
917
|
|
|
*/ |
918
|
|
|
public function testSchemaUseTableFalse() { |
919
|
|
|
$model = new TheVoid(); |
920
|
|
|
$result = $model->schema(); |
921
|
|
|
$this->assertNull($result); |
922
|
|
|
|
923
|
|
|
$result = $model->create(); |
924
|
|
|
$this->assertEmpty($result); |
925
|
|
|
} |
926
|
|
|
|
927
|
|
|
/** |
928
|
|
|
* data provider for time tests. |
929
|
|
|
* |
930
|
|
|
* @return array |
931
|
|
|
*/ |
932
|
|
|
public static function timeProvider() { |
933
|
|
|
$db = ConnectionManager::getDataSource('test'); |
934
|
|
|
$now = $db->expression('NOW()'); |
935
|
|
|
return array( |
936
|
|
|
// blank |
937
|
|
|
array( |
938
|
|
|
array('hour' => '', 'min' => '', 'meridian' => ''), |
939
|
|
|
'' |
940
|
|
|
), |
941
|
|
|
// missing hour |
942
|
|
|
array( |
943
|
|
|
array('hour' => '', 'min' => '00', 'meridian' => 'pm'), |
944
|
|
|
'' |
945
|
|
|
), |
946
|
|
|
// all blank |
947
|
|
|
array( |
948
|
|
|
array('hour' => '', 'min' => '', 'sec' => ''), |
949
|
|
|
'' |
950
|
|
|
), |
951
|
|
|
// set and empty merdian |
952
|
|
|
array( |
953
|
|
|
array('hour' => '1', 'min' => '00', 'meridian' => ''), |
954
|
|
|
'' |
955
|
|
|
), |
956
|
|
|
// midnight |
957
|
|
|
array( |
958
|
|
|
array('hour' => '12', 'min' => '0', 'meridian' => 'am'), |
959
|
|
|
'00:00:00' |
960
|
|
|
), |
961
|
|
|
array( |
962
|
|
|
array('hour' => '00', 'min' => '00'), |
963
|
|
|
'00:00:00' |
964
|
|
|
), |
965
|
|
|
// 3am |
966
|
|
|
array( |
967
|
|
|
array('hour' => '03', 'min' => '04', 'sec' => '04'), |
968
|
|
|
'03:04:04' |
969
|
|
|
), |
970
|
|
|
array( |
971
|
|
|
array('hour' => '3', 'min' => '4', 'sec' => '4'), |
972
|
|
|
'03:04:04' |
973
|
|
|
), |
974
|
|
|
array( |
975
|
|
|
array('hour' => '03', 'min' => '4', 'sec' => '4'), |
976
|
|
|
'03:04:04' |
977
|
|
|
), |
978
|
|
|
array( |
979
|
|
|
$now, |
980
|
|
|
$now |
981
|
|
|
) |
982
|
|
|
); |
983
|
|
|
} |
984
|
|
|
|
985
|
|
|
/** |
986
|
|
|
* test deconstruct with time fields. |
987
|
|
|
* |
988
|
|
|
* @dataProvider timeProvider |
989
|
|
|
* @return void |
990
|
|
|
*/ |
991
|
|
|
public function testDeconstructFieldsTime($input, $result) { |
992
|
|
|
$this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.'); |
993
|
|
|
|
994
|
|
|
$this->loadFixtures('Apple'); |
995
|
|
|
$TestModel = new Apple(); |
996
|
|
|
|
997
|
|
|
$data = array( |
998
|
|
|
'Apple' => array( |
999
|
|
|
'mytime' => $input |
1000
|
|
|
) |
1001
|
|
|
); |
1002
|
|
|
|
1003
|
|
|
$TestModel->data = null; |
|
|
|
|
1004
|
|
|
$TestModel->set($data); |
1005
|
|
|
$expected = array('Apple' => array('mytime' => $result)); |
1006
|
|
|
$this->assertEquals($expected, $TestModel->data); |
1007
|
|
|
} |
1008
|
|
|
|
1009
|
|
|
/** |
1010
|
|
|
* testDeconstructFields with datetime, timestamp, and date fields |
1011
|
|
|
* |
1012
|
|
|
* @return void |
1013
|
|
|
*/ |
1014
|
|
|
public function testDeconstructFieldsDateTime() { |
1015
|
|
|
$this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.'); |
1016
|
|
|
|
1017
|
|
|
$this->loadFixtures('Apple'); |
1018
|
|
|
$TestModel = new Apple(); |
1019
|
|
|
|
1020
|
|
|
//test null/empty values first |
1021
|
|
|
$data['Apple']['created']['year'] = ''; |
|
|
|
|
1022
|
|
|
$data['Apple']['created']['month'] = ''; |
1023
|
|
|
$data['Apple']['created']['day'] = ''; |
1024
|
|
|
$data['Apple']['created']['hour'] = ''; |
1025
|
|
|
$data['Apple']['created']['min'] = ''; |
1026
|
|
|
$data['Apple']['created']['sec'] = ''; |
1027
|
|
|
|
1028
|
|
|
$TestModel->data = null; |
|
|
|
|
1029
|
|
|
$TestModel->set($data); |
1030
|
|
|
$expected = array('Apple' => array('created' => '')); |
1031
|
|
|
$this->assertEquals($expected, $TestModel->data); |
1032
|
|
|
|
1033
|
|
|
$data = array(); |
1034
|
|
|
$data['Apple']['date']['year'] = ''; |
1035
|
|
|
$data['Apple']['date']['month'] = ''; |
1036
|
|
|
$data['Apple']['date']['day'] = ''; |
1037
|
|
|
|
1038
|
|
|
$TestModel->data = null; |
1039
|
|
|
$TestModel->set($data); |
1040
|
|
|
$expected = array('Apple' => array('date' => '')); |
1041
|
|
|
$this->assertEquals($expected, $TestModel->data); |
1042
|
|
|
|
1043
|
|
|
$data = array(); |
1044
|
|
|
$data['Apple']['created']['year'] = '2007'; |
1045
|
|
|
$data['Apple']['created']['month'] = '08'; |
1046
|
|
|
$data['Apple']['created']['day'] = '20'; |
1047
|
|
|
$data['Apple']['created']['hour'] = ''; |
1048
|
|
|
$data['Apple']['created']['min'] = ''; |
1049
|
|
|
$data['Apple']['created']['sec'] = ''; |
1050
|
|
|
|
1051
|
|
|
$TestModel->data = null; |
1052
|
|
|
$TestModel->set($data); |
1053
|
|
|
$expected = array('Apple' => array('created' => '2007-08-20 00:00:00')); |
1054
|
|
|
$this->assertEquals($expected, $TestModel->data); |
1055
|
|
|
|
1056
|
|
|
$data = array(); |
1057
|
|
|
$data['Apple']['created']['year'] = '2007'; |
1058
|
|
|
$data['Apple']['created']['month'] = '08'; |
1059
|
|
|
$data['Apple']['created']['day'] = '20'; |
1060
|
|
|
$data['Apple']['created']['hour'] = '10'; |
1061
|
|
|
$data['Apple']['created']['min'] = '12'; |
1062
|
|
|
$data['Apple']['created']['sec'] = ''; |
1063
|
|
|
|
1064
|
|
|
$TestModel->data = null; |
1065
|
|
|
$TestModel->set($data); |
1066
|
|
|
$expected = array('Apple' => array('created' => '2007-08-20 10:12:00')); |
1067
|
|
|
$this->assertEquals($expected, $TestModel->data); |
1068
|
|
|
|
1069
|
|
|
$data = array(); |
1070
|
|
|
$data['Apple']['created']['year'] = '2007'; |
1071
|
|
|
$data['Apple']['created']['month'] = ''; |
1072
|
|
|
$data['Apple']['created']['day'] = '12'; |
1073
|
|
|
$data['Apple']['created']['hour'] = '20'; |
1074
|
|
|
$data['Apple']['created']['min'] = ''; |
1075
|
|
|
$data['Apple']['created']['sec'] = ''; |
1076
|
|
|
|
1077
|
|
|
$TestModel->data = null; |
1078
|
|
|
$TestModel->set($data); |
1079
|
|
|
$expected = array('Apple' => array('created' => '')); |
1080
|
|
|
$this->assertEquals($expected, $TestModel->data); |
1081
|
|
|
|
1082
|
|
|
$data = array(); |
1083
|
|
|
$data['Apple']['created']['hour'] = '20'; |
1084
|
|
|
$data['Apple']['created']['min'] = '33'; |
1085
|
|
|
|
1086
|
|
|
$TestModel->data = null; |
1087
|
|
|
$TestModel->set($data); |
1088
|
|
|
$expected = array('Apple' => array('created' => '')); |
1089
|
|
|
$this->assertEquals($expected, $TestModel->data); |
1090
|
|
|
|
1091
|
|
|
$data = array(); |
1092
|
|
|
$data['Apple']['created']['hour'] = '20'; |
1093
|
|
|
$data['Apple']['created']['min'] = '33'; |
1094
|
|
|
$data['Apple']['created']['sec'] = '33'; |
1095
|
|
|
|
1096
|
|
|
$TestModel->data = null; |
1097
|
|
|
$TestModel->set($data); |
1098
|
|
|
$expected = array('Apple' => array('created' => '')); |
1099
|
|
|
$this->assertEquals($expected, $TestModel->data); |
1100
|
|
|
|
1101
|
|
|
$data = array(); |
1102
|
|
|
$data['Apple']['created']['hour'] = '13'; |
1103
|
|
|
$data['Apple']['created']['min'] = '00'; |
1104
|
|
|
$data['Apple']['date']['year'] = '2006'; |
1105
|
|
|
$data['Apple']['date']['month'] = '12'; |
1106
|
|
|
$data['Apple']['date']['day'] = '25'; |
1107
|
|
|
|
1108
|
|
|
$TestModel->data = null; |
1109
|
|
|
$TestModel->set($data); |
1110
|
|
|
$expected = array( |
1111
|
|
|
'Apple' => array( |
1112
|
|
|
'created' => '', |
1113
|
|
|
'date' => '2006-12-25' |
1114
|
|
|
)); |
1115
|
|
|
$this->assertEquals($expected, $TestModel->data); |
1116
|
|
|
|
1117
|
|
|
$data = array(); |
1118
|
|
|
$data['Apple']['created']['year'] = '2007'; |
1119
|
|
|
$data['Apple']['created']['month'] = '08'; |
1120
|
|
|
$data['Apple']['created']['day'] = '20'; |
1121
|
|
|
$data['Apple']['created']['hour'] = '10'; |
1122
|
|
|
$data['Apple']['created']['min'] = '12'; |
1123
|
|
|
$data['Apple']['created']['sec'] = '09'; |
1124
|
|
|
$data['Apple']['date']['year'] = '2006'; |
1125
|
|
|
$data['Apple']['date']['month'] = '12'; |
1126
|
|
|
$data['Apple']['date']['day'] = '25'; |
1127
|
|
|
|
1128
|
|
|
$TestModel->data = null; |
1129
|
|
|
$TestModel->set($data); |
1130
|
|
|
$expected = array( |
1131
|
|
|
'Apple' => array( |
1132
|
|
|
'created' => '2007-08-20 10:12:09', |
1133
|
|
|
'date' => '2006-12-25' |
1134
|
|
|
)); |
1135
|
|
|
$this->assertEquals($expected, $TestModel->data); |
1136
|
|
|
|
1137
|
|
|
$data = array(); |
1138
|
|
|
$data['Apple']['created']['year'] = '--'; |
1139
|
|
|
$data['Apple']['created']['month'] = '--'; |
1140
|
|
|
$data['Apple']['created']['day'] = '--'; |
1141
|
|
|
$data['Apple']['created']['hour'] = '--'; |
1142
|
|
|
$data['Apple']['created']['min'] = '--'; |
1143
|
|
|
$data['Apple']['created']['sec'] = '--'; |
1144
|
|
|
$data['Apple']['date']['year'] = '--'; |
1145
|
|
|
$data['Apple']['date']['month'] = '--'; |
1146
|
|
|
$data['Apple']['date']['day'] = '--'; |
1147
|
|
|
|
1148
|
|
|
$TestModel->data = null; |
1149
|
|
|
$TestModel->set($data); |
1150
|
|
|
$expected = array('Apple' => array('created' => '', 'date' => '')); |
1151
|
|
|
$this->assertEquals($expected, $TestModel->data); |
1152
|
|
|
|
1153
|
|
|
$data = array(); |
1154
|
|
|
$data['Apple']['created']['year'] = '2007'; |
1155
|
|
|
$data['Apple']['created']['month'] = '--'; |
1156
|
|
|
$data['Apple']['created']['day'] = '20'; |
1157
|
|
|
$data['Apple']['created']['hour'] = '10'; |
1158
|
|
|
$data['Apple']['created']['min'] = '12'; |
1159
|
|
|
$data['Apple']['created']['sec'] = '09'; |
1160
|
|
|
$data['Apple']['date']['year'] = '2006'; |
1161
|
|
|
$data['Apple']['date']['month'] = '12'; |
1162
|
|
|
$data['Apple']['date']['day'] = '25'; |
1163
|
|
|
|
1164
|
|
|
$TestModel->data = null; |
1165
|
|
|
$TestModel->set($data); |
1166
|
|
|
$expected = array('Apple' => array('created' => '', 'date' => '2006-12-25')); |
1167
|
|
|
$this->assertEquals($expected, $TestModel->data); |
1168
|
|
|
|
1169
|
|
|
$data = array(); |
1170
|
|
|
$data['Apple']['date']['year'] = '2006'; |
1171
|
|
|
$data['Apple']['date']['month'] = '12'; |
1172
|
|
|
$data['Apple']['date']['day'] = '25'; |
1173
|
|
|
|
1174
|
|
|
$TestModel->data = null; |
1175
|
|
|
$TestModel->set($data); |
1176
|
|
|
$expected = array('Apple' => array('date' => '2006-12-25')); |
1177
|
|
|
$this->assertEquals($expected, $TestModel->data); |
1178
|
|
|
|
1179
|
|
|
$db = ConnectionManager::getDataSource('test'); |
1180
|
|
|
$data = array(); |
1181
|
|
|
$data['Apple']['modified'] = $db->expression('NOW()'); |
1182
|
|
|
$TestModel->data = null; |
1183
|
|
|
$TestModel->set($data); |
1184
|
|
|
$this->assertEquals($TestModel->data, $data); |
1185
|
|
|
} |
1186
|
|
|
|
1187
|
|
|
/** |
1188
|
|
|
* testTablePrefixSwitching method |
1189
|
|
|
* |
1190
|
|
|
* @return void |
1191
|
|
|
*/ |
1192
|
|
|
public function testTablePrefixSwitching() { |
1193
|
|
|
ConnectionManager::create('database1', |
1194
|
|
|
array_merge($this->db->config, array('prefix' => 'aaa_') |
1195
|
|
|
)); |
1196
|
|
|
ConnectionManager::create('database2', |
1197
|
|
|
array_merge($this->db->config, array('prefix' => 'bbb_') |
1198
|
|
|
)); |
1199
|
|
|
|
1200
|
|
|
$db1 = ConnectionManager::getDataSource('database1'); |
1201
|
|
|
$db2 = ConnectionManager::getDataSource('database2'); |
1202
|
|
|
|
1203
|
|
|
$TestModel = new Apple(); |
1204
|
|
|
$TestModel->setDataSource('database1'); |
1205
|
|
|
$this->assertContains('aaa_apples', $this->db->fullTableName($TestModel)); |
1206
|
|
|
$this->assertContains('aaa_apples', $db1->fullTableName($TestModel)); |
1207
|
|
|
$this->assertContains('aaa_apples', $db2->fullTableName($TestModel)); |
1208
|
|
|
|
1209
|
|
|
$TestModel->setDataSource('database2'); |
1210
|
|
|
$this->assertContains('bbb_apples', $this->db->fullTableName($TestModel)); |
1211
|
|
|
$this->assertContains('bbb_apples', $db1->fullTableName($TestModel)); |
1212
|
|
|
$this->assertContains('bbb_apples', $db2->fullTableName($TestModel)); |
1213
|
|
|
|
1214
|
|
|
$TestModel = new Apple(); |
1215
|
|
|
$TestModel->tablePrefix = 'custom_'; |
1216
|
|
|
$this->assertContains('custom_apples', $this->db->fullTableName($TestModel)); |
1217
|
|
|
$TestModel->setDataSource('database1'); |
1218
|
|
|
$this->assertContains('custom_apples', $this->db->fullTableName($TestModel)); |
1219
|
|
|
$this->assertContains('custom_apples', $db1->fullTableName($TestModel)); |
1220
|
|
|
|
1221
|
|
|
$TestModel = new Apple(); |
1222
|
|
|
$TestModel->setDataSource('database1'); |
1223
|
|
|
$this->assertContains('aaa_apples', $this->db->fullTableName($TestModel)); |
1224
|
|
|
$TestModel->tablePrefix = ''; |
1225
|
|
|
$TestModel->setDataSource('database2'); |
1226
|
|
|
$this->assertContains('apples', $db2->fullTableName($TestModel)); |
1227
|
|
|
$this->assertContains('apples', $db1->fullTableName($TestModel)); |
1228
|
|
|
|
1229
|
|
|
$TestModel->tablePrefix = null; |
1230
|
|
|
$TestModel->setDataSource('database1'); |
1231
|
|
|
$this->assertContains('aaa_apples', $db2->fullTableName($TestModel)); |
1232
|
|
|
$this->assertContains('aaa_apples', $db1->fullTableName($TestModel)); |
1233
|
|
|
|
1234
|
|
|
$TestModel->tablePrefix = false; |
|
|
|
|
1235
|
|
|
$TestModel->setDataSource('database2'); |
1236
|
|
|
$this->assertContains('apples', $db2->fullTableName($TestModel)); |
1237
|
|
|
$this->assertContains('apples', $db1->fullTableName($TestModel)); |
1238
|
|
|
} |
1239
|
|
|
|
1240
|
|
|
/** |
1241
|
|
|
* Tests validation parameter order in custom validation methods |
1242
|
|
|
* |
1243
|
|
|
* @return void |
1244
|
|
|
*/ |
1245
|
|
|
public function testInvalidAssociation() { |
1246
|
|
|
$TestModel = new ValidationTest1(); |
1247
|
|
|
$this->assertNull($TestModel->getAssociated('Foo')); |
1248
|
|
|
} |
1249
|
|
|
|
1250
|
|
|
/** |
1251
|
|
|
* testLoadModelSecondIteration method |
1252
|
|
|
* |
1253
|
|
|
* @return void |
1254
|
|
|
*/ |
1255
|
|
|
public function testLoadModelSecondIteration() { |
1256
|
|
|
$this->loadFixtures('Apple', 'Message', 'Thread', 'Bid'); |
1257
|
|
|
$model = new ModelA(); |
1258
|
|
|
$this->assertInstanceOf('ModelA', $model); |
1259
|
|
|
|
1260
|
|
|
$this->assertInstanceOf('ModelB', $model->ModelB); |
1261
|
|
|
$this->assertInstanceOf('ModelD', $model->ModelB->ModelD); |
1262
|
|
|
|
1263
|
|
|
$this->assertInstanceOf('ModelC', $model->ModelC); |
1264
|
|
|
$this->assertInstanceOf('ModelD', $model->ModelC->ModelD); |
1265
|
|
|
} |
1266
|
|
|
|
1267
|
|
|
/** |
1268
|
|
|
* ensure that exists() does not persist between method calls reset on create |
1269
|
|
|
* |
1270
|
|
|
* @return void |
1271
|
|
|
*/ |
1272
|
|
|
public function testResetOfExistsOnCreate() { |
1273
|
|
|
$this->loadFixtures('Article'); |
1274
|
|
|
$Article = new Article(); |
1275
|
|
|
$Article->id = 1; |
1276
|
|
|
$Article->saveField('title', 'Reset me'); |
1277
|
|
|
$Article->delete(); |
1278
|
|
|
$Article->id = 1; |
1279
|
|
|
$this->assertFalse($Article->exists()); |
1280
|
|
|
|
1281
|
|
|
$Article->create(); |
1282
|
|
|
$this->assertFalse($Article->exists()); |
1283
|
|
|
$Article->id = 2; |
1284
|
|
|
$Article->saveField('title', 'Staying alive'); |
1285
|
|
|
$result = $Article->read(null, 2); |
1286
|
|
|
$this->assertEquals('Staying alive', $result['Article']['title']); |
1287
|
|
|
} |
1288
|
|
|
|
1289
|
|
|
/** |
1290
|
|
|
* testUseTableFalseExistsCheck method |
1291
|
|
|
* |
1292
|
|
|
* @return void |
1293
|
|
|
*/ |
1294
|
|
|
public function testUseTableFalseExistsCheck() { |
1295
|
|
|
$this->loadFixtures('Article'); |
1296
|
|
|
$Article = new Article(); |
1297
|
|
|
$Article->id = 1337; |
1298
|
|
|
$result = $Article->exists(); |
1299
|
|
|
$this->assertFalse($result); |
1300
|
|
|
|
1301
|
|
|
$Article->useTable = false; |
|
|
|
|
1302
|
|
|
$Article->id = null; |
1303
|
|
|
$result = $Article->exists(); |
1304
|
|
|
$this->assertFalse($result); |
1305
|
|
|
|
1306
|
|
|
// An article with primary key of '1' has been loaded by the fixtures. |
1307
|
|
|
$Article->useTable = false; |
1308
|
|
|
$Article->id = 1; |
1309
|
|
|
$result = $Article->exists(); |
1310
|
|
|
$this->assertTrue($result); |
1311
|
|
|
} |
1312
|
|
|
|
1313
|
|
|
/** |
1314
|
|
|
* testPluginAssociations method |
1315
|
|
|
* |
1316
|
|
|
* @return void |
1317
|
|
|
*/ |
1318
|
|
|
public function testPluginAssociations() { |
1319
|
|
|
$this->loadFixtures('TestPluginArticle', 'User', 'TestPluginComment'); |
1320
|
|
|
$TestModel = new TestPluginArticle(); |
1321
|
|
|
|
1322
|
|
|
$result = $TestModel->find('all'); |
1323
|
|
|
$expected = array( |
1324
|
|
|
array( |
1325
|
|
|
'TestPluginArticle' => array( |
1326
|
|
|
'id' => 1, |
1327
|
|
|
'user_id' => 1, |
1328
|
|
|
'title' => 'First Plugin Article', |
1329
|
|
|
'body' => 'First Plugin Article Body', |
1330
|
|
|
'published' => 'Y', |
1331
|
|
|
'created' => '2008-09-24 10:39:23', |
1332
|
|
|
'updated' => '2008-09-24 10:41:31' |
1333
|
|
|
), |
1334
|
|
|
'User' => array( |
1335
|
|
|
'id' => 1, |
1336
|
|
|
'user' => 'mariano', |
1337
|
|
|
'password' => '5f4dcc3b5aa765d61d8327deb882cf99', |
1338
|
|
|
'created' => '2007-03-17 01:16:23', |
1339
|
|
|
'updated' => '2007-03-17 01:18:31' |
1340
|
|
|
), |
1341
|
|
|
'TestPluginComment' => array( |
1342
|
|
|
array( |
1343
|
|
|
'id' => 1, |
1344
|
|
|
'article_id' => 1, |
1345
|
|
|
'user_id' => 2, |
1346
|
|
|
'comment' => 'First Comment for First Plugin Article', |
1347
|
|
|
'published' => 'Y', |
1348
|
|
|
'created' => '2008-09-24 10:45:23', |
1349
|
|
|
'updated' => '2008-09-24 10:47:31' |
1350
|
|
|
), |
1351
|
|
|
array( |
1352
|
|
|
'id' => 2, |
1353
|
|
|
'article_id' => 1, |
1354
|
|
|
'user_id' => 4, |
1355
|
|
|
'comment' => 'Second Comment for First Plugin Article', |
1356
|
|
|
'published' => 'Y', |
1357
|
|
|
'created' => '2008-09-24 10:47:23', |
1358
|
|
|
'updated' => '2008-09-24 10:49:31' |
1359
|
|
|
), |
1360
|
|
|
array( |
1361
|
|
|
'id' => 3, |
1362
|
|
|
'article_id' => 1, |
1363
|
|
|
'user_id' => 1, |
1364
|
|
|
'comment' => 'Third Comment for First Plugin Article', |
1365
|
|
|
'published' => 'Y', |
1366
|
|
|
'created' => '2008-09-24 10:49:23', |
1367
|
|
|
'updated' => '2008-09-24 10:51:31' |
1368
|
|
|
), |
1369
|
|
|
array( |
1370
|
|
|
'id' => 4, |
1371
|
|
|
'article_id' => 1, |
1372
|
|
|
'user_id' => 1, |
1373
|
|
|
'comment' => 'Fourth Comment for First Plugin Article', |
1374
|
|
|
'published' => 'N', |
1375
|
|
|
'created' => '2008-09-24 10:51:23', |
1376
|
|
|
'updated' => '2008-09-24 10:53:31' |
1377
|
|
|
))), |
1378
|
|
|
array( |
1379
|
|
|
'TestPluginArticle' => array( |
1380
|
|
|
'id' => 2, |
1381
|
|
|
'user_id' => 3, |
1382
|
|
|
'title' => 'Second Plugin Article', |
1383
|
|
|
'body' => 'Second Plugin Article Body', |
1384
|
|
|
'published' => 'Y', |
1385
|
|
|
'created' => '2008-09-24 10:41:23', |
1386
|
|
|
'updated' => '2008-09-24 10:43:31' |
1387
|
|
|
), |
1388
|
|
|
'User' => array( |
1389
|
|
|
'id' => 3, |
1390
|
|
|
'user' => 'larry', |
1391
|
|
|
'password' => '5f4dcc3b5aa765d61d8327deb882cf99', |
1392
|
|
|
'created' => '2007-03-17 01:20:23', |
1393
|
|
|
'updated' => '2007-03-17 01:22:31' |
1394
|
|
|
), |
1395
|
|
|
'TestPluginComment' => array( |
1396
|
|
|
array( |
1397
|
|
|
'id' => 5, |
1398
|
|
|
'article_id' => 2, |
1399
|
|
|
'user_id' => 1, |
1400
|
|
|
'comment' => 'First Comment for Second Plugin Article', |
1401
|
|
|
'published' => 'Y', |
1402
|
|
|
'created' => '2008-09-24 10:53:23', |
1403
|
|
|
'updated' => '2008-09-24 10:55:31' |
1404
|
|
|
), |
1405
|
|
|
array( |
1406
|
|
|
'id' => 6, |
1407
|
|
|
'article_id' => 2, |
1408
|
|
|
'user_id' => 2, |
1409
|
|
|
'comment' => 'Second Comment for Second Plugin Article', |
1410
|
|
|
'published' => 'Y', |
1411
|
|
|
'created' => '2008-09-24 10:55:23', |
1412
|
|
|
'updated' => '2008-09-24 10:57:31' |
1413
|
|
|
))), |
1414
|
|
|
array( |
1415
|
|
|
'TestPluginArticle' => array( |
1416
|
|
|
'id' => 3, |
1417
|
|
|
'user_id' => 1, |
1418
|
|
|
'title' => 'Third Plugin Article', |
1419
|
|
|
'body' => 'Third Plugin Article Body', |
1420
|
|
|
'published' => 'Y', |
1421
|
|
|
'created' => '2008-09-24 10:43:23', |
1422
|
|
|
'updated' => '2008-09-24 10:45:31' |
1423
|
|
|
), |
1424
|
|
|
'User' => array( |
1425
|
|
|
'id' => 1, |
1426
|
|
|
'user' => 'mariano', |
1427
|
|
|
'password' => '5f4dcc3b5aa765d61d8327deb882cf99', |
1428
|
|
|
'created' => '2007-03-17 01:16:23', |
1429
|
|
|
'updated' => '2007-03-17 01:18:31' |
1430
|
|
|
), |
1431
|
|
|
'TestPluginComment' => array() |
1432
|
|
|
)); |
1433
|
|
|
|
1434
|
|
|
$this->assertEquals($expected, $result); |
1435
|
|
|
} |
1436
|
|
|
|
1437
|
|
|
/** |
1438
|
|
|
* Tests getAssociated method |
1439
|
|
|
* |
1440
|
|
|
* @return void |
1441
|
|
|
*/ |
1442
|
|
|
public function testGetAssociated() { |
1443
|
|
|
$this->loadFixtures('Article', 'Tag'); |
1444
|
|
|
$Article = ClassRegistry::init('Article'); |
1445
|
|
|
|
1446
|
|
|
$assocTypes = array('hasMany', 'hasOne', 'belongsTo', 'hasAndBelongsToMany'); |
1447
|
|
|
foreach ($assocTypes as $type) { |
1448
|
|
|
$this->assertEquals($Article->getAssociated($type), array_keys($Article->{$type})); |
1449
|
|
|
} |
1450
|
|
|
|
1451
|
|
|
$Article->bindModel(array('hasMany' => array('Category'))); |
1452
|
|
|
$this->assertEquals(array('Comment', 'Category'), $Article->getAssociated('hasMany')); |
1453
|
|
|
|
1454
|
|
|
$results = $Article->getAssociated(); |
1455
|
|
|
$results = array_keys($results); |
1456
|
|
|
sort($results); |
1457
|
|
|
$this->assertEquals(array('Category', 'Comment', 'Tag', 'User'), $results); |
1458
|
|
|
|
1459
|
|
|
$Article->unbindModel(array('hasAndBelongsToMany' => array('Tag'))); |
1460
|
|
|
$this->assertEquals(array(), $Article->getAssociated('hasAndBelongsToMany')); |
1461
|
|
|
|
1462
|
|
|
$result = $Article->getAssociated('Category'); |
1463
|
|
|
$expected = array( |
1464
|
|
|
'className' => 'Category', |
1465
|
|
|
'foreignKey' => 'article_id', |
1466
|
|
|
'conditions' => '', |
1467
|
|
|
'fields' => '', |
1468
|
|
|
'order' => '', |
1469
|
|
|
'limit' => '', |
1470
|
|
|
'offset' => '', |
1471
|
|
|
'dependent' => '', |
1472
|
|
|
'exclusive' => '', |
1473
|
|
|
'finderQuery' => '', |
1474
|
|
|
'counterQuery' => '', |
1475
|
|
|
'association' => 'hasMany', |
1476
|
|
|
); |
1477
|
|
|
$this->assertEquals($expected, $result); |
1478
|
|
|
} |
1479
|
|
|
|
1480
|
|
|
/** |
1481
|
|
|
* testAutoConstructAssociations method |
1482
|
|
|
* |
1483
|
|
|
* @return void |
1484
|
|
|
*/ |
1485
|
|
|
public function testAutoConstructAssociations() { |
1486
|
|
|
$this->loadFixtures('User', 'ArticleFeatured', 'Featured', 'ArticleFeaturedsTags'); |
1487
|
|
|
$TestModel = new AssociationTest1(); |
1488
|
|
|
|
1489
|
|
|
$result = $TestModel->hasAndBelongsToMany; |
1490
|
|
|
$expected = array('AssociationTest2' => array( |
1491
|
|
|
'unique' => false, |
1492
|
|
|
'joinTable' => 'join_as_join_bs', |
1493
|
|
|
'foreignKey' => false, |
1494
|
|
|
'className' => 'AssociationTest2', |
1495
|
|
|
'with' => 'JoinAsJoinB', |
1496
|
|
|
'dynamicWith' => true, |
1497
|
|
|
'associationForeignKey' => 'join_b_id', |
1498
|
|
|
'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', |
1499
|
|
|
'finderQuery' => '' |
1500
|
|
|
)); |
1501
|
|
|
$this->assertEquals($expected, $result); |
1502
|
|
|
|
1503
|
|
|
$TestModel = new ArticleFeatured(); |
1504
|
|
|
$TestFakeModel = new ArticleFeatured(array('table' => false)); |
1505
|
|
|
|
1506
|
|
|
$expected = array( |
1507
|
|
|
'User' => array( |
1508
|
|
|
'className' => 'User', 'foreignKey' => 'user_id', |
1509
|
|
|
'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => '' |
1510
|
|
|
), |
1511
|
|
|
'Category' => array( |
1512
|
|
|
'className' => 'Category', 'foreignKey' => 'category_id', |
1513
|
|
|
'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => '' |
1514
|
|
|
) |
1515
|
|
|
); |
1516
|
|
|
$this->assertSame($TestModel->belongsTo, $expected); |
1517
|
|
|
$this->assertSame($TestFakeModel->belongsTo, $expected); |
1518
|
|
|
|
1519
|
|
|
$this->assertEquals('User', $TestModel->User->name); |
1520
|
|
|
$this->assertEquals('User', $TestFakeModel->User->name); |
1521
|
|
|
$this->assertEquals('Category', $TestModel->Category->name); |
1522
|
|
|
$this->assertEquals('Category', $TestFakeModel->Category->name); |
1523
|
|
|
|
1524
|
|
|
$expected = array( |
1525
|
|
|
'Featured' => array( |
1526
|
|
|
'className' => 'Featured', |
1527
|
|
|
'foreignKey' => 'article_featured_id', |
1528
|
|
|
'conditions' => '', |
1529
|
|
|
'fields' => '', |
1530
|
|
|
'order' => '', |
1531
|
|
|
'dependent' => '' |
1532
|
|
|
)); |
1533
|
|
|
|
1534
|
|
|
$this->assertSame($TestModel->hasOne, $expected); |
1535
|
|
|
$this->assertSame($TestFakeModel->hasOne, $expected); |
1536
|
|
|
|
1537
|
|
|
$this->assertEquals('Featured', $TestModel->Featured->name); |
1538
|
|
|
$this->assertEquals('Featured', $TestFakeModel->Featured->name); |
1539
|
|
|
|
1540
|
|
|
$expected = array( |
1541
|
|
|
'Comment' => array( |
1542
|
|
|
'className' => 'Comment', |
1543
|
|
|
'dependent' => true, |
1544
|
|
|
'foreignKey' => 'article_featured_id', |
1545
|
|
|
'conditions' => '', |
1546
|
|
|
'fields' => '', |
1547
|
|
|
'order' => '', |
1548
|
|
|
'limit' => '', |
1549
|
|
|
'offset' => '', |
1550
|
|
|
'exclusive' => '', |
1551
|
|
|
'finderQuery' => '', |
1552
|
|
|
'counterQuery' => '' |
1553
|
|
|
)); |
1554
|
|
|
|
1555
|
|
|
$this->assertSame($TestModel->hasMany, $expected); |
1556
|
|
|
$this->assertSame($TestFakeModel->hasMany, $expected); |
1557
|
|
|
|
1558
|
|
|
$this->assertEquals('Comment', $TestModel->Comment->name); |
1559
|
|
|
$this->assertEquals('Comment', $TestFakeModel->Comment->name); |
1560
|
|
|
|
1561
|
|
|
$expected = array( |
1562
|
|
|
'Tag' => array( |
1563
|
|
|
'className' => 'Tag', |
1564
|
|
|
'joinTable' => 'article_featureds_tags', |
1565
|
|
|
'with' => 'ArticleFeaturedsTag', |
1566
|
|
|
'dynamicWith' => true, |
1567
|
|
|
'foreignKey' => 'article_featured_id', |
1568
|
|
|
'associationForeignKey' => 'tag_id', |
1569
|
|
|
'conditions' => '', |
1570
|
|
|
'fields' => '', |
1571
|
|
|
'order' => '', |
1572
|
|
|
'limit' => '', |
1573
|
|
|
'offset' => '', |
1574
|
|
|
'unique' => true, |
1575
|
|
|
'finderQuery' => '', |
1576
|
|
|
)); |
1577
|
|
|
|
1578
|
|
|
$this->assertSame($TestModel->hasAndBelongsToMany, $expected); |
1579
|
|
|
$this->assertSame($TestFakeModel->hasAndBelongsToMany, $expected); |
1580
|
|
|
|
1581
|
|
|
$this->assertEquals('Tag', $TestModel->Tag->name); |
1582
|
|
|
$this->assertEquals('Tag', $TestFakeModel->Tag->name); |
1583
|
|
|
} |
1584
|
|
|
|
1585
|
|
|
/** |
1586
|
|
|
* test creating associations with plugins. Ensure a double alias isn't created |
1587
|
|
|
* |
1588
|
|
|
* @return void |
1589
|
|
|
*/ |
1590
|
|
|
public function testAutoConstructPluginAssociations() { |
1591
|
|
|
$Comment = ClassRegistry::init('TestPluginComment'); |
1592
|
|
|
|
1593
|
|
|
$this->assertEquals(2, count($Comment->belongsTo), 'Too many associations'); |
1594
|
|
|
$this->assertFalse(isset($Comment->belongsTo['TestPlugin.User'])); |
1595
|
|
|
$this->assertTrue(isset($Comment->belongsTo['User']), 'Missing association'); |
1596
|
|
|
$this->assertTrue(isset($Comment->belongsTo['TestPluginArticle']), 'Missing association'); |
1597
|
|
|
} |
1598
|
|
|
|
1599
|
|
|
/** |
1600
|
|
|
* test Model::__construct |
1601
|
|
|
* |
1602
|
|
|
* ensure that $actsAS and $findMethods are merged. |
1603
|
|
|
* |
1604
|
|
|
* @return void |
1605
|
|
|
*/ |
1606
|
|
|
public function testConstruct() { |
1607
|
|
|
$this->loadFixtures('Post'); |
1608
|
|
|
|
1609
|
|
|
$TestModel = ClassRegistry::init('MergeVarPluginPost'); |
1610
|
|
|
$this->assertEquals(array('Containable' => null, 'Tree' => null), $TestModel->actsAs); |
1611
|
|
|
$this->assertTrue(isset($TestModel->Behaviors->Containable)); |
1612
|
|
|
$this->assertTrue(isset($TestModel->Behaviors->Tree)); |
1613
|
|
|
|
1614
|
|
|
$TestModel = ClassRegistry::init('MergeVarPluginComment'); |
1615
|
|
|
$expected = array('Containable' => array('some_settings')); |
1616
|
|
|
$this->assertEquals($expected, $TestModel->actsAs); |
1617
|
|
|
$this->assertTrue(isset($TestModel->Behaviors->Containable)); |
1618
|
|
|
} |
1619
|
|
|
|
1620
|
|
|
/** |
1621
|
|
|
* test Model::__construct |
1622
|
|
|
* |
1623
|
|
|
* ensure that $actsAS and $findMethods are merged. |
1624
|
|
|
* |
1625
|
|
|
* @return void |
1626
|
|
|
*/ |
1627
|
|
|
public function testConstructWithAlternateDataSource() { |
1628
|
|
|
$TestModel = ClassRegistry::init(array( |
1629
|
|
|
'class' => 'DoesntMatter', 'ds' => 'test', 'table' => false |
1630
|
|
|
)); |
1631
|
|
|
$this->assertEquals('test', $TestModel->useDbConfig); |
1632
|
|
|
|
1633
|
|
|
//deprecated but test it anyway |
1634
|
|
|
$NewVoid = new TheVoid(null, false, 'other'); |
1635
|
|
|
$this->assertEquals('other', $NewVoid->useDbConfig); |
1636
|
|
|
} |
1637
|
|
|
|
1638
|
|
|
/** |
1639
|
|
|
* testColumnTypeFetching method |
1640
|
|
|
* |
1641
|
|
|
* @return void |
1642
|
|
|
*/ |
1643
|
|
|
public function testColumnTypeFetching() { |
1644
|
|
|
$model = new Test(); |
1645
|
|
|
$this->assertEquals('integer', $model->getColumnType('id')); |
1646
|
|
|
$this->assertEquals('text', $model->getColumnType('notes')); |
1647
|
|
|
$this->assertEquals('datetime', $model->getColumnType('updated')); |
1648
|
|
|
$this->assertEquals(null, $model->getColumnType('unknown')); |
1649
|
|
|
|
1650
|
|
|
$model = new Article(); |
1651
|
|
|
$this->assertEquals('datetime', $model->getColumnType('User.created')); |
1652
|
|
|
$this->assertEquals('integer', $model->getColumnType('Tag.id')); |
1653
|
|
|
$this->assertEquals('integer', $model->getColumnType('Article.id')); |
1654
|
|
|
} |
1655
|
|
|
|
1656
|
|
|
/** |
1657
|
|
|
* testHabtmUniqueKey method |
1658
|
|
|
* |
1659
|
|
|
* @return void |
1660
|
|
|
*/ |
1661
|
|
|
public function testHabtmUniqueKey() { |
1662
|
|
|
$model = new Item(); |
1663
|
|
|
$this->assertFalse($model->hasAndBelongsToMany['Portfolio']['unique']); |
1664
|
|
|
} |
1665
|
|
|
|
1666
|
|
|
/** |
1667
|
|
|
* testIdentity method |
1668
|
|
|
* |
1669
|
|
|
* @return void |
1670
|
|
|
*/ |
1671
|
|
|
public function testIdentity() { |
1672
|
|
|
$TestModel = new Test(); |
1673
|
|
|
$result = $TestModel->alias; |
1674
|
|
|
$expected = 'Test'; |
1675
|
|
|
$this->assertEquals($expected, $result); |
1676
|
|
|
|
1677
|
|
|
$TestModel = new TestAlias(); |
1678
|
|
|
$result = $TestModel->alias; |
1679
|
|
|
$expected = 'TestAlias'; |
1680
|
|
|
$this->assertEquals($expected, $result); |
1681
|
|
|
|
1682
|
|
|
$TestModel = new Test(array('alias' => 'AnotherTest')); |
1683
|
|
|
$result = $TestModel->alias; |
1684
|
|
|
$expected = 'AnotherTest'; |
1685
|
|
|
$this->assertEquals($expected, $result); |
1686
|
|
|
|
1687
|
|
|
$TestModel = ClassRegistry::init('Test'); |
1688
|
|
|
$expected = null; |
1689
|
|
|
$this->assertEquals($expected, $TestModel->plugin); |
1690
|
|
|
|
1691
|
|
|
$TestModel = ClassRegistry::init('TestPlugin.TestPluginComment'); |
1692
|
|
|
$expected = 'TestPlugin'; |
1693
|
|
|
$this->assertEquals($expected, $TestModel->plugin); |
1694
|
|
|
} |
1695
|
|
|
|
1696
|
|
|
/** |
1697
|
|
|
* testWithAssociation method |
1698
|
|
|
* |
1699
|
|
|
* @return void |
1700
|
|
|
*/ |
1701
|
|
|
public function testWithAssociation() { |
1702
|
|
|
$this->loadFixtures('Something', 'SomethingElse', 'JoinThing'); |
1703
|
|
|
$TestModel = new Something(); |
1704
|
|
|
$result = $TestModel->SomethingElse->find('all'); |
1705
|
|
|
|
1706
|
|
|
$expected = array( |
1707
|
|
|
array( |
1708
|
|
|
'SomethingElse' => array( |
1709
|
|
|
'id' => '1', |
1710
|
|
|
'title' => 'First Post', |
1711
|
|
|
'body' => 'First Post Body', |
1712
|
|
|
'published' => 'Y', |
1713
|
|
|
'created' => '2007-03-18 10:39:23', |
1714
|
|
|
'updated' => '2007-03-18 10:41:31' |
1715
|
|
|
), |
1716
|
|
|
'Something' => array( |
1717
|
|
|
array( |
1718
|
|
|
'id' => '3', |
1719
|
|
|
'title' => 'Third Post', |
1720
|
|
|
'body' => 'Third Post Body', |
1721
|
|
|
'published' => 'Y', |
1722
|
|
|
'created' => '2007-03-18 10:43:23', |
1723
|
|
|
'updated' => '2007-03-18 10:45:31', |
1724
|
|
|
'JoinThing' => array( |
1725
|
|
|
'id' => '3', |
1726
|
|
|
'something_id' => '3', |
1727
|
|
|
'something_else_id' => '1', |
1728
|
|
|
'doomed' => true, |
1729
|
|
|
'created' => '2007-03-18 10:43:23', |
1730
|
|
|
'updated' => '2007-03-18 10:45:31' |
1731
|
|
|
)))), |
1732
|
|
|
array( |
1733
|
|
|
'SomethingElse' => array( |
1734
|
|
|
'id' => '2', |
1735
|
|
|
'title' => 'Second Post', |
1736
|
|
|
'body' => 'Second Post Body', |
1737
|
|
|
'published' => 'Y', |
1738
|
|
|
'created' => '2007-03-18 10:41:23', |
1739
|
|
|
'updated' => '2007-03-18 10:43:31' |
1740
|
|
|
), |
1741
|
|
|
'Something' => array( |
1742
|
|
|
array( |
1743
|
|
|
'id' => '1', |
1744
|
|
|
'title' => 'First Post', |
1745
|
|
|
'body' => 'First Post Body', |
1746
|
|
|
'published' => 'Y', |
1747
|
|
|
'created' => '2007-03-18 10:39:23', |
1748
|
|
|
'updated' => '2007-03-18 10:41:31', |
1749
|
|
|
'JoinThing' => array( |
1750
|
|
|
'id' => '1', |
1751
|
|
|
'something_id' => '1', |
1752
|
|
|
'something_else_id' => '2', |
1753
|
|
|
'doomed' => true, |
1754
|
|
|
'created' => '2007-03-18 10:39:23', |
1755
|
|
|
'updated' => '2007-03-18 10:41:31' |
1756
|
|
|
)))), |
1757
|
|
|
array( |
1758
|
|
|
'SomethingElse' => array( |
1759
|
|
|
'id' => '3', |
1760
|
|
|
'title' => 'Third Post', |
1761
|
|
|
'body' => 'Third Post Body', |
1762
|
|
|
'published' => 'Y', |
1763
|
|
|
'created' => '2007-03-18 10:43:23', |
1764
|
|
|
'updated' => '2007-03-18 10:45:31' |
1765
|
|
|
), |
1766
|
|
|
'Something' => array( |
1767
|
|
|
array( |
1768
|
|
|
'id' => '2', |
1769
|
|
|
'title' => 'Second Post', |
1770
|
|
|
'body' => 'Second Post Body', |
1771
|
|
|
'published' => 'Y', |
1772
|
|
|
'created' => '2007-03-18 10:41:23', |
1773
|
|
|
'updated' => '2007-03-18 10:43:31', |
1774
|
|
|
'JoinThing' => array( |
1775
|
|
|
'id' => '2', |
1776
|
|
|
'something_id' => '2', |
1777
|
|
|
'something_else_id' => '3', |
1778
|
|
|
'doomed' => false, |
1779
|
|
|
'created' => '2007-03-18 10:41:23', |
1780
|
|
|
'updated' => '2007-03-18 10:43:31' |
1781
|
|
|
))))); |
1782
|
|
|
$this->assertEquals($expected, $result); |
1783
|
|
|
|
1784
|
|
|
$result = $TestModel->find('all'); |
1785
|
|
|
$expected = array( |
1786
|
|
|
array( |
1787
|
|
|
'Something' => array( |
1788
|
|
|
'id' => '1', |
1789
|
|
|
'title' => 'First Post', |
1790
|
|
|
'body' => 'First Post Body', |
1791
|
|
|
'published' => 'Y', |
1792
|
|
|
'created' => '2007-03-18 10:39:23', |
1793
|
|
|
'updated' => '2007-03-18 10:41:31' |
1794
|
|
|
), |
1795
|
|
|
'SomethingElse' => array( |
1796
|
|
|
array( |
1797
|
|
|
'id' => '2', |
1798
|
|
|
'title' => 'Second Post', |
1799
|
|
|
'body' => 'Second Post Body', |
1800
|
|
|
'published' => 'Y', |
1801
|
|
|
'created' => '2007-03-18 10:41:23', |
1802
|
|
|
'updated' => '2007-03-18 10:43:31', |
1803
|
|
|
'JoinThing' => array( |
1804
|
|
|
'doomed' => true, |
1805
|
|
|
'something_id' => '1', |
1806
|
|
|
'something_else_id' => '2' |
1807
|
|
|
)))), |
1808
|
|
|
array( |
1809
|
|
|
'Something' => array( |
1810
|
|
|
'id' => '2', |
1811
|
|
|
'title' => 'Second Post', |
1812
|
|
|
'body' => 'Second Post Body', |
1813
|
|
|
'published' => 'Y', |
1814
|
|
|
'created' => '2007-03-18 10:41:23', |
1815
|
|
|
'updated' => '2007-03-18 10:43:31' |
1816
|
|
|
), |
1817
|
|
|
'SomethingElse' => array( |
1818
|
|
|
array( |
1819
|
|
|
'id' => '3', |
1820
|
|
|
'title' => 'Third Post', |
1821
|
|
|
'body' => 'Third Post Body', |
1822
|
|
|
'published' => 'Y', |
1823
|
|
|
'created' => '2007-03-18 10:43:23', |
1824
|
|
|
'updated' => '2007-03-18 10:45:31', |
1825
|
|
|
'JoinThing' => array( |
1826
|
|
|
'doomed' => false, |
1827
|
|
|
'something_id' => '2', |
1828
|
|
|
'something_else_id' => '3' |
1829
|
|
|
)))), |
1830
|
|
|
array( |
1831
|
|
|
'Something' => array( |
1832
|
|
|
'id' => '3', |
1833
|
|
|
'title' => 'Third Post', |
1834
|
|
|
'body' => 'Third Post Body', |
1835
|
|
|
'published' => 'Y', |
1836
|
|
|
'created' => '2007-03-18 10:43:23', |
1837
|
|
|
'updated' => '2007-03-18 10:45:31' |
1838
|
|
|
), |
1839
|
|
|
'SomethingElse' => array( |
1840
|
|
|
array( |
1841
|
|
|
'id' => '1', |
1842
|
|
|
'title' => 'First Post', |
1843
|
|
|
'body' => 'First Post Body', |
1844
|
|
|
'published' => 'Y', |
1845
|
|
|
'created' => '2007-03-18 10:39:23', |
1846
|
|
|
'updated' => '2007-03-18 10:41:31', |
1847
|
|
|
'JoinThing' => array( |
1848
|
|
|
'doomed' => true, |
1849
|
|
|
'something_id' => '3', |
1850
|
|
|
'something_else_id' => '1' |
1851
|
|
|
))))); |
1852
|
|
|
$this->assertEquals($expected, $result); |
1853
|
|
|
|
1854
|
|
|
$result = $TestModel->findById(1); |
|
|
|
|
1855
|
|
|
$expected = array( |
1856
|
|
|
'Something' => array( |
1857
|
|
|
'id' => '1', |
1858
|
|
|
'title' => 'First Post', |
1859
|
|
|
'body' => 'First Post Body', |
1860
|
|
|
'published' => 'Y', |
1861
|
|
|
'created' => '2007-03-18 10:39:23', |
1862
|
|
|
'updated' => '2007-03-18 10:41:31' |
1863
|
|
|
), |
1864
|
|
|
'SomethingElse' => array( |
1865
|
|
|
array( |
1866
|
|
|
'id' => '2', |
1867
|
|
|
'title' => 'Second Post', |
1868
|
|
|
'body' => 'Second Post Body', |
1869
|
|
|
'published' => 'Y', |
1870
|
|
|
'created' => '2007-03-18 10:41:23', |
1871
|
|
|
'updated' => '2007-03-18 10:43:31', |
1872
|
|
|
'JoinThing' => array( |
1873
|
|
|
'doomed' => true, |
1874
|
|
|
'something_id' => '1', |
1875
|
|
|
'something_else_id' => '2' |
1876
|
|
|
)))); |
1877
|
|
|
$this->assertEquals($expected, $result); |
1878
|
|
|
|
1879
|
|
|
$expected = $TestModel->findById(1); |
|
|
|
|
1880
|
|
|
$TestModel->set($expected); |
1881
|
|
|
$TestModel->save(); |
1882
|
|
|
$result = $TestModel->findById(1); |
|
|
|
|
1883
|
|
|
$this->assertEquals($expected, $result); |
1884
|
|
|
|
1885
|
|
|
$TestModel->hasAndBelongsToMany['SomethingElse']['unique'] = false; |
1886
|
|
|
$TestModel->create(array( |
1887
|
|
|
'Something' => array('id' => 1), |
1888
|
|
|
'SomethingElse' => array(3, array( |
1889
|
|
|
'something_else_id' => 1, |
1890
|
|
|
'doomed' => true |
1891
|
|
|
)))); |
1892
|
|
|
|
1893
|
|
|
$TestModel->save(); |
1894
|
|
|
|
1895
|
|
|
$TestModel->hasAndBelongsToMany['SomethingElse']['order'] = 'SomethingElse.id ASC'; |
1896
|
|
|
$result = $TestModel->findById(1); |
|
|
|
|
1897
|
|
|
$expected = array( |
1898
|
|
|
'Something' => array( |
1899
|
|
|
'id' => '1', |
1900
|
|
|
'title' => 'First Post', |
1901
|
|
|
'body' => 'First Post Body', |
1902
|
|
|
'published' => 'Y', |
1903
|
|
|
'created' => '2007-03-18 10:39:23' |
1904
|
|
|
), |
1905
|
|
|
'SomethingElse' => array( |
1906
|
|
|
array( |
1907
|
|
|
'id' => '1', |
1908
|
|
|
'title' => 'First Post', |
1909
|
|
|
'body' => 'First Post Body', |
1910
|
|
|
'published' => 'Y', |
1911
|
|
|
'created' => '2007-03-18 10:39:23', |
1912
|
|
|
'updated' => '2007-03-18 10:41:31', |
1913
|
|
|
'JoinThing' => array( |
1914
|
|
|
'doomed' => true, |
1915
|
|
|
'something_id' => '1', |
1916
|
|
|
'something_else_id' => '1' |
1917
|
|
|
) |
1918
|
|
|
), |
1919
|
|
|
array( |
1920
|
|
|
'id' => '2', |
1921
|
|
|
'title' => 'Second Post', |
1922
|
|
|
'body' => 'Second Post Body', |
1923
|
|
|
'published' => 'Y', |
1924
|
|
|
'created' => '2007-03-18 10:41:23', |
1925
|
|
|
'updated' => '2007-03-18 10:43:31', |
1926
|
|
|
'JoinThing' => array( |
1927
|
|
|
'doomed' => true, |
1928
|
|
|
'something_id' => '1', |
1929
|
|
|
'something_else_id' => '2' |
1930
|
|
|
) |
1931
|
|
|
), |
1932
|
|
|
array( |
1933
|
|
|
'id' => '3', |
1934
|
|
|
'title' => 'Third Post', |
1935
|
|
|
'body' => 'Third Post Body', |
1936
|
|
|
'published' => 'Y', |
1937
|
|
|
'created' => '2007-03-18 10:43:23', |
1938
|
|
|
'updated' => '2007-03-18 10:45:31', |
1939
|
|
|
'JoinThing' => array( |
1940
|
|
|
'doomed' => false, |
1941
|
|
|
'something_id' => '1', |
1942
|
|
|
'something_else_id' => '3') |
1943
|
|
|
) |
1944
|
|
|
) |
1945
|
|
|
); |
1946
|
|
|
$this->assertEquals(self::date(), $result['Something']['updated']); |
1947
|
|
|
unset($result['Something']['updated']); |
1948
|
|
|
$this->assertEquals($expected, $result); |
1949
|
|
|
} |
1950
|
|
|
|
1951
|
|
|
/** |
1952
|
|
|
* testFindSelfAssociations method |
1953
|
|
|
* |
1954
|
|
|
* @return void |
1955
|
|
|
*/ |
1956
|
|
|
public function testFindSelfAssociations() { |
1957
|
|
|
$this->loadFixtures('Person'); |
1958
|
|
|
|
1959
|
|
|
$TestModel = new Person(); |
1960
|
|
|
$TestModel->recursive = 2; |
1961
|
|
|
$result = $TestModel->read(null, 1); |
1962
|
|
|
$expected = array( |
1963
|
|
|
'Person' => array( |
1964
|
|
|
'id' => 1, |
1965
|
|
|
'name' => 'person', |
1966
|
|
|
'mother_id' => 2, |
1967
|
|
|
'father_id' => 3 |
1968
|
|
|
), |
1969
|
|
|
'Mother' => array( |
1970
|
|
|
'id' => 2, |
1971
|
|
|
'name' => 'mother', |
1972
|
|
|
'mother_id' => 4, |
1973
|
|
|
'father_id' => 5, |
1974
|
|
|
'Mother' => array( |
1975
|
|
|
'id' => 4, |
1976
|
|
|
'name' => 'mother - grand mother', |
1977
|
|
|
'mother_id' => 0, |
1978
|
|
|
'father_id' => 0 |
1979
|
|
|
), |
1980
|
|
|
'Father' => array( |
1981
|
|
|
'id' => 5, |
1982
|
|
|
'name' => 'mother - grand father', |
1983
|
|
|
'mother_id' => 0, |
1984
|
|
|
'father_id' => 0 |
1985
|
|
|
)), |
1986
|
|
|
'Father' => array( |
1987
|
|
|
'id' => 3, |
1988
|
|
|
'name' => 'father', |
1989
|
|
|
'mother_id' => 6, |
1990
|
|
|
'father_id' => 7, |
1991
|
|
|
'Father' => array( |
1992
|
|
|
'id' => 7, |
1993
|
|
|
'name' => 'father - grand father', |
1994
|
|
|
'mother_id' => 0, |
1995
|
|
|
'father_id' => 0 |
1996
|
|
|
), |
1997
|
|
|
'Mother' => array( |
1998
|
|
|
'id' => 6, |
1999
|
|
|
'name' => 'father - grand mother', |
2000
|
|
|
'mother_id' => 0, |
2001
|
|
|
'father_id' => 0 |
2002
|
|
|
))); |
2003
|
|
|
|
2004
|
|
|
$this->assertEquals($expected, $result); |
2005
|
|
|
|
2006
|
|
|
$TestModel->recursive = 3; |
2007
|
|
|
$result = $TestModel->read(null, 1); |
2008
|
|
|
$expected = array( |
2009
|
|
|
'Person' => array( |
2010
|
|
|
'id' => 1, |
2011
|
|
|
'name' => 'person', |
2012
|
|
|
'mother_id' => 2, |
2013
|
|
|
'father_id' => 3 |
2014
|
|
|
), |
2015
|
|
|
'Mother' => array( |
2016
|
|
|
'id' => 2, |
2017
|
|
|
'name' => 'mother', |
2018
|
|
|
'mother_id' => 4, |
2019
|
|
|
'father_id' => 5, |
2020
|
|
|
'Mother' => array( |
2021
|
|
|
'id' => 4, |
2022
|
|
|
'name' => 'mother - grand mother', |
2023
|
|
|
'mother_id' => 0, |
2024
|
|
|
'father_id' => 0, |
2025
|
|
|
'Mother' => array(), |
2026
|
|
|
'Father' => array()), |
2027
|
|
|
'Father' => array( |
2028
|
|
|
'id' => 5, |
2029
|
|
|
'name' => 'mother - grand father', |
2030
|
|
|
'mother_id' => 0, |
2031
|
|
|
'father_id' => 0, |
2032
|
|
|
'Father' => array(), |
2033
|
|
|
'Mother' => array() |
2034
|
|
|
)), |
2035
|
|
|
'Father' => array( |
2036
|
|
|
'id' => 3, |
2037
|
|
|
'name' => 'father', |
2038
|
|
|
'mother_id' => 6, |
2039
|
|
|
'father_id' => 7, |
2040
|
|
|
'Father' => array( |
2041
|
|
|
'id' => 7, |
2042
|
|
|
'name' => 'father - grand father', |
2043
|
|
|
'mother_id' => 0, |
2044
|
|
|
'father_id' => 0, |
2045
|
|
|
'Father' => array(), |
2046
|
|
|
'Mother' => array() |
2047
|
|
|
), |
2048
|
|
|
'Mother' => array( |
2049
|
|
|
'id' => 6, |
2050
|
|
|
'name' => 'father - grand mother', |
2051
|
|
|
'mother_id' => 0, |
2052
|
|
|
'father_id' => 0, |
2053
|
|
|
'Mother' => array(), |
2054
|
|
|
'Father' => array() |
2055
|
|
|
))); |
2056
|
|
|
|
2057
|
|
|
$this->assertEquals($expected, $result); |
2058
|
|
|
} |
2059
|
|
|
|
2060
|
|
|
/** |
2061
|
|
|
* testDynamicAssociations method |
2062
|
|
|
* |
2063
|
|
|
* @return void |
2064
|
|
|
*/ |
2065
|
|
|
public function testDynamicAssociations() { |
2066
|
|
|
$this->loadFixtures('Article', 'Comment'); |
2067
|
|
|
$TestModel = new Article(); |
2068
|
|
|
|
2069
|
|
|
$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->hasOne = array(); |
2070
|
|
|
$TestModel->hasMany['Comment'] = array_merge($TestModel->hasMany['Comment'], array( |
2071
|
|
|
'foreignKey' => false, |
2072
|
|
|
'conditions' => array('Comment.user_id =' => '2') |
2073
|
|
|
)); |
2074
|
|
|
$result = $TestModel->find('all'); |
2075
|
|
|
$expected = array( |
2076
|
|
|
array( |
2077
|
|
|
'Article' => array( |
2078
|
|
|
'id' => '1', |
2079
|
|
|
'user_id' => '1', |
2080
|
|
|
'title' => 'First Article', |
2081
|
|
|
'body' => 'First Article Body', |
2082
|
|
|
'published' => 'Y', |
2083
|
|
|
'created' => '2007-03-18 10:39:23', |
2084
|
|
|
'updated' => '2007-03-18 10:41:31' |
2085
|
|
|
), |
2086
|
|
|
'Comment' => array( |
2087
|
|
|
array( |
2088
|
|
|
'id' => '1', |
2089
|
|
|
'article_id' => '1', |
2090
|
|
|
'user_id' => '2', |
2091
|
|
|
'comment' => 'First Comment for First Article', |
2092
|
|
|
'published' => 'Y', |
2093
|
|
|
'created' => '2007-03-18 10:45:23', |
2094
|
|
|
'updated' => '2007-03-18 10:47:31' |
2095
|
|
|
), |
2096
|
|
|
array( |
2097
|
|
|
'id' => '6', |
2098
|
|
|
'article_id' => '2', |
2099
|
|
|
'user_id' => '2', |
2100
|
|
|
'comment' => 'Second Comment for Second Article', |
2101
|
|
|
'published' => 'Y', |
2102
|
|
|
'created' => '2007-03-18 10:55:23', |
2103
|
|
|
'updated' => '2007-03-18 10:57:31' |
2104
|
|
|
))), |
2105
|
|
|
array( |
2106
|
|
|
'Article' => array( |
2107
|
|
|
'id' => '2', |
2108
|
|
|
'user_id' => '3', |
2109
|
|
|
'title' => 'Second Article', |
2110
|
|
|
'body' => 'Second Article Body', |
2111
|
|
|
'published' => 'Y', |
2112
|
|
|
'created' => '2007-03-18 10:41:23', |
2113
|
|
|
'updated' => '2007-03-18 10:43:31' |
2114
|
|
|
), |
2115
|
|
|
'Comment' => array( |
2116
|
|
|
array( |
2117
|
|
|
'id' => '1', |
2118
|
|
|
'article_id' => '1', |
2119
|
|
|
'user_id' => '2', |
2120
|
|
|
'comment' => 'First Comment for First Article', |
2121
|
|
|
'published' => 'Y', |
2122
|
|
|
'created' => '2007-03-18 10:45:23', |
2123
|
|
|
'updated' => '2007-03-18 10:47:31' |
2124
|
|
|
), |
2125
|
|
|
array( |
2126
|
|
|
'id' => '6', |
2127
|
|
|
'article_id' => '2', |
2128
|
|
|
'user_id' => '2', |
2129
|
|
|
'comment' => 'Second Comment for Second Article', |
2130
|
|
|
'published' => 'Y', |
2131
|
|
|
'created' => '2007-03-18 10:55:23', |
2132
|
|
|
'updated' => '2007-03-18 10:57:31' |
2133
|
|
|
))), |
2134
|
|
|
array( |
2135
|
|
|
'Article' => array( |
2136
|
|
|
'id' => '3', |
2137
|
|
|
'user_id' => '1', |
2138
|
|
|
'title' => 'Third Article', |
2139
|
|
|
'body' => 'Third Article Body', |
2140
|
|
|
'published' => 'Y', |
2141
|
|
|
'created' => '2007-03-18 10:43:23', |
2142
|
|
|
'updated' => '2007-03-18 10:45:31' |
2143
|
|
|
), |
2144
|
|
|
'Comment' => array( |
2145
|
|
|
array( |
2146
|
|
|
'id' => '1', |
2147
|
|
|
'article_id' => '1', |
2148
|
|
|
'user_id' => '2', |
2149
|
|
|
'comment' => 'First Comment for First Article', |
2150
|
|
|
'published' => 'Y', |
2151
|
|
|
'created' => '2007-03-18 10:45:23', |
2152
|
|
|
'updated' => '2007-03-18 10:47:31' |
2153
|
|
|
), |
2154
|
|
|
array( |
2155
|
|
|
'id' => '6', |
2156
|
|
|
'article_id' => '2', |
2157
|
|
|
'user_id' => '2', |
2158
|
|
|
'comment' => 'Second Comment for Second Article', |
2159
|
|
|
'published' => 'Y', |
2160
|
|
|
'created' => '2007-03-18 10:55:23', |
2161
|
|
|
'updated' => '2007-03-18 10:57:31' |
2162
|
|
|
)))); |
2163
|
|
|
|
2164
|
|
|
$this->assertEquals($expected, $result); |
2165
|
|
|
} |
2166
|
|
|
|
2167
|
|
|
/** |
2168
|
|
|
* testCreation method |
2169
|
|
|
* |
2170
|
|
|
* @return void |
2171
|
|
|
*/ |
2172
|
|
|
public function testCreation() { |
2173
|
|
|
$this->loadFixtures('Article', 'ArticleFeaturedsTags', 'User', 'Featured'); |
2174
|
|
|
$TestModel = new Test(); |
2175
|
|
|
$result = $TestModel->create(); |
2176
|
|
|
$expected = array('Test' => array('notes' => 'write some notes here')); |
2177
|
|
|
$this->assertEquals($expected, $result); |
2178
|
|
|
$TestModel = new User(); |
2179
|
|
|
$result = $TestModel->schema(); |
2180
|
|
|
|
2181
|
|
|
if (isset($this->db->columns['primary_key']['length'])) { |
2182
|
|
|
$intLength = $this->db->columns['primary_key']['length']; |
2183
|
|
|
} elseif (isset($this->db->columns['integer']['length'])) { |
2184
|
|
|
$intLength = $this->db->columns['integer']['length']; |
2185
|
|
|
} else { |
2186
|
|
|
$intLength = 11; |
2187
|
|
|
} |
2188
|
|
|
foreach (array('collate', 'charset', 'comment') as $type) { |
2189
|
|
|
foreach ($result as $i => $r) { |
2190
|
|
|
unset($result[$i][$type]); |
2191
|
|
|
} |
2192
|
|
|
} |
2193
|
|
|
|
2194
|
|
|
$expected = array( |
2195
|
|
|
'id' => array( |
2196
|
|
|
'type' => 'integer', |
2197
|
|
|
'null' => false, |
2198
|
|
|
'default' => null, |
2199
|
|
|
'length' => $intLength, |
2200
|
|
|
'key' => 'primary' |
2201
|
|
|
), |
2202
|
|
|
'user' => array( |
2203
|
|
|
'type' => 'string', |
2204
|
|
|
'null' => true, |
2205
|
|
|
'default' => '', |
2206
|
|
|
'length' => 255 |
2207
|
|
|
), |
2208
|
|
|
'password' => array( |
2209
|
|
|
'type' => 'string', |
2210
|
|
|
'null' => true, |
2211
|
|
|
'default' => '', |
2212
|
|
|
'length' => 255 |
2213
|
|
|
), |
2214
|
|
|
'created' => array( |
2215
|
|
|
'type' => 'datetime', |
2216
|
|
|
'null' => true, |
2217
|
|
|
'default' => null, |
2218
|
|
|
'length' => null |
2219
|
|
|
), |
2220
|
|
|
'updated' => array( |
2221
|
|
|
'type' => 'datetime', |
2222
|
|
|
'null' => true, |
2223
|
|
|
'default' => null, |
2224
|
|
|
'length' => null |
2225
|
|
|
)); |
2226
|
|
|
|
2227
|
|
|
$this->assertEquals($expected, $result); |
2228
|
|
|
|
2229
|
|
|
$TestModel = new Article(); |
2230
|
|
|
$result = $TestModel->create(); |
2231
|
|
|
$expected = array('Article' => array('published' => 'N')); |
2232
|
|
|
$this->assertEquals($expected, $result); |
2233
|
|
|
|
2234
|
|
|
$FeaturedModel = new Featured(); |
2235
|
|
|
$data = array( |
2236
|
|
|
'article_featured_id' => 1, |
2237
|
|
|
'category_id' => 1, |
2238
|
|
|
'published_date' => array( |
2239
|
|
|
'year' => 2008, |
2240
|
|
|
'month' => 06, |
2241
|
|
|
'day' => 11 |
2242
|
|
|
), |
2243
|
|
|
'end_date' => array( |
2244
|
|
|
'year' => 2008, |
2245
|
|
|
'month' => 06, |
2246
|
|
|
'day' => 20 |
2247
|
|
|
)); |
2248
|
|
|
|
2249
|
|
|
$expected = array( |
2250
|
|
|
'Featured' => array( |
2251
|
|
|
'article_featured_id' => 1, |
2252
|
|
|
'category_id' => 1, |
2253
|
|
|
'published_date' => '2008-06-11 00:00:00', |
2254
|
|
|
'end_date' => '2008-06-20 00:00:00' |
2255
|
|
|
)); |
2256
|
|
|
|
2257
|
|
|
$this->assertEquals($expected, $FeaturedModel->create($data)); |
2258
|
|
|
|
2259
|
|
|
$data = array( |
2260
|
|
|
'published_date' => array( |
2261
|
|
|
'year' => 2008, |
2262
|
|
|
'month' => 06, |
2263
|
|
|
'day' => 11 |
2264
|
|
|
), |
2265
|
|
|
'end_date' => array( |
2266
|
|
|
'year' => 2008, |
2267
|
|
|
'month' => 06, |
2268
|
|
|
'day' => 20 |
2269
|
|
|
), |
2270
|
|
|
'article_featured_id' => 1, |
2271
|
|
|
'category_id' => 1 |
2272
|
|
|
); |
2273
|
|
|
|
2274
|
|
|
$expected = array( |
2275
|
|
|
'Featured' => array( |
2276
|
|
|
'published_date' => '2008-06-11 00:00:00', |
2277
|
|
|
'end_date' => '2008-06-20 00:00:00', |
2278
|
|
|
'article_featured_id' => 1, |
2279
|
|
|
'category_id' => 1 |
2280
|
|
|
)); |
2281
|
|
|
|
2282
|
|
|
$this->assertEquals($expected, $FeaturedModel->create($data)); |
2283
|
|
|
} |
2284
|
|
|
|
2285
|
|
|
/** |
2286
|
|
|
* testEscapeField to prove it escapes the field well even when it has part of the alias on it |
2287
|
|
|
* |
2288
|
|
|
* @return void |
2289
|
|
|
*/ |
2290
|
|
|
public function testEscapeField() { |
2291
|
|
|
$TestModel = new Test(); |
2292
|
|
|
$db = $TestModel->getDataSource(); |
2293
|
|
|
|
2294
|
|
|
$result = $TestModel->escapeField('test_field'); |
2295
|
|
|
$expected = $db->name('Test.test_field'); |
2296
|
|
|
$this->assertEquals($expected, $result); |
2297
|
|
|
|
2298
|
|
|
$result = $TestModel->escapeField('TestField'); |
2299
|
|
|
$expected = $db->name('Test.TestField'); |
2300
|
|
|
$this->assertEquals($expected, $result); |
2301
|
|
|
|
2302
|
|
|
$result = $TestModel->escapeField('DomainHandle', 'Domain'); |
2303
|
|
|
$expected = $db->name('Domain.DomainHandle'); |
2304
|
|
|
$this->assertEquals($expected, $result); |
2305
|
|
|
|
2306
|
|
|
ConnectionManager::create('mock', array('datasource' => 'DboMock')); |
2307
|
|
|
$TestModel->setDataSource('mock'); |
2308
|
|
|
$db = $TestModel->getDataSource(); |
2309
|
|
|
|
2310
|
|
|
$result = $TestModel->escapeField('DomainHandle', 'Domain'); |
2311
|
|
|
$expected = $db->name('Domain.DomainHandle'); |
2312
|
|
|
$this->assertEquals($expected, $result); |
2313
|
|
|
ConnectionManager::drop('mock'); |
2314
|
|
|
} |
2315
|
|
|
|
2316
|
|
|
/** |
2317
|
|
|
* testGetID |
2318
|
|
|
* |
2319
|
|
|
* @return void |
2320
|
|
|
*/ |
2321
|
|
|
public function testGetID() { |
2322
|
|
|
$TestModel = new Test(); |
2323
|
|
|
|
2324
|
|
|
$result = $TestModel->getID(); |
2325
|
|
|
$this->assertFalse($result); |
2326
|
|
|
|
2327
|
|
|
$TestModel->id = 9; |
2328
|
|
|
$result = $TestModel->getID(); |
2329
|
|
|
$this->assertEquals(9, $result); |
2330
|
|
|
|
2331
|
|
|
$TestModel->id = array(10, 9, 8, 7); |
2332
|
|
|
$result = $TestModel->getID(2); |
2333
|
|
|
$this->assertEquals(8, $result); |
2334
|
|
|
|
2335
|
|
|
$TestModel->id = array(array(), 1, 2, 3); |
2336
|
|
|
$result = $TestModel->getID(); |
2337
|
|
|
$this->assertFalse($result); |
2338
|
|
|
} |
2339
|
|
|
|
2340
|
|
|
/** |
2341
|
|
|
* test that model->hasMethod checks self and behaviors. |
2342
|
|
|
* |
2343
|
|
|
* @return void |
2344
|
|
|
*/ |
2345
|
|
|
public function testHasMethod() { |
2346
|
|
|
$Article = new Article(); |
2347
|
|
|
$Article->Behaviors = $this->getMock('BehaviorCollection'); |
2348
|
|
|
|
2349
|
|
|
$Article->Behaviors->expects($this->at(0)) |
2350
|
|
|
->method('hasMethod') |
2351
|
|
|
->will($this->returnValue(true)); |
2352
|
|
|
|
2353
|
|
|
$Article->Behaviors->expects($this->at(1)) |
2354
|
|
|
->method('hasMethod') |
2355
|
|
|
->will($this->returnValue(false)); |
2356
|
|
|
|
2357
|
|
|
$this->assertTrue($Article->hasMethod('find')); |
2358
|
|
|
|
2359
|
|
|
$this->assertTrue($Article->hasMethod('pass')); |
2360
|
|
|
$this->assertFalse($Article->hasMethod('fail')); |
2361
|
|
|
} |
2362
|
|
|
|
2363
|
|
|
/** |
2364
|
|
|
* testMultischemaFixture |
2365
|
|
|
* |
2366
|
|
|
* @return void |
2367
|
|
|
*/ |
2368
|
|
|
public function testMultischemaFixture() { |
2369
|
|
|
$config = ConnectionManager::enumConnectionObjects(); |
2370
|
|
|
$this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with Sqlite.'); |
2371
|
|
|
$this->skipIf(!isset($config['test']) || !isset($config['test2']), |
2372
|
|
|
'Primary and secondary test databases not configured, skipping cross-database join tests. To run these tests define $test and $test2 in your database configuration.' |
2373
|
|
|
); |
2374
|
|
|
|
2375
|
|
|
$this->loadFixtures('Player', 'Guild', 'GuildsPlayer'); |
2376
|
|
|
|
2377
|
|
|
$Player = ClassRegistry::init('Player'); |
2378
|
|
|
$this->assertEquals('test', $Player->useDbConfig); |
2379
|
|
|
$this->assertEquals('test', $Player->Guild->useDbConfig); |
2380
|
|
|
$this->assertEquals('test2', $Player->Guild->GuildsPlayer->useDbConfig); |
2381
|
|
|
$this->assertEquals('test2', $Player->GuildsPlayer->useDbConfig); |
2382
|
|
|
|
2383
|
|
|
$players = $Player->find('all', array('recursive' => -1)); |
2384
|
|
|
$guilds = $Player->Guild->find('all', array('recursive' => -1)); |
2385
|
|
|
$guildsPlayers = $Player->GuildsPlayer->find('all', array('recursive' => -1)); |
2386
|
|
|
|
2387
|
|
|
$this->assertEquals(true, count($players) > 1); |
2388
|
|
|
$this->assertEquals(true, count($guilds) > 1); |
2389
|
|
|
$this->assertEquals(true, count($guildsPlayers) > 1); |
2390
|
|
|
} |
2391
|
|
|
|
2392
|
|
|
/** |
2393
|
|
|
* testMultischemaFixtureWithThreeDatabases, three databases |
2394
|
|
|
* |
2395
|
|
|
* @return void |
2396
|
|
|
*/ |
2397
|
|
|
public function testMultischemaFixtureWithThreeDatabases() { |
2398
|
|
|
$config = ConnectionManager::enumConnectionObjects(); |
2399
|
|
|
$this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with Sqlite.'); |
2400
|
|
|
$this->skipIf( |
2401
|
|
|
!isset($config['test']) || !isset($config['test2']) || !isset($config['test_database_three']), |
2402
|
|
|
'Primary, secondary, and tertiary test databases not configured, skipping test. To run this test define $test, $test2, and $test_database_three in your database configuration.' |
2403
|
|
|
); |
2404
|
|
|
|
2405
|
|
|
$this->loadFixtures('Player', 'Guild', 'GuildsPlayer', 'Armor', 'ArmorsPlayer'); |
2406
|
|
|
|
2407
|
|
|
$Player = ClassRegistry::init('Player'); |
2408
|
|
|
$Player->bindModel(array( |
2409
|
|
|
'hasAndBelongsToMany' => array( |
2410
|
|
|
'Armor' => array( |
2411
|
|
|
'with' => 'ArmorsPlayer', |
2412
|
|
|
), |
2413
|
|
|
), |
2414
|
|
|
), false); |
2415
|
|
|
$this->assertEquals('test', $Player->useDbConfig); |
2416
|
|
|
$this->assertEquals('test', $Player->Guild->useDbConfig); |
2417
|
|
|
$this->assertEquals('test2', $Player->Guild->GuildsPlayer->useDbConfig); |
2418
|
|
|
$this->assertEquals('test2', $Player->GuildsPlayer->useDbConfig); |
2419
|
|
|
$this->assertEquals('test2', $Player->Armor->useDbConfig); |
2420
|
|
|
$this->assertEquals('test_database_three', $Player->Armor->ArmorsPlayer->useDbConfig); |
2421
|
|
|
$this->assertEquals('test', $Player->getDataSource()->configKeyName); |
2422
|
|
|
$this->assertEquals('test', $Player->Guild->getDataSource()->configKeyName); |
2423
|
|
|
$this->assertEquals('test2', $Player->GuildsPlayer->getDataSource()->configKeyName); |
2424
|
|
|
$this->assertEquals('test2', $Player->Armor->getDataSource()->configKeyName); |
2425
|
|
|
$this->assertEquals('test_database_three', $Player->Armor->ArmorsPlayer->getDataSource()->configKeyName); |
2426
|
|
|
|
2427
|
|
|
$players = $Player->find('all', array('recursive' => -1)); |
2428
|
|
|
$guilds = $Player->Guild->find('all', array('recursive' => -1)); |
2429
|
|
|
$guildsPlayers = $Player->GuildsPlayer->find('all', array('recursive' => -1)); |
2430
|
|
|
$armorsPlayers = $Player->ArmorsPlayer->find('all', array('recursive' => -1)); |
2431
|
|
|
|
2432
|
|
|
$this->assertEquals(true, count($players) > 1); |
2433
|
|
|
$this->assertEquals(true, count($guilds) > 1); |
2434
|
|
|
$this->assertEquals(true, count($guildsPlayers) > 1); |
2435
|
|
|
$this->assertEquals(true, count($armorsPlayers) > 1); |
2436
|
|
|
} |
2437
|
|
|
|
2438
|
|
|
/** |
2439
|
|
|
* Tests that calling schema() on a model that is not supposed to use a table |
2440
|
|
|
* does not trigger any calls on any datasource |
2441
|
|
|
* |
2442
|
|
|
* @return void |
2443
|
|
|
*/ |
2444
|
|
|
public function testSchemaNoDB() { |
2445
|
|
|
$model = $this->getMock('Article', array('getDataSource')); |
2446
|
|
|
$model->useTable = false; |
2447
|
|
|
$model->expects($this->never())->method('getDataSource'); |
2448
|
|
|
$this->assertEmpty($model->schema()); |
2449
|
|
|
} |
2450
|
|
|
} |
2451
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: