-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathSearchApiTest.php
More file actions
381 lines (330 loc) · 11.6 KB
/
SearchApiTest.php
File metadata and controls
381 lines (330 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
/**
* This file is part of the Cloudinary PHP package.
*
* (c) Cloudinary
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cloudinary\Test\Integration\Search;
use Cloudinary\Api\Exception\ApiError;
use Cloudinary\Api\Exception\BadRequest;
use Cloudinary\Api\Search\SearchFoldersApi;
use Cloudinary\Api\Search\SearchApi;
use Cloudinary\Cloudinary;
use Cloudinary\Test\Integration\IntegrationTestCase;
use Cloudinary\StringUtils;
use Cloudinary\Transformation\Scale;
use Cloudinary\Transformation\Transformation;
/**
* Class SearchApiTest
*/
class SearchApiTest extends IntegrationTestCase
{
const CONTEXT_KEY = 'key';
const SEARCH_ASSET_1 = 'search_asset_1';
const SEARCH_ASSET_2 = 'search_asset_2';
const SEARCH_ASSET_3 = 'search_asset_3';
const FOLDER_BASE_NAME = 'test_folder';
private static $STRING_WITH_UNDERSCORE;
private static $STRING_1;
private static $STRING_2;
private static $MULTI_STRING;
private static $FOLDER_NAME;
private static $FOLDER2_NAME;
/**
* @var SearchApi
*/
public $search;
/**
* @var SearchFoldersApi
*/
public $searchFolders;
/**
* @throws ApiError
*/
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
// Define a number of unique strings to be used in tags and public ids
self::$STRING_WITH_UNDERSCORE = 'expression_' . self::$UNIQUE_TEST_ID;
self::$STRING_1 = '1stString' . self::$SUFFIX;
self::$STRING_2 = '2ndString' . self::$SUFFIX;
self::$MULTI_STRING = self::$STRING_1 . '_' . self::$STRING_2;
self::$FOLDER_NAME = self::FOLDER_BASE_NAME . '_' . self::$UNIQUE_TEST_ID;
self::$FOLDER2_NAME = self::FOLDER_BASE_NAME . '_2_' . self::$UNIQUE_TEST_ID;
$assets = [
'options' => [
'context' => ['stage' => 'value'],
'eager' => (new Transformation())->resize(Scale::scale(100)),
],
];
self::createTestAssets(
[
self::SEARCH_ASSET_1 => $assets,
self::SEARCH_ASSET_2 => $assets,
self::SEARCH_ASSET_3 => $assets,
[
'options' => [
'tags' => [self::$STRING_WITH_UNDERSCORE, self::$STRING_1],
'public_id' => self::$STRING_1,
'folder' => self::$FOLDER_NAME,
],
],
[
'options' => [
'tags' => [self::$STRING_2],
'context' => [self::CONTEXT_KEY => self::$STRING_WITH_UNDERSCORE],
'folder' => self::$FOLDER_NAME,
],
],
[
'options' => [
'tags' => [
self::$STRING_WITH_UNDERSCORE,
self::$MULTI_STRING,
],
'context' => [self::CONTEXT_KEY => self::$STRING_WITH_UNDERSCORE],
'folder' => self::$FOLDER2_NAME,
],
],
]
);
sleep(3); // FIXME
}
public function setUp()
{
parent::setUp();
$this->search = (new Cloudinary())->searchApi();
$this->searchFolders = (new Cloudinary())->searchFoldersApi();
}
public static function tearDownAfterClass()
{
self::cleanupTestAssets();
}
/**
* Assert that a search request with empty query parameters
*/
public function testEmptyQuery()
{
$result = $this->search->asArray();
self::assertCount(0, $result, 'Should generate an empty query JSON');
}
/**
* Asserts that a filter value was added to a search query parameter
*/
public function testShouldAddExpressionAsArray()
{
$query = $this->search->expression('format:jpg')->asArray();
self::assertEquals(['expression' => 'format:jpg'], $query);
}
/**
* Asserts that a sort value was added to a search query parameter
*/
public function testShouldAddSortByAsArray()
{
$query = $this->search->sortBy('created_at', 'asc')->sortBy('updated_at', 'desc')->asArray();
self::assertEquals(
[
'sort_by' => [
['created_at' => 'asc'],
['updated_at' => 'desc'],
],
],
$query
);
}
/**
* Asserts that a limit value of results was added to a search query parameter
*/
public function testShouldAddMaxResultsAsArray()
{
$query = $this->search->maxResults('10')->asArray();
self::assertEquals(['max_results' => '10'], $query);
}
/**
* Asserts that a `next_cursor` was added to a search query parameter
*/
public function testShouldAddNextCursorAsArray()
{
$query = $this
->search
->nextCursor(
'ec471a97ba510904ab57460b3ba3150ec29b6f8563eb1c10f6925ed0c6813f33cfa62ec6cf5ad96be6d6fa3ac3a76ccb'
)
->asArray();
self::assertEquals(
[
'next_cursor' =>
'ec471a97ba510904ab57460b3ba3150ec29b6f8563eb1c10f6925ed0c6813f33cfa62ec6cf5ad96be6d6fa3ac3a76ccb',
],
$query
);
}
/**
* Asserts that aggregate fields was added to a search query parameter
*/
public function testShouldAddAggregationsArgumentsAsArrayAsArray()
{
$query = $this->search->aggregate('format')->aggregate('size_category')->asArray();
self::assertEquals(['aggregate' => ['format', 'size_category']], $query);
}
/**
* Asserts that additional asset attribute was added to a search query parameter
*/
public function testShouldAddWithFieldAsArray()
{
$query = $this->search->withField('context')->withField('tags')->asArray();
self::assertEquals(['with_field' => ['context', 'tags']], $query);
}
/**
* Finds assets by certain tag
*
* @throws ApiError
*/
public function testShouldReturnAllImagesTagged()
{
$results = $this->search->expression('tags:' . self::$UNIQUE_TEST_TAG)->execute();
self::assertCount(6, $results['resources']);
}
/**
* Finds assets by public id
*
* @throws ApiError
*/
public function testShouldReturnResource()
{
$results = $this->search->expression(
'public_id:' . self::getTestAssetPublicId(self::SEARCH_ASSET_1)
)->execute();
self::assertCount(1, $results['resources']);
}
/**
* Finds assets by asset id using a colon.
*
* @throws ApiError
*/
public function testShouldReturnResourceByAssetIdUsingColon()
{
$results = $this->search->expression(
'asset_id:' . self::getTestAssetAssetId(self::SEARCH_ASSET_1)
)->execute();
self::assertCount(1, $results['resources']);
}
/**
* Finds assets by asset id using an equal sign.
*
* @throws ApiError
*/
public function testShouldReturnResourceByAssetIdUsingEqualSign()
{
$results = $this->search->expression(
'asset_id=' . self::getTestAssetAssetId(self::SEARCH_ASSET_1)
)->execute();
self::assertCount(1, $results['resources']);
}
/**
* Find assets without limiting expression to certain fields
* Shows results containing given text in any string field
* This test will match two results where the expression is matched in tags and public_id
*
* @throws ApiError
*/
public function testFindAssetsByGeneralExpression()
{
$result = $this->search
->expression(self::$STRING_1 . '*')
->maxResults(1)
->execute();
self::assertEquals(2, $result['total_count']);
self::assertCount(1, $result['resources']);
self::assertValidAsset($result['resources'][0]);
}
/**
* Find assets with an expression limiting the search expression to certain fields
* Shows results containing given text in tags field
*
* @throws ApiError
*/
public function testFindAssetsByExpressionWithTag()
{
$result = $this->search
->expression('resource_type:image AND tags:' . self::$STRING_WITH_UNDERSCORE)
->withField('tags')
->maxResults(1)
->execute();
self::assertEquals(2, $result['total_count']);
self::assertCount(1, $result['resources']);
self::assertValidAsset($result['resources'][0]);
self::assertContains(self::$STRING_WITH_UNDERSCORE, $result['resources'][0]['tags']);
}
/**
* Find assets by expression and returns sorted results
* Shows results in a certain order containing given text in tags field
*
* @throws ApiError
*/
public function testFindAssetsByExpressionSorted()
{
$result = $this->search
->expression('resource_type:image AND tags:' . self::$STRING_WITH_UNDERSCORE)
->withField('tags')
->sortBy('public_id', 'desc')
->maxResults(2)
->execute();
self::assertCount(2, $result['resources']);
// Verify that $result['resources'] is sorted by public_id in a descending order. We do this by sorting the
// result ourselves and comparing the sorted results to the result returned from the Search API.
$resources = $result['resources'];
usort(
$resources,
static function ($a, $b) {
if ($a['public_id'] === $b['public_id']) {
return 0;
}
return ($a['public_id'] > $b['public_id']) ? -1 : 1;
}
);
self::assertEquals($resources, $result['resources']);
}
/**
* Find assets with a negative expression limiting results returned
* Shows results containing given text in context but not as a tag in the tags field
* Return aggregate field
*
* @throws ApiError
*/
public function testFindAssetsByExpressionWithoutCertainTag()
{
$expression = 'resource_type:image'
. ' AND context.key:' . self::$STRING_WITH_UNDERSCORE
. ' AND -tags:' . self::$STRING_WITH_UNDERSCORE;
try {
$result = $this->search
->expression($expression)
->withField('tags')
->maxResults(1)
->aggregate('format')
->execute();
} catch (BadRequest $br) {
if (StringUtils::contains($br->getMessage(), 'Your subscription plan does not support aggregations')) {
$this->markTestSkipped($br->getMessage());
}
}
self::assertEquals(1, $result['aggregations']['format']['gif']);
self::assertEquals(1, $result['total_count']);
self::assertCount(1, $result['resources']);
self::assertValidAsset($result['resources'][0]);
}
public function testSearchFoldersApi()
{
$result = $this->searchFolders
->expression(self::FOLDER_BASE_NAME . '*')
->maxResults(2)
->execute();
self::assertGreaterThan(1, $result['total_count']);
self::assertCount(2, $result['folders']);
self::assertStringContainsString(self::FOLDER_BASE_NAME, $result['folders'][0]['name']);
}
}