Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 1
<?php
2
/**
3
 * DboSourceTest 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.Datasource
15
 * @since         CakePHP(tm) v 1.2.0.4206
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('Model', 'Model');
20
App::uses('AppModel', 'Model');
21
App::uses('DataSource', 'Model/Datasource');
22
App::uses('DboSource', 'Model/Datasource');
23
App::uses('DboTestSource', 'Model/Datasource');
24
App::uses('DboSecondTestSource', 'Model/Datasource');
25
App::uses('MockDataSource', 'Model/Datasource');
26
 
27
require_once dirname(dirname(__FILE__)) . DS . 'models.php';
28
 
29
/**
30
 * Class MockPDO
31
 *
32
 * @package       Cake.Test.Case.Model.Datasource
33
 */
34
class MockPDO extends PDO {
35
 
36
/**
37
 * Constructor.
38
 */
39
	public function __construct() {
40
	}
41
 
42
}
43
 
44
/**
45
 * Class MockDataSource
46
 *
47
 * @package       Cake.Test.Case.Model.Datasource
48
 */
49
class MockDataSource extends DataSource {
50
}
51
 
52
/**
53
 * Class DboTestSource
54
 *
55
 * @package       Cake.Test.Case.Model.Datasource
56
 */
57
class DboTestSource extends DboSource {
58
 
59
	public $nestedSupport = false;
60
 
61
	public function connect($config = array()) {
62
		$this->connected = true;
63
	}
64
 
65
	public function mergeAssociation(&$data, &$merge, $association, $type, $selfJoin = false) {
66
		return parent::_mergeAssociation($data, $merge, $association, $type, $selfJoin);
67
	}
68
 
69
	public function setConfig($config = array()) {
70
		$this->config = $config;
71
	}
72
 
73
	public function setConnection($conn) {
74
		$this->_connection = $conn;
75
	}
76
 
77
	public function nestedTransactionSupported() {
78
		return $this->useNestedTransactions && $this->nestedSupport;
79
	}
80
 
81
}
82
 
83
/**
84
 * Class DboSecondTestSource
85
 *
86
 * @package       Cake.Test.Case.Model.Datasource
87
 */
88
class DboSecondTestSource extends DboSource {
89
 
90
	public $startQuote = '_';
91
 
92
	public $endQuote = '_';
93
 
94
	public function connect($config = array()) {
95
		$this->connected = true;
96
	}
97
 
98
	public function mergeAssociation(&$data, &$merge, $association, $type, $selfJoin = false) {
99
		return parent::_mergeAssociation($data, $merge, $association, $type, $selfJoin);
100
	}
101
 
102
	public function setConfig($config = array()) {
103
		$this->config = $config;
104
	}
105
 
106
	public function setConnection($conn) {
107
		$this->_connection = $conn;
108
	}
109
 
110
}
111
 
112
/**
113
 * DboSourceTest class
114
 *
115
 * @package       Cake.Test.Case.Model.Datasource
116
 */
