Skip to content

Commit df6e8ec

Browse files
committed
Making all tests pass when using autoQuoting
1 parent a88f451 commit df6e8ec

File tree

4 files changed

+62
-18
lines changed

4 files changed

+62
-18
lines changed

Cake/ORM/Association/ExternalAssociationTrait.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
namespace Cake\ORM\Association;
1818

19-
use Cake\Database\Expression\Comparison;
2019
use Cake\Database\Expression\FieldExpression;
2120
use Cake\ORM\Query;
2221
use Cake\Utility\Inflector;
@@ -120,7 +119,7 @@ public abstract function eagerLoader(array $options);
120119
* clause for getting the results on the target table.
121120
*
122121
* @param array $options list of options passed to attachTo method
123-
* @return string|array
122+
* @return array
124123
*/
125124
protected function _joinCondition(array $options) {
126125
$field = sprintf(

Cake/ORM/Association/HasOne.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
namespace Cake\ORM\Association;
1818

19+
use Cake\Database\Expression\FieldExpression;
1920
use Cake\ORM\Association;
2021
use Cake\ORM\Association\DependentDeleteTrait;
2122
use Cake\ORM\Query;
@@ -67,15 +68,17 @@ public function foreignKey($key = null) {
6768
* clause for getting the results on the target table.
6869
*
6970
* @param array $options list of options passed to attachTo method
70-
* @return string|array
71+
* @return array
7172
*/
7273
protected function _joinCondition(array $options) {
73-
return sprintf('%s.%s = %s.%s',
74-
$this->_sourceTable->alias(),
75-
$this->_sourceTable->primaryKey(),
76-
$this->_targetTable->alias(),
77-
$options['foreignKey']
78-
);
74+
$field = sprintf('%s.%s',
75+
$this->_sourceTable->alias(),
76+
$this->_sourceTable->primaryKey()
77+
);
78+
$value = new FieldExpression(sprintf(
79+
'%s.%s', $this->_targetTable->alias(), $options['foreignKey']
80+
));
81+
return [$field => $value];
7982
}
8083

8184
}

Cake/Test/TestCase/ORM/Association/HasOneTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
namespace Cake\Test\TestCase\ORM\Association;
1818

19+
use Cake\Database\Expression\FieldExpression;
1920
use Cake\ORM\Association\HasOne;
2021
use Cake\ORM\Query;
2122
use Cake\ORM\Table;
@@ -84,11 +85,12 @@ public function testAttachTo() {
8485
'conditions' => ['Profile.is_active' => true]
8586
];
8687
$association = new HasOne('Profile', $config);
88+
$field = new FieldExpression('Profile.user_id');
8789
$query->expects($this->once())->method('join')->with([
8890
'Profile' => [
8991
'conditions' => [
9092
'Profile.is_active' => true,
91-
'User.id = Profile.user_id',
93+
['User.id' => $field],
9294
],
9395
'type' => 'INNER',
9496
'table' => 'profiles'
@@ -150,11 +152,12 @@ public function testAttachToNoFields() {
150152
'conditions' => ['Profile.is_active' => true]
151153
];
152154
$association = new HasOne('Profile', $config);
155+
$field = new FieldExpression('Profile.user_id');
153156
$query->expects($this->once())->method('join')->with([
154157
'Profile' => [
155158
'conditions' => [
156159
'Profile.is_active' => true,
157-
'User.id = Profile.user_id',
160+
['User.id' => $field],
158161
],
159162
'type' => 'INNER',
160163
'table' => 'profiles'

Cake/Test/TestCase/ORM/QueryTest.php

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
use Cake\Core\Configure;
2020
use Cake\Database\ConnectionManager;
21+
use Cake\Database\Expression\FieldExpression;
2122
use Cake\Database\Expression\OrderByExpression;
2223
use Cake\Database\Expression\QueryExpression;
2324
use Cake\ORM\Query;
@@ -113,55 +114,69 @@ public function testContainToJoinsOneLevel() {
113114
->with(['client' => [
114115
'table' => 'clients',
115116
'type' => 'LEFT',
116-
'conditions' => ['client.id = foo.client_id']
117+
'conditions' => [
118+
['client.id' => new FieldExpression('foo.client_id')]
119+
]
117120
]])
118121
->will($this->returnValue($query));
119122

120123
$query->expects($this->at(1))->method('join')
121124
->with(['order' => [
122125
'table' => 'orders',
123126
'type' => 'INNER',
124-
'conditions' => ['client.id = order.client_id']
127+
'conditions' => [
128+
['client.id' => new FieldExpression('order.client_id')]
129+
]
125130
]])
126131
->will($this->returnValue($query));
127132

128133
$query->expects($this->at(2))->method('join')
129134
->with(['orderType' => [
130135
'table' => 'order_types',
131136
'type' => 'LEFT',
132-
'conditions' => ['orderType.id = order.order_type_id']
137+
'conditions' => [
138+
['orderType.id' => new FieldExpression('order.order_type_id')]
139+
]
133140
]])
134141
->will($this->returnValue($query));
135142

136143
$query->expects($this->at(3))->method('join')
137144
->with(['stuff' => [
138145
'table' => 'things',
139146
'type' => 'INNER',
140-
'conditions' => ['order.id = stuff.order_id']
147+
'conditions' => [
148+
['order.id' => new FieldExpression('stuff.order_id')]
149+
]
141150
]])
142151
->will($this->returnValue($query));
143152

144153
$query->expects($this->at(4))->method('join')
145154
->with(['stuffType' => [
146155
'table' => 'stuff_types',
147156
'type' => 'LEFT',
148-
'conditions' => ['stuffType.id = stuff.stuff_type_id']
157+
'conditions' => [
158+
['stuffType.id' => new FieldExpression('stuff.stuff_type_id')]
159+
]
149160
]])
150161
->will($this->returnValue($query));
151162

152163
$query->expects($this->at(5))->method('join')
153164
->with(['company' => [
154165
'table' => 'organizations',
155166
'type' => 'LEFT',
156-
'conditions' => ['company.id = client.organization_id']
167+
'conditions' => [
168+
['company.id' => new FieldExpression('client.organization_id')]
169+
]
157170
]])
158171
->will($this->returnValue($query));
159172

160173
$query->expects($this->at(6))->method('join')
161174
->with(['category' => [
162175
'table' => 'categories',
163176
'type' => 'LEFT',
164-
'conditions' => ['category.id = company.category_id']
177+
'conditions' => [
178+
['category.id' => new FieldExpression('company.category_id')]
179+
]
165180
]])
166181
->will($this->returnValue($query));
167182

@@ -196,6 +211,7 @@ public function testContainToFieldsPredefined() {
196211
'client__telephone' => 'client.telephone',
197212
'order__total' => 'order.total', 'order__placed' => 'order.placed'
198213
];
214+
$expected = $this->_quoteArray($expected);
199215
$this->assertEquals($expected, $select);
200216
}
201217

@@ -217,13 +233,15 @@ public function testContainToFieldsDefault() {
217233
'order__id' => 'order.id', 'order__total' => 'order.total',
218234
'order__placed' => 'order.placed'
219235
];
236+
$expected = $this->_quoteArray($expected);
220237
$this->assertEquals($expected, $select);
221238

222239
$contains['client']['fields'] = ['name'];
223240
$query = new Query($this->connection, $this->table);
224241
$query->select('foo.id')->contain($contains)->sql();
225242
$select = $query->clause('select');
226243
$expected = ['foo__id' => 'foo.id', 'client__name' => 'client.name'];
244+
$expected = $this->_quoteArray($expected);
227245
$this->assertEquals($expected, $select);
228246

229247
$contains['client']['fields'] = [];
@@ -237,9 +255,30 @@ public function testContainToFieldsDefault() {
237255
'client__name' => 'client.name',
238256
'client__phone' => 'client.phone',
239257
];
258+
$expected = $this->_quoteArray($expected);
240259
$this->assertEquals($expected, $select);
241260
}
242261

262+
/**
263+
* Helper function sued to quoted both keys and values in an array in case
264+
* the test suite is running with auto quoting enabled
265+
*
266+
* @param array $elements
267+
* @return array
268+
*/
269+
protected function _quoteArray($elements) {
270+
if ($this->connection->driver()->autoQuoting()) {
271+
$quoter = function($e) {
272+
return $this->connection->driver()->quoteIdentifier($e);
273+
};
274+
return array_combine(
275+
array_map($quoter, array_keys($elements)),
276+
array_map($quoter, array_values($elements))
277+
);
278+
}
279+
return $elements;
280+
}
281+
243282
/**
244283
* Tests that results are grouped correctly when using contain()
245284
* and results are not hydrated

0 commit comments

Comments
 (0)