18
18
19
19
use Cake \Core \Configure ;
20
20
use Cake \Database \ConnectionManager ;
21
+ use Cake \Database \Expression \FieldExpression ;
21
22
use Cake \Database \Expression \OrderByExpression ;
22
23
use Cake \Database \Expression \QueryExpression ;
23
24
use Cake \ORM \Query ;
@@ -113,55 +114,69 @@ public function testContainToJoinsOneLevel() {
113
114
->with (['client ' => [
114
115
'table ' => 'clients ' ,
115
116
'type ' => 'LEFT ' ,
116
- 'conditions ' => ['client.id = foo.client_id ' ]
117
+ 'conditions ' => [
118
+ ['client.id ' => new FieldExpression ('foo.client_id ' )]
119
+ ]
117
120
]])
118
121
->will ($ this ->returnValue ($ query ));
119
122
120
123
$ query ->expects ($ this ->at (1 ))->method ('join ' )
121
124
->with (['order ' => [
122
125
'table ' => 'orders ' ,
123
126
'type ' => 'INNER ' ,
124
- 'conditions ' => ['client.id = order.client_id ' ]
127
+ 'conditions ' => [
128
+ ['client.id ' => new FieldExpression ('order.client_id ' )]
129
+ ]
125
130
]])
126
131
->will ($ this ->returnValue ($ query ));
127
132
128
133
$ query ->expects ($ this ->at (2 ))->method ('join ' )
129
134
->with (['orderType ' => [
130
135
'table ' => 'order_types ' ,
131
136
'type ' => 'LEFT ' ,
132
- 'conditions ' => ['orderType.id = order.order_type_id ' ]
137
+ 'conditions ' => [
138
+ ['orderType.id ' => new FieldExpression ('order.order_type_id ' )]
139
+ ]
133
140
]])
134
141
->will ($ this ->returnValue ($ query ));
135
142
136
143
$ query ->expects ($ this ->at (3 ))->method ('join ' )
137
144
->with (['stuff ' => [
138
145
'table ' => 'things ' ,
139
146
'type ' => 'INNER ' ,
140
- 'conditions ' => ['order.id = stuff.order_id ' ]
147
+ 'conditions ' => [
148
+ ['order.id ' => new FieldExpression ('stuff.order_id ' )]
149
+ ]
141
150
]])
142
151
->will ($ this ->returnValue ($ query ));
143
152
144
153
$ query ->expects ($ this ->at (4 ))->method ('join ' )
145
154
->with (['stuffType ' => [
146
155
'table ' => 'stuff_types ' ,
147
156
'type ' => 'LEFT ' ,
148
- 'conditions ' => ['stuffType.id = stuff.stuff_type_id ' ]
157
+ 'conditions ' => [
158
+ ['stuffType.id ' => new FieldExpression ('stuff.stuff_type_id ' )]
159
+ ]
149
160
]])
150
161
->will ($ this ->returnValue ($ query ));
151
162
152
163
$ query ->expects ($ this ->at (5 ))->method ('join ' )
153
164
->with (['company ' => [
154
165
'table ' => 'organizations ' ,
155
166
'type ' => 'LEFT ' ,
156
- 'conditions ' => ['company.id = client.organization_id ' ]
167
+ 'conditions ' => [
168
+ ['company.id ' => new FieldExpression ('client.organization_id ' )]
169
+ ]
157
170
]])
158
171
->will ($ this ->returnValue ($ query ));
159
172
160
173
$ query ->expects ($ this ->at (6 ))->method ('join ' )
161
174
->with (['category ' => [
162
175
'table ' => 'categories ' ,
163
176
'type ' => 'LEFT ' ,
164
- 'conditions ' => ['category.id = company.category_id ' ]
177
+ 'conditions ' => [
178
+ ['category.id ' => new FieldExpression ('company.category_id ' )]
179
+ ]
165
180
]])
166
181
->will ($ this ->returnValue ($ query ));
167
182
@@ -196,6 +211,7 @@ public function testContainToFieldsPredefined() {
196
211
'client__telephone ' => 'client.telephone ' ,
197
212
'order__total ' => 'order.total ' , 'order__placed ' => 'order.placed '
198
213
];
214
+ $ expected = $ this ->_quoteArray ($ expected );
199
215
$ this ->assertEquals ($ expected , $ select );
200
216
}
201
217
@@ -217,13 +233,15 @@ public function testContainToFieldsDefault() {
217
233
'order__id ' => 'order.id ' , 'order__total ' => 'order.total ' ,
218
234
'order__placed ' => 'order.placed '
219
235
];
236
+ $ expected = $ this ->_quoteArray ($ expected );
220
237
$ this ->assertEquals ($ expected , $ select );
221
238
222
239
$ contains ['client ' ]['fields ' ] = ['name ' ];
223
240
$ query = new Query ($ this ->connection , $ this ->table );
224
241
$ query ->select ('foo.id ' )->contain ($ contains )->sql ();
225
242
$ select = $ query ->clause ('select ' );
226
243
$ expected = ['foo__id ' => 'foo.id ' , 'client__name ' => 'client.name ' ];
244
+ $ expected = $ this ->_quoteArray ($ expected );
227
245
$ this ->assertEquals ($ expected , $ select );
228
246
229
247
$ contains ['client ' ]['fields ' ] = [];
@@ -237,9 +255,30 @@ public function testContainToFieldsDefault() {
237
255
'client__name ' => 'client.name ' ,
238
256
'client__phone ' => 'client.phone ' ,
239
257
];
258
+ $ expected = $ this ->_quoteArray ($ expected );
240
259
$ this ->assertEquals ($ expected , $ select );
241
260
}
242
261
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
+
243
282
/**
244
283
* Tests that results are grouped correctly when using contain()
245
284
* and results are not hydrated
0 commit comments