117
class DboSourceTest extends CakeTestCase {
118
 
119
/**
120
 * autoFixtures property
121
 *
122
 * @var bool
123
 */
124
	public $autoFixtures = false;
125
 
126
/**
127
 * fixtures property
128
 *
129
 * @var array
130
 */
131
	public $fixtures = array(
132
		'core.apple', 'core.article', 'core.articles_tag', 'core.attachment', 'core.comment',
133
		'core.sample', 'core.tag', 'core.user', 'core.post', 'core.author', 'core.data_test'
134
	);
135
 
136
/**
137
 * setUp method
138
 *
139
 * @return void
140
 */
141
	public function setUp() {
142
		parent::setUp();
143
 
144
		$this->testDb = new DboTestSource();
145
		$this->testDb->cacheSources = false;
146
		$this->testDb->startQuote = '`';
147
		$this->testDb->endQuote = '`';
148
 
149
		$this->Model = new TestModel();
150
	}
151
 
152
/**
153
 * tearDown method
154
 *
155
 * @return void
156
 */
157
	public function tearDown() {
158
		parent::tearDown();
159
		unset($this->Model);
160
	}
161
 
162
/**
163
 * test that booleans and null make logical condition strings.
164
 *
165
 * @return void
166
 */
167
	public function testBooleanNullConditionsParsing() {
168
		$result = $this->testDb->conditions(true);
169
		$this->assertEquals(' WHERE 1 = 1', $result, 'true conditions failed %s');
170
 
171
		$result = $this->testDb->conditions(false);
172
		$this->assertEquals(' WHERE 0 = 1', $result, 'false conditions failed %s');
173
 
174
		$result = $this->testDb->conditions(null);
175
		$this->assertEquals(' WHERE 1 = 1', $result, 'null conditions failed %s');
176
 
177
		$result = $this->testDb->conditions(array());
178
		$this->assertEquals(' WHERE 1 = 1', $result, 'array() conditions failed %s');
179
 
180
		$result = $this->testDb->conditions('');
181
		$this->assertEquals(' WHERE 1 = 1', $result, '"" conditions failed %s');
182
 
183
		$result = $this->testDb->conditions(' ', '"  " conditions failed %s');
184
		$this->assertEquals(' WHERE 1 = 1', $result);
185
	}
186
 
187
/**
188
 * test that booleans work on empty set.
189
 *
190
 * @return void
191
 */
192
	public function testBooleanEmptyConditionsParsing() {
193
		$result = $this->testDb->conditions(array('OR' => array()));
194
		$this->assertEquals(' WHERE  1 = 1', $result, 'empty conditions failed');
195
 
196
		$result = $this->testDb->conditions(array('OR' => array('OR' => array())));
197
		$this->assertEquals(' WHERE  1 = 1', $result, 'nested empty conditions failed');
198
	}
199
 
200
/**
201
 * test that order() will accept objects made from DboSource::expression
202
 *
203
 * @return void
204
 */
205
	public function testOrderWithExpression() {
206
		$expression = $this->testDb->expression("CASE Sample.id WHEN 1 THEN 'Id One' ELSE 'Other Id' END AS case_col");
207
		$result = $this->testDb->order($expression);
208
		$expected = " ORDER BY CASE Sample.id WHEN 1 THEN 'Id One' ELSE 'Other Id' END AS case_col";
209
		$this->assertEquals($expected, $result);
210
	}
211
 
212
/**
213
 * testMergeAssociations method
214
 *
215
 * @return void
216
 */
217
	public function testMergeAssociations() {
218
		$data = array('Article2' => array(
219
				'id' => '1', 'user_id' => '1', 'title' => 'First Article',
220
				'body' => 'First Article Body', 'published' => 'Y',
221
				'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
222
		));
223
		$merge = array('Topic' => array(array(
224
			'id' => '1', 'topic' => 'Topic', 'created' => '2007-03-17 01:16:23',
225
			'updated' => '2007-03-17 01:18:31'
226
		)));
227
		$expected = array(
228
			'Article2' => array(
229
				'id' => '1', 'user_id' => '1', 'title' => 'First Article',
230
				'body' => 'First Article Body', 'published' => 'Y',
231
				'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
232
			),
233
			'Topic' => array(
234
				'id' => '1', 'topic' => 'Topic', 'created' => '2007-03-17 01:16:23',
235
				'updated' => '2007-03-17 01:18:31'
236
			)
237
		);
238
		$this->testDb->mergeAssociation($data, $merge, 'Topic', 'hasOne');
239
		$this->assertEquals($expected, $data);
240
 
241
		$data = array('Article2' => array(
242
				'id' => '1', 'user_id' => '1', 'title' => 'First Article',
243
				'body' => 'First Article Body', 'published' => 'Y',
244
				'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
245
		));
246
		$merge = array('User2' => array(array(
247
			'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
248
			'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
249
		)));
250
 
251
		$expected = array(
252
			'Article2' => array(
253
				'id' => '1', 'user_id' => '1', 'title' => 'First Article',
254
				'body' => 'First Article Body', 'published' => 'Y',
255
				'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
256
			),
257
			'User2' => array(
258
				'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
259
			)
260
		);
261
		$this->testDb->mergeAssociation($data, $merge, 'User2', 'belongsTo');
262
		$this->assertEquals($expected, $data);
263
 
264
		$data = array(
265
			'Article2' => array(
266
				'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
267
			)
268
		);
269
		$merge = array(array('Comment' => false));
270
		$expected = array(
271
			'Article2' => array(
272
				'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
273
			),
274
			'Comment' => array()
275
		);
276
		$this->testDb->mergeAssociation($data, $merge, 'Comment', 'hasMany');
277
		$this->assertEquals($expected, $data);
278
 
279
		$data = array(
280
			'Article' => array(
281
				'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
282
			)
283
		);
284
		$merge = array(
285
			array(
286
				'Comment' => array(
287
					'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
288
				)
289
			),
290
			array(
291
				'Comment' => array(
292
					'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
293
				)
294
			)
295
		);
296
		$expected = array(
297
			'Article' => array(
298
				'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
299
			),
300
			'Comment' => array(
301
				array(
302
					'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
303
				),
304
				array(
305
					'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
306
				)
307
			)
308
		);
309
		$this->testDb->mergeAssociation($data, $merge, 'Comment', 'hasMany');
310
		$this->assertEquals($expected, $data);
311
 
312
		$data = array(
313
			'Article' => array(
314
				'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
315
			)
316
		);
317
		$merge = array(
318
			array(
319
				'Comment' => array(
320
					'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
321
				),
322
				'User2' => array(
323
					'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
324
				)
325
			),
326
			array(
327
				'Comment' => array(
328
					'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
329
				),
330
				'User2' => array(
331
					'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
332
				)
333
			)
334
		);
335
		$expected = array(
336
			'Article' => array(
337
				'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
338
			),
339
			'Comment' => array(
340
				array(
341
					'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31',
342
					'User2' => array(
343
						'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
344
					)
345
				),
346
				array(
347
					'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31',
348
					'User2' => array(
349
						'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
350
					)
351
				)
352
			)
353
		);
354
		$this->testDb->mergeAssociation($data, $merge, 'Comment', 'hasMany');
355
		$this->assertEquals($expected, $data);
356
 
357
		$data = array(
358
			'Article' => array(
359
				'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
360
			)
361
		);
362
		$merge = array(
363
			array(
364
				'Comment' => array(
365
					'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
366
				),
367
				'User2' => array(
368
					'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
369
				),
370
				'Tag' => array(
371
					array('id' => 1, 'tag' => 'Tag 1'),
372
					array('id' => 2, 'tag' => 'Tag 2')
373
				)
374
			),
375
			array(
376
				'Comment' => array(
377
					'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
378
				),
379
				'User2' => array(
380
					'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
381
				),
382
				'Tag' => array()
383
			)
384
		);
385
		$expected = array(
386
			'Article' => array(
387
				'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
388
			),
389
			'Comment' => array(
390
				array(
391
					'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31',
392
					'User2' => array(
393
						'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
394
					),
395
					'Tag' => array(
396
						array('id' => 1, 'tag' => 'Tag 1'),
397
						array('id' => 2, 'tag' => 'Tag 2')
398
					)
399
				),
400
				array(
401
					'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31',
402
					'User2' => array(
403
						'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
404
					),
405
					'Tag' => array()
406
				)
407
			)
408
		);
409
		$this->testDb->mergeAssociation($data, $merge, 'Comment', 'hasMany');
410
		$this->assertEquals($expected, $data);
411
 
412
		$data = array(
413
			'Article' => array(
414
				'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
415
			)
416
		);
417
		$merge = array(
418
			array(
419
				'Tag' => array(
420
					'id' => '1', 'tag' => 'Tag 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
421
				)
422
			),
423
			array(
424
				'Tag' => array(
425
					'id' => '2', 'tag' => 'Tag 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
426
				)
427
			),
428
			array(
429
				'Tag' => array(
430
					'id' => '3', 'tag' => 'Tag 3', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
431
				)
432
			)
433
		);
434
		$expected = array(
435
			'Article' => array(
436
				'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
437
			),
438
			'Tag' => array(
439
				array(
440
					'id' => '1', 'tag' => 'Tag 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
441
				),
442
				array(
443
					'id' => '2', 'tag' => 'Tag 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
444
				),
445
				array(
446
					'id' => '3', 'tag' => 'Tag 3', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
447
				)
448
			)
449
		);
450
		$this->testDb->mergeAssociation($data, $merge, 'Tag', 'hasAndBelongsToMany');
451
		$this->assertEquals($expected, $data);
452
 
453
		$data = array(
454
			'Article' => array(
455
				'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
456
			)
457
		);
458
		$merge = array(
459
			array(
460
				'Tag' => array(
461
					'id' => '1', 'tag' => 'Tag 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
462
				)
463
			),
464
			array(
465
				'Tag' => array(
466
					'id' => '2', 'tag' => 'Tag 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
467
				)
468
			),
469
			array(
470
				'Tag' => array(
471
					'id' => '3', 'tag' => 'Tag 3', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
472
				)
473
			)
474
		);
475
		$expected = array(
476
			'Article' => array(
477
				'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
478
			),
479
			'Tag' => array('id' => '1', 'tag' => 'Tag 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31')
480
		);
481
		$this->testDb->mergeAssociation($data, $merge, 'Tag', 'hasOne');
482
		$this->assertEquals($expected, $data);
483
	}
484
 
485
/**
486
 * testMagicMethodQuerying method
487
 *
488
 * @return void
489
 */
490
	public function testMagicMethodQuerying() {
491
		$result = $this->db->query('findByFieldName', array('value'), $this->Model);
492
		$expected = array('first', array(
493
			'conditions' => array('TestModel.field_name' => 'value'),
494
			'fields' => null, 'order' => null, 'recursive' => null
495
		));
496
		$this->assertEquals($expected, $result);
497
 
498
		$result = $this->db->query('findByFindBy', array('value'), $this->Model);
499
		$expected = array('first', array(
500
			'conditions' => array('TestModel.find_by' => 'value'),
501
			'fields' => null, 'order' => null, 'recursive' => null
502
		));
503
		$this->assertEquals($expected, $result);
504
 
505
		$result = $this->db->query('findAllByFieldName', array('value'), $this->Model);
506
		$expected = array('all', array(
507
			'conditions' => array('TestModel.field_name' => 'value'),
508
			'fields' => null, 'order' => null, 'limit' => null,
509
			'page' => null, 'recursive' => null
510
		));
511
		$this->assertEquals($expected, $result);
512
 
513
		$result = $this->db->query('findAllById', array('a'), $this->Model);
514
		$expected = array('all', array(
515
			'conditions' => array('TestModel.id' => 'a'),
516
			'fields' => null, 'order' => null, 'limit' => null,
517
			'page' => null, 'recursive' => null
518
		));
519
		$this->assertEquals($expected, $result);
520
 
521
		$result = $this->db->query('findByFieldName', array(array('value1', 'value2', 'value3')), $this->Model);
522
		$expected = array('first', array(
523
			'conditions' => array('TestModel.field_name' => array('value1', 'value2', 'value3')),
524
			'fields' => null, 'order' => null, 'recursive' => null
525
		));
526
		$this->assertEquals($expected, $result);
527
 
528
		$result = $this->db->query('findByFieldName', array(null), $this->Model);
529
		$expected = array('first', array(
530
			'conditions' => array('TestModel.field_name' => null),
531
			'fields' => null, 'order' => null, 'recursive' => null
532
		));
533
		$this->assertEquals($expected, $result);
534
 
535
		$result = $this->db->query('findByFieldName', array('= a'), $this->Model);
536
		$expected = array('first', array(
537
			'conditions' => array('TestModel.field_name' => '= a'),
538
			'fields' => null, 'order' => null, 'recursive' => null
539
		));
540
		$this->assertEquals($expected, $result);
541
 
542
		$result = $this->db->query('findByFieldName', array(), $this->Model);
543
		$expected = false;
544
		$this->assertEquals($expected, $result);
545
	}
546
 
547
/**
548
 * @expectedException PDOException
549
 * @return void
550
 */
551
	public function testDirectCallThrowsException() {
552
		$this->db->query('directCall', array(), $this->Model);
553
	}
554
 
555
/**
556
 * testValue method
557
 *
558
 * @return void
559
 */
560
	public function testValue() {
561
		if ($this->db instanceof Sqlserver) {
562
			$this->markTestSkipped('Cannot run this test with SqlServer');
563
		}
564
		$result = $this->db->value('{$__cakeForeignKey__$}');
565
		$this->assertEquals('{$__cakeForeignKey__$}', $result);
566
 
567
		$result = $this->db->value(array('first', 2, 'third'));
568
		$expected = array('\'first\'', 2, '\'third\'');
569
		$this->assertEquals($expected, $result);
570
	}
571
 
572
/**
573
 * Tests if the connection can be re-established and that the new (optional) config is set.
574
 *
575
 * @return void
576
 */
577
	public function testReconnect() {
578
		$this->testDb->reconnect(array('prefix' => 'foo'));
579
		$this->assertTrue($this->testDb->connected);
580
		$this->assertEquals('foo', $this->testDb->config['prefix']);
581
	}
582
 
583
/**
584
 * testName method
585
 *
586
 * @return void
587
 */
588
	public function testName() {
589
		$result = $this->testDb->name('name');
590
		$expected = '`name`';
591
		$this->assertEquals($expected, $result);
592
 
593
		$result = $this->testDb->name(array('name', 'Model.*'));
594
		$expected = array('`name`', '`Model`.*');
595
		$this->assertEquals($expected, $result);
596
 
597
		$result = $this->testDb->name('MTD()');
598
		$expected = 'MTD()';
599
		$this->assertEquals($expected, $result);
600
 
601
		$result = $this->testDb->name('(sm)');
602
		$expected = '(sm)';
603
		$this->assertEquals($expected, $result);
604
 
605
		$result = $this->testDb->name('name AS x');
606
		$expected = '`name` AS `x`';
607
		$this->assertEquals($expected, $result);
608
 
609
		$result = $this->testDb->name('Model.name AS x');
610
		$expected = '`Model`.`name` AS `x`';
611
		$this->assertEquals($expected, $result);
612
 
613
		$result = $this->testDb->name('Function(Something.foo)');
614
		$expected = 'Function(`Something`.`foo`)';
615
		$this->assertEquals($expected, $result);
616
 
617
		$result = $this->testDb->name('Function(SubFunction(Something.foo))');
618
		$expected = 'Function(SubFunction(`Something`.`foo`))';
619
		$this->assertEquals($expected, $result);
620
 
621
		$result = $this->testDb->name('Function(Something.foo) AS x');
622
		$expected = 'Function(`Something`.`foo`) AS `x`';
623
		$this->assertEquals($expected, $result);
624
 
625
		$result = $this->testDb->name('I18n__title__pt-br.locale');
626
		$expected = '`I18n__title__pt-br`.`locale`';
627
		$this->assertEquals($expected, $result);
628
 
629
		$result = $this->testDb->name('name-with-minus');
630
		$expected = '`name-with-minus`';
631
		$this->assertEquals($expected, $result);
632
 
633
		$result = $this->testDb->name(array('my-name', 'Foo-Model.*'));
634
		$expected = array('`my-name`', '`Foo-Model`.*');
635
		$this->assertEquals($expected, $result);
636
 
637
		$result = $this->testDb->name(array('Team.P%', 'Team.G/G'));
638
		$expected = array('`Team`.`P%`', '`Team`.`G/G`');
639
		$this->assertEquals($expected, $result);
640
 
641
		$result = $this->testDb->name('Model.name as y');
642
		$expected = '`Model`.`name` AS `y`';
643
		$this->assertEquals($expected, $result);
644
	}
645
 
646
/**
647
 * test that cacheMethod works as expected
648
 *
649
 * @return void
650
 */
651
	public function testCacheMethod() {
652
		$this->testDb->cacheMethods = true;
653
		$result = $this->testDb->cacheMethod('name', 'some-key', 'stuff');
654
		$this->assertEquals('stuff', $result);
655
 
656
		$result = $this->testDb->cacheMethod('name', 'some-key');
657
		$this->assertEquals('stuff', $result);
658
 
659
		$result = $this->testDb->cacheMethod('conditions', 'some-key');
660
		$this->assertNull($result);
661
 
662
		$result = $this->testDb->cacheMethod('name', 'other-key');
663
		$this->assertNull($result);
664
 
665
		$this->testDb->cacheMethods = false;
666
		$result = $this->testDb->cacheMethod('name', 'some-key', 'stuff');
667
		$this->assertEquals('stuff', $result);
668
 
669
		$result = $this->testDb->cacheMethod('name', 'some-key');
670
		$this->assertNull($result);
671
	}
672
 
673
/**
674
 * Test that rare collisions do not happen with method caching
675
 *
676
 * @return void
677
 */
678
	public function testNameMethodCacheCollisions() {
679
		$this->testDb->cacheMethods = true;
680
		$this->testDb->flushMethodCache();
681
		$this->testDb->name('Model.fieldlbqndkezcoapfgirmjsh');
682
		$result = $this->testDb->name('Model.fieldkhdfjmelarbqnzsogcpi');
683
		$expected = '`Model`.`fieldkhdfjmelarbqnzsogcpi`';
684
		$this->assertEquals($expected, $result);
685
	}
686
 
687
/**
688
 * Test that flushMethodCache works as expected
689
 *
690
 * @return void
691
 */
692
	public function testFlushMethodCache() {
693
		$this->testDb->cacheMethods = true;
694
		$this->testDb->cacheMethod('name', 'some-key', 'stuff');
695
 
696
		Cache::write('method_cache', DboTestSource::$methodCache, '_cake_core_');
697
 
698
		$this->testDb->flushMethodCache();
699
		$result = $this->testDb->cacheMethod('name', 'some-key');
700
		$this->assertNull($result);
701
	}
702
 
703
/**
704
 * testLog method
705
 *
706
 * @outputBuffering enabled
707
 * @return void
708
 */
709
	public function testLog() {
710
		$this->testDb->logQuery('Query 1');
711
		$this->testDb->logQuery('Query 2');
712
 
713
		$log = $this->testDb->getLog(false, false);
714
		$result = Hash::extract($log['log'], '{n}.query');
715
		$expected = array('Query 1', 'Query 2');
716
		$this->assertEquals($expected, $result);
717
 
718
		$oldDebug = Configure::read('debug');
719
		Configure::write('debug', 2);
720
		ob_start();
721
		$this->testDb->showLog();
722
		$contents = ob_get_clean();
723
 
724
		$this->assertRegExp('/Query 1/s', $contents);
725
		$this->assertRegExp('/Query 2/s', $contents);
726
 
727
		ob_start();
728
		$this->testDb->showLog(true);
729
		$contents = ob_get_clean();
730
 
731
		$this->assertRegExp('/Query 1/s', $contents);
732
		$this->assertRegExp('/Query 2/s', $contents);
733
 
734
		Configure::write('debug', $oldDebug);
735
	}
736
 
737
/**
738
 * test getting the query log as an array.
739
 *
740
 * @return void
741
 */
742
	public function testGetLog() {
743
		$this->testDb->logQuery('Query 1');
744
		$this->testDb->logQuery('Query 2');
745
 
746
		$log = $this->testDb->getLog();
747
		$expected = array('query' => 'Query 1', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => '');
748
 
749
		$this->assertEquals($expected, $log['log'][0]);
750
		$expected = array('query' => 'Query 2', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => '');
751
		$this->assertEquals($expected, $log['log'][1]);
752
		$expected = array('query' => 'Error 1', 'affected' => '', 'numRows' => '', 'took' => '');
753
	}
754
 
755
/**
756
 * test getting the query log as an array, setting bind params.
757
 *
758
 * @return void
759
 */
760
	public function testGetLogParams() {
761
		$this->testDb->logQuery('Query 1', array(1, 2, 'abc'));
762
		$this->testDb->logQuery('Query 2', array('field1' => 1, 'field2' => 'abc'));
763
 
764
		$log = $this->testDb->getLog();
765
		$expected = array('query' => 'Query 1', 'params' => array(1, 2, 'abc'), 'affected' => '', 'numRows' => '', 'took' => '');
766
		$this->assertEquals($expected, $log['log'][0]);
767
		$expected = array('query' => 'Query 2', 'params' => array('field1' => 1, 'field2' => 'abc'), 'affected' => '', 'numRows' => '', 'took' => '');
768
		$this->assertEquals($expected, $log['log'][1]);
769
	}
770
 
771
/**
772
 * test that query() returns boolean values from operations like CREATE TABLE
773
 *
774
 * @return void
775
 */
776
	public function testFetchAllBooleanReturns() {
777
		$name = $this->db->fullTableName('test_query');
778
		$query = "CREATE TABLE {$name} (name varchar(10));";
779
		$result = $this->db->query($query);
780
		$this->assertTrue($result, 'Query did not return a boolean');
781
 
782
		$query = "DROP TABLE {$name};";
783
		$result = $this->db->query($query);
784
		$this->assertTrue($result, 'Query did not return a boolean');
785
	}
786
 
787
/**
788
 * test order to generate query order clause for virtual fields
789
 *
790
 * @return void
791
 */
792
	public function testVirtualFieldsInOrder() {
793
		$Article = ClassRegistry::init('Article');
794
		$Article->virtualFields = array(
795
			'this_moment' => 'NOW()',
796
			'two' => '1 + 1',
797
		);
798
		$order = array('two', 'this_moment');
799
		$result = $this->db->order($order, 'ASC', $Article);
800
		$expected = ' ORDER BY (1 + 1) ASC, (NOW()) ASC';
801
		$this->assertEquals($expected, $result);
802
 
803
		$order = array('Article.two', 'Article.this_moment');
804
		$result = $this->db->order($order, 'ASC', $Article);
805
		$expected = ' ORDER BY (1 + 1) ASC, (NOW()) ASC';
806
		$this->assertEquals($expected, $result);
807
	}
808
 
809
/**
810
 * test the permutations of fullTableName()
811
 *
812
 * @return void
813
 */
814
	public function testFullTablePermutations() {
815
		$Article = ClassRegistry::init('Article');
816
		$result = $this->testDb->fullTableName($Article, false, false);
817
		$this->assertEquals('articles', $result);
818
 
819
		$Article->tablePrefix = 'tbl_';
820
		$result = $this->testDb->fullTableName($Article, false, false);
821
		$this->assertEquals('tbl_articles', $result);
822
 
823
		$Article->useTable = $Article->table = 'with spaces';
824
		$Article->tablePrefix = '';
825
		$result = $this->testDb->fullTableName($Article, true, false);
826
		$this->assertEquals('`with spaces`', $result);
827
 
828
		$this->loadFixtures('Article');
829
		$Article->useTable = $Article->table = 'articles';
830
		$Article->setDataSource('test');
831
		$testdb = $Article->getDataSource();
832
		$result = $testdb->fullTableName($Article, false, true);
833
		$this->assertEquals($testdb->getSchemaName() . '.articles', $result);
834
 
835
		// tests for empty schemaName
836
		$noschema = ConnectionManager::create('noschema', array(
837
			'datasource' => 'DboTestSource'
838
			));
839
		$Article->setDataSource('noschema');
840
		$Article->schemaName = null;
841
		$result = $noschema->fullTableName($Article, false, true);
842
		$this->assertEquals('articles', $result);
843
 
844
		$this->testDb->config['prefix'] = 't_';
845
		$result = $this->testDb->fullTableName('post_tag', false, false);
846
		$this->assertEquals('t_post_tag', $result);
847
	}
848
 
849
/**
850
 * test that read() only calls queryAssociation on db objects when the method is defined.
851
 *
852
 * @return void
853
 */
854
	public function testReadOnlyCallingQueryAssociationWhenDefined() {
855
		$this->loadFixtures('Article', 'User', 'ArticlesTag', 'Tag');
856
		ConnectionManager::create('test_no_queryAssociation', array(
857
			'datasource' => 'MockDataSource'
858
		));
859
		$Article = ClassRegistry::init('Article');
860
		$Article->Comment->useDbConfig = 'test_no_queryAssociation';
861
		$result = $Article->find('all');
862
		$this->assertTrue(is_array($result));
863
	}
864
 
865
/**
866
 * test that queryAssociation() reuse already joined data for 'belongsTo' and 'hasOne' associations
867
 * instead of running unneeded queries for each record
868
 *
869
 * @return void
870
 */
871
	public function testQueryAssociationUnneededQueries() {
872
		$this->loadFixtures('Article', 'User', 'Comment', 'Attachment', 'Tag', 'ArticlesTag');
873
		$Comment = ClassRegistry::init('Comment');
874
 
875
		$fullDebug = $this->db->fullDebug;
876
		$this->db->fullDebug = true;
877
 
878
		$Comment->find('all', array('recursive' => 2)); // ensure Model descriptions are saved
879
		$this->db->getLog();
880
 
881
		// case: Comment belongsTo User and Article
882
		$Comment->unbindModel(array(
883
			'hasOne' => array('Attachment')
884
		));
885
		$Comment->Article->unbindModel(array(
886
			'belongsTo' => array('User'),
887
			'hasMany' => array('Comment'),
888
			'hasAndBelongsToMany' => array('Tag')
889
		));
890
		$Comment->find('all', array('recursive' => 2));
891
		$log = $this->db->getLog();
892
		$this->assertEquals(1, count($log['log']));
893
 
894
		// case: Comment belongsTo Article, Article belongsTo User
895
		$Comment->unbindModel(array(
896
			'belongsTo' => array('User'),
897
			'hasOne' => array('Attachment')
898
		));
899
		$Comment->Article->unbindModel(array(
900
			'hasMany' => array('Comment'),
901
			'hasAndBelongsToMany' => array('Tag'),
902
		));
903
		$Comment->find('all', array('recursive' => 2));
904
		$log = $this->db->getLog();
905
		$this->assertEquals(7, count($log['log']));
906
 
907
		// case: Comment hasOne Attachment
908
		$Comment->unbindModel(array(
909
			'belongsTo' => array('Article', 'User'),
910
		));
911
		$Comment->Attachment->unbindModel(array(
912
			'belongsTo' => array('Comment'),
913
		));
914
		$Comment->find('all', array('recursive' => 2));
915
		$log = $this->db->getLog();
916
		$this->assertEquals(1, count($log['log']));
917
 
918
		$this->db->fullDebug = $fullDebug;
919
	}
920
 
921
/**
922
 * Tests that generation association queries without LinkModel still works.
923
 * Mainly BC.
924
 *
925
 * @return void
926
 */
927
	public function testGenerateAssociationQuery() {
928
		$this->loadFixtures('Article');
929
		$Article = ClassRegistry::init('Article');
930
 
931
		$queryData = array(
932
			'conditions' => array(
933
				'Article.id' => 1
934
			),
935
			'fields' => array(
936
				'Article.id',
937
				'Article.title',
938
			),
939
			'joins' => array(),
940
			'limit' => 2,
941
			'offset' => 2,
942
			'order' => array('title'),
943
			'page' => 2,
944
			'group' => null,
945
			'callbacks' => 1
946
		);
947
 
948
		$result = $this->db->generateAssociationQuery($Article, null, null, null, null, $queryData, false);
949
		$this->assertContains('SELECT', $result);
950
		$this->assertContains('FROM', $result);
951
		$this->assertContains('WHERE', $result);
952
		$this->assertContains('ORDER', $result);
953
	}
954
 
955
/**
956
 * test that fields() is using methodCache()
957
 *
958
 * @return void
959
 */
960
	public function testFieldsUsingMethodCache() {
961
		$this->testDb->cacheMethods = false;
962
		DboTestSource::$methodCache = array();
963
 
964
		$Article = ClassRegistry::init('Article');
965
		$this->testDb->fields($Article, null, array('title', 'body', 'published'));
966
		$this->assertTrue(empty(DboTestSource::$methodCache['fields']), 'Cache not empty');
967
	}
968
 
969
/**
970
 * test that fields() method cache detects datasource changes
971
 *
972
 * @return void
973
 */
974
	public function testFieldsCacheKeyWithDatasourceChange() {
975
		ConnectionManager::create('firstschema', array(
976
			'datasource' => 'DboTestSource'
977
		));
978
		ConnectionManager::create('secondschema', array(
979
			'datasource' => 'DboSecondTestSource'
980
		));
981
		Cache::delete('method_cache', '_cake_core_');
982
		DboTestSource::$methodCache = array();
983
		$Article = ClassRegistry::init('Article');
984
 
985
		$Article->setDataSource('firstschema');
986
		$ds = $Article->getDataSource();
987
		$ds->cacheMethods = true;
988
		$first = $ds->fields($Article, null, array('title', 'body', 'published'));
989
 
990
		$Article->setDataSource('secondschema');
991
		$ds = $Article->getDataSource();
992
		$ds->cacheMethods = true;
993
		$second = $ds->fields($Article, null, array('title', 'body', 'published'));
994
 
995
		$this->assertNotEquals($first, $second);
996
		$this->assertEquals(2, count(DboTestSource::$methodCache['fields']));
997
	}
998
 
999
/**
1000
 * test that fields() method cache detects schema name changes
1001
 *
1002
 * @return void
1003
 */
1004
	public function testFieldsCacheKeyWithSchemanameChange() {
1005
		if ($this->db instanceof Postgres || $this->db instanceof Sqlserver) {
1006
			$this->markTestSkipped('Cannot run this test with SqlServer or Postgres');
1007
		}
1008
		Cache::delete('method_cache', '_cake_core_');
1009
		DboSource::$methodCache = array();
1010
		$Article = ClassRegistry::init('Article');
1011
 
1012
		$ds = $Article->getDataSource();
1013
		$ds->cacheMethods = true;
1014
		$first = $ds->fields($Article);
1015
 
1016
		$Article->schemaName = 'secondSchema';
1017
		$ds = $Article->getDataSource();
1018
		$ds->cacheMethods = true;
1019
		$second = $ds->fields($Article);
1020
 
1021
		$this->assertEquals(2, count(DboSource::$methodCache['fields']));
1022
	}
1023
 
1024
/**
1025
 * Test that group works without a model
1026
 *
1027
 * @return void
1028
 */
1029
	public function testGroupNoModel() {
1030
		$result = $this->db->group('created');
1031
		$this->assertEquals(' GROUP BY created', $result);
1032
	}
1033
 
1034
/**
1035
 * Test getting the last error.
1036
 *
1037
 * @return void
1038
 */
1039
	public function testLastError() {
1040
		$stmt = $this->getMock('PDOStatement');
1041
		$stmt->expects($this->any())
1042
			->method('errorInfo')
1043
			->will($this->returnValue(array('', 'something', 'bad')));
1044
 
1045
		$result = $this->db->lastError($stmt);
1046
		$expected = 'something: bad';
1047
		$this->assertEquals($expected, $result);
1048
	}
1049
 
1050
/**
1051
 * Tests that transaction commands are logged
1052
 *
1053
 * @return void
1054
 */
1055
	public function testTransactionLogging() {
1056
		$conn = $this->getMock('MockPDO');
1057
		$db = new DboTestSource();
1058
		$db->setConnection($conn);
1059
		$conn->expects($this->exactly(2))->method('beginTransaction')
1060
			->will($this->returnValue(true));
1061
		$conn->expects($this->once())->method('commit')->will($this->returnValue(true));
1062
		$conn->expects($this->once())->method('rollback')->will($this->returnValue(true));
1063
 
1064
		$db->begin();
1065
		$log = $db->getLog();
1066
		$expected = array('query' => 'BEGIN', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => '');
1067
		$this->assertEquals($expected, $log['log'][0]);
1068
 
1069
		$db->commit();
1070
		$expected = array('query' => 'COMMIT', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => '');
1071
		$log = $db->getLog();
1072
		$this->assertEquals($expected, $log['log'][0]);
1073
 
1074
		$db->begin();
1075
		$expected = array('query' => 'BEGIN', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => '');
1076
		$log = $db->getLog();
1077
		$this->assertEquals($expected, $log['log'][0]);
1078
 
1079
		$db->rollback();
1080
		$expected = array('query' => 'ROLLBACK', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => '');
1081
		$log = $db->getLog();
1082
		$this->assertEquals($expected, $log['log'][0]);
1083
	}
1084
 
1085
/**
1086
 * Test nested transaction calls
1087
 *
1088
 * @return void
1089
 */
1090
	public function testTransactionNested() {
1091
		$conn = $this->getMock('MockPDO');
1092
		$db = new DboTestSource();
1093
		$db->setConnection($conn);
1094
		$db->useNestedTransactions = true;
1095
		$db->nestedSupport = true;
1096
 
1097
		$conn->expects($this->at(0))->method('beginTransaction')->will($this->returnValue(true));
1098
		$conn->expects($this->at(1))->method('exec')->with($this->equalTo('SAVEPOINT LEVEL1'))->will($this->returnValue(true));
1099
		$conn->expects($this->at(2))->method('exec')->with($this->equalTo('RELEASE SAVEPOINT LEVEL1'))->will($this->returnValue(true));
1100
		$conn->expects($this->at(3))->method('exec')->with($this->equalTo('SAVEPOINT LEVEL1'))->will($this->returnValue(true));
1101
		$conn->expects($this->at(4))->method('exec')->with($this->equalTo('ROLLBACK TO SAVEPOINT LEVEL1'))->will($this->returnValue(true));
1102
		$conn->expects($this->at(5))->method('commit')->will($this->returnValue(true));
1103
 
1104
		$this->_runTransactions($db);
1105
	}
1106
 
1107
/**
1108
 * Test nested transaction calls without support
1109
 *
1110
 * @return void
1111
 */
1112
	public function testTransactionNestedWithoutSupport() {
1113
		$conn = $this->getMock('MockPDO');
1114
		$db = new DboTestSource();
1115
		$db->setConnection($conn);
1116
		$db->useNestedTransactions = true;
1117
		$db->nestedSupport = false;
1118
 
1119
		$conn->expects($this->once())->method('beginTransaction')->will($this->returnValue(true));
1120
		$conn->expects($this->never())->method('exec');
1121
		$conn->expects($this->once())->method('commit')->will($this->returnValue(true));
1122
 
1123
		$this->_runTransactions($db);
1124
	}
1125
 
1126
/**
1127
 * Test nested transaction disabled
1128
 *
1129
 * @return void
1130
 */
1131
	public function testTransactionNestedDisabled() {
1132
		$conn = $this->getMock('MockPDO');
1133
		$db = new DboTestSource();
1134
		$db->setConnection($conn);
1135
		$db->useNestedTransactions = false;
1136
		$db->nestedSupport = true;
1137
 
1138
		$conn->expects($this->once())->method('beginTransaction')->will($this->returnValue(true));
1139
		$conn->expects($this->never())->method('exec');
1140
		$conn->expects($this->once())->method('commit')->will($this->returnValue(true));
1141
 
1142
		$this->_runTransactions($db);
1143
	}
1144
 
1145
/**
1146
 * Nested transaction calls
1147
 *
1148
 * @param DboTestSource $db
1149
 * @return void
1150
 */
1151
	protected function _runTransactions($db) {
1152
		$db->begin();
1153
		$db->begin();
1154
		$db->commit();
1155
		$db->begin();
1156
		$db->rollback();
1157
		$db->commit();
1158
	}
1159
 
1160
/**
1161
 * Test build statement with some fields missing
1162
 *
1163
 * @return void
1164
 */
1165
	public function testBuildStatementDefaults() {
1166
		$conn = $this->getMock('MockPDO', array('quote'));
1167
		$conn->expects($this->at(0))
1168
			->method('quote')
1169
			->will($this->returnValue('foo bar'));
1170
		$db = new DboTestSource();
1171
		$db->setConnection($conn);
1172
		$subQuery = $db->buildStatement(
1173
			array(
1174
				'fields' => array('DISTINCT(AssetsTag.asset_id)'),
1175
				'table' => "assets_tags",
1176
				'alias' => "AssetsTag",
1177
				'conditions' => array("Tag.name" => 'foo bar'),
1178
				'limit' => null,
1179
				'group' => "AssetsTag.asset_id"
1180
			),
1181
			$this->Model
1182
		);
1183
		$expected = 'SELECT DISTINCT(AssetsTag.asset_id) FROM assets_tags AS AssetsTag   WHERE Tag.name = foo bar  GROUP BY AssetsTag.asset_id';
1184
		$this->assertEquals($expected, $subQuery);
1185
	}
1186
 
1187
/**
1188
 * data provider for testBuildJoinStatement
1189
 *
1190
 * @return array
1191
 */
1192
	public static function joinStatements() {
1193
		return array(
1194
			array(array(
1195
				'type' => 'CROSS',
1196
				'alias' => 'PostsTag',
1197
				'table' => 'posts_tags',
1198
				'conditions' => array('1 = 1')
1199
			), 'CROSS JOIN cakephp.posts_tags AS PostsTag'),
1200
			array(array(
1201
				'type' => 'LEFT',
1202
				'alias' => 'PostsTag',
1203
				'table' => 'posts_tags',
1204
				'conditions' => array('PostsTag.post_id = Post.id')
1205
			), 'LEFT JOIN cakephp.posts_tags AS PostsTag ON (PostsTag.post_id = Post.id)'),
1206
			array(array(
1207
				'type' => 'LEFT',
1208
				'alias' => 'Stock',
1209
				'table' => '(SELECT Stock.article_id, sum(quantite) quantite FROM stocks AS Stock GROUP BY Stock.article_id)',
1210
				'conditions' => 'Stock.article_id = Article.id'
1211
			), 'LEFT JOIN (SELECT Stock.article_id, sum(quantite) quantite FROM stocks AS Stock GROUP BY Stock.article_id) AS Stock ON (Stock.article_id = Article.id)')
1212
		);
1213
	}
1214
 
1215
/**
1216
 * Test buildJoinStatement()
1217
 * ensure that schemaName is not added when table value is a subquery
1218
 *
1219
 * @dataProvider joinStatements
1220
 * @return void
1221
 */
1222
	public function testBuildJoinStatement($join, $expected) {
1223
		$db = $this->getMock('DboTestSource', array('getSchemaName'));
1224
		$db->expects($this->any())
1225
			->method('getSchemaName')
1226
			->will($this->returnValue('cakephp'));
1227
		$result = $db->buildJoinStatement($join);
1228
		$this->assertEquals($expected, $result);
1229
	}
1230
 
1231
/**
1232
 * data provider for testBuildJoinStatementWithTablePrefix
1233
 *
1234
 * @return array
1235
 */
1236
	public static function joinStatementsWithPrefix($schema) {
1237
		return array(
1238
			array(array(
1239
				'type' => 'LEFT',
1240
				'alias' => 'PostsTag',
1241
				'table' => 'posts_tags',
1242
				'conditions' => array('PostsTag.post_id = Post.id')
1243
			), 'LEFT JOIN pre_posts_tags AS PostsTag ON (PostsTag.post_id = Post.id)'),
1244
				array(array(
1245
					'type' => 'LEFT',
1246
					'alias' => 'Stock',
1247
					'table' => '(SELECT Stock.article_id, sum(quantite) quantite FROM stocks AS Stock GROUP BY Stock.article_id)',
1248
					'conditions' => 'Stock.article_id = Article.id'
1249
				), 'LEFT JOIN (SELECT Stock.article_id, sum(quantite) quantite FROM stocks AS Stock GROUP BY Stock.article_id) AS Stock ON (Stock.article_id = Article.id)')
1250
			);
1251
	}
1252
 
1253
/**
1254
 * Test buildJoinStatement()
1255
 * ensure that prefix is not added when table value is a subquery
1256
 *
1257
 * @dataProvider joinStatementsWithPrefix
1258
 * @return void
1259
 */
1260
	public function testBuildJoinStatementWithTablePrefix($join, $expected) {
1261
		$db = new DboTestSource();
1262
		$db->config['prefix'] = 'pre_';
1263
		$result = $db->buildJoinStatement($join);
1264
		$this->assertEquals($expected, $result);
1265
	}
1266
 
1267
/**
1268
 * Test conditionKeysToString()
1269
 *
1270
 * @return void
1271
 */
1272
	public function testConditionKeysToString() {
1273
		$Article = ClassRegistry::init('Article');
1274
		$conn = $this->getMock('MockPDO', array('quote'));
1275
		$db = new DboTestSource();
1276
		$db->setConnection($conn);
1277
 
1278
		$conn->expects($this->at(0))
1279
			->method('quote')
1280
			->will($this->returnValue('just text'));
1281
 
1282
		$conditions = array('Article.name' => 'just text');
1283
		$result = $db->conditionKeysToString($conditions, true, $Article);
1284
		$expected = "Article.name = just text";
1285
		$this->assertEquals($expected, $result[0]);
1286
 
1287
		$conn->expects($this->at(0))
1288
			->method('quote')
1289
			->will($this->returnValue('just text'));
1290
		$conn->expects($this->at(1))
1291
			->method('quote')
1292
			->will($this->returnValue('other text'));
1293
 
1294
		$conditions = array('Article.name' => array('just text', 'other text'));
1295
		$result = $db->conditionKeysToString($conditions, true, $Article);
1296
		$expected = "Article.name IN (just text, other text)";
1297
		$this->assertEquals($expected, $result[0]);
1298
	}
1299
 
1300
/**
1301
 * Test conditionKeysToString() with virtual field
1302
 *
1303
 * @return void
1304
 */
1305
	public function testConditionKeysToStringVirtualField() {
1306
		$Article = ClassRegistry::init('Article');
1307
		$Article->virtualFields = array(
1308
			'extra' => 'something virtual'
1309
		);
1310
		$conn = $this->getMock('MockPDO', array('quote'));
1311
		$db = new DboTestSource();
1312
		$db->setConnection($conn);
1313
 
1314
		$conn->expects($this->at(0))
1315
			->method('quote')
1316
			->will($this->returnValue('just text'));
1317
 
1318
		$conditions = array('Article.extra' => 'just text');
1319
		$result = $db->conditionKeysToString($conditions, true, $Article);
1320
		$expected = "(" . $Article->virtualFields['extra'] . ") = just text";
1321
		$this->assertEquals($expected, $result[0]);
1322
 
1323
		$conn->expects($this->at(0))
1324
			->method('quote')
1325
			->will($this->returnValue('just text'));
1326
		$conn->expects($this->at(1))
1327
			->method('quote')
1328
			->will($this->returnValue('other text'));
1329
 
1330
		$conditions = array('Article.extra' => array('just text', 'other text'));
1331
		$result = $db->conditionKeysToString($conditions, true, $Article);
1332
		$expected = "(" . $Article->virtualFields['extra'] . ") IN (just text, other text)";
1333
		$this->assertEquals($expected, $result[0]);
1334
	}
1335
 
1336
/**
1337
 * Test the limit function.
1338
 *
1339
 * @return void
1340
 */
1341
	public function testLimit() {
1342
		$db = new DboTestSource();
1343
 
1344
		$result = $db->limit('0');
1345
		$this->assertNull($result);
1346
 
1347
		$result = $db->limit('10');
1348
		$this->assertEquals(' LIMIT 10', $result);
1349
 
1350
		$result = $db->limit('FARTS', 'BOOGERS');
1351
		$this->assertEquals(' LIMIT 0, 0', $result);
1352
 
1353
		$result = $db->limit(20, 10);
1354
		$this->assertEquals(' LIMIT 10, 20', $result);
1355
 
1356
		$result = $db->limit(10, 300000000000000000000000000000);
1357
		$scientificNotation = sprintf('%.1E', 300000000000000000000000000000);
1358
		$this->assertNotContains($scientificNotation, $result);
1359
	}
1360
 
1361
/**
1362
 * Test insertMulti with id position.
1363
 *
1364
 * @return void
1365
 */
1366
	public function testInsertMultiId() {
1367
		$this->loadFixtures('Article');
1368
		$Article = ClassRegistry::init('Article');
1369
		$db = $Article->getDatasource();
1370
		$datetime = date('Y-m-d H:i:s');
1371
		$data = array(
1372
			array(
1373
				'user_id' => 1,
1374
				'title' => 'test',
1375
				'body' => 'test',
1376
				'published' => 'N',
1377
				'created' => $datetime,
1378
				'updated' => $datetime,
1379
				'id' => 100,
1380
			),
1381
			array(
1382
				'user_id' => 1,
1383
				'title' => 'test 101',
1384
				'body' => 'test 101',
1385
				'published' => 'N',
1386
				'created' => $datetime,
1387
				'updated' => $datetime,
1388
				'id' => 101,
1389
			)
1390
		);
1391
		$result = $db->insertMulti('articles', array_keys($data[0]), $data);
1392
		$this->assertTrue($result, 'Data was saved');
1393
 
1394
		$data = array(
1395
			array(
1396
				'id' => 102,
1397
				'user_id' => 1,
1398
				'title' => 'test',
1399
				'body' => 'test',
1400
				'published' => 'N',
1401
				'created' => $datetime,
1402
				'updated' => $datetime,
1403
			),
1404
			array(
1405
				'id' => 103,
1406
				'user_id' => 1,
1407
				'title' => 'test 101',
1408
				'body' => 'test 101',
1409
				'published' => 'N',
1410
				'created' => $datetime,
1411
				'updated' => $datetime,
1412
			)
1413
		);
1414
 
1415
		$result = $db->insertMulti('articles', array_keys($data[0]), $data);
1416
		$this->assertTrue($result, 'Data was saved');
1417
	}
1418
 
1419
/**
1420
 * Test defaultConditions()
1421
 *
1422
 * @return void
1423
 */
1424
	public function testDefaultConditions() {
1425
		$this->loadFixtures('Article');
1426
		$Article = ClassRegistry::init('Article');
1427
		$db = $Article->getDataSource();
1428
 
1429
		// Creates a default set of conditions from the model if $conditions is null/empty.
1430
		$Article->id = 1;
1431
		$result = $db->defaultConditions($Article, null);
1432
		$this->assertEquals(array('Article.id' => 1), $result);
1433
 
1434
		// $useAlias == false
1435
		$Article->id = 1;
1436
		$result = $db->defaultConditions($Article, null, false);
1437
		$this->assertEquals(array($db->fullTableName($Article, false) . '.id' => 1), $result);
1438
 
1439
		// If conditions are supplied then they will be returned.
1440
		$Article->id = 1;
1441
		$result = $db->defaultConditions($Article, array('Article.title' => 'First article'));
1442
		$this->assertEquals(array('Article.title' => 'First article'), $result);
1443
 
1444
		// If a model doesn't exist and no conditions were provided either null or false will be returned based on what was input.
1445
		$Article->id = 1000000;
1446
		$result = $db->defaultConditions($Article, null);
1447
		$this->assertNull($result);
1448
 
1449
		$Article->id = 1000000;
1450
		$result = $db->defaultConditions($Article, false);
1451
		$this->assertFalse($result);
1452
 
1453
		// Safe update mode
1454
		$Article->id = 1000000;
1455
		$Article->__safeUpdateMode = true;
1456
		$result = $db->defaultConditions($Article, null);
1457
		$this->assertFalse($result);
1458
	}
1459
}