forked from cloudinary/cloudinary_php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiTest.php
More file actions
399 lines (360 loc) · 16.4 KB
/
Copy pathApiTest.php
File metadata and controls
399 lines (360 loc) · 16.4 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php
$base = realpath(dirname(__FILE__).DIRECTORY_SEPARATOR.'..');
require_once(join(DIRECTORY_SEPARATOR, array($base, 'src', 'Cloudinary.php')));
require_once(join(DIRECTORY_SEPARATOR, array($base, 'src', 'Uploader.php')));
require_once(join(DIRECTORY_SEPARATOR, array($base, 'src', 'Api.php')));
class ApiTest extends PHPUnit_Framework_TestCase {
static $initialized = FALSE;
public function setUp() {
if (!Cloudinary::config_get("api_secret")) {
$this->markTestSkipped('Please setup environment for API test to run');
}
$this->api = new \Cloudinary\Api();
if (self::$initialized) return;
self::$initialized = TRUE;
try {
$this->api->delete_resources(array("api_test", "api_test2", "api_test3", "api_test5"));
} catch (Exception $e) {}
try {
$this->api->delete_transformation("api_test_transformation");
} catch (Exception $e) {}
try {
$this->api->delete_transformation("api_test_transformation2");
} catch (Exception $e) {}
try {
$this->api->delete_transformation("api_test_transformation3");
} catch (Exception $e) {}
\Cloudinary\Uploader::upload("tests/logo.png",
array("public_id"=>"api_test", "tags"=>"api_test_tag", "context" => "key=value", "eager"=>array("transformation"=>array("width"=>100,"crop"=>"scale"))));
\Cloudinary\Uploader::upload("tests/logo.png",
array("public_id"=>"api_test2", "tags"=>"api_test_tag", "context" => "key=value", "eager"=>array("transformation"=>array("width"=>100,"crop"=>"scale"))));
}
function find_by_attr($elements, $attr, $value) {
foreach ($elements as $element) {
if ($element[$attr] == $value) return $element;
}
return NULL;
}
function test01_resource_types() {
// should allow listing resource_types
$result = $this->api->resource_types();
$this->assertContains("image", $result["resource_types"]);
}
function test02_resources() {
// should allow listing resources
$result = $this->api->resources();
$resource = $this->find_by_attr($result["resources"], "public_id", "api_test");
$this->assertNotEquals($resource, NULL);
$this->assertEquals($resource["type"], "upload");
}
function test03_resources_cursor() {
// should allow listing resources with cursor
$result = $this->api->resources(array("max_results"=>1));
$this->assertNotEquals($result["resources"], NULL);
$this->assertEquals(count($result["resources"]), 1);
$this->assertNotEquals($result["next_cursor"], NULL);
$result2 = $this->api->resources(array("max_results"=>1, "next_cursor"=>$result["next_cursor"]));
$this->assertNotEquals($result2["resources"], NULL);
$this->assertEquals(count($result2["resources"]), 1);
$this->assertNotEquals($result2["resources"][0]["public_id"], $result["resources"][0]["public_id"]);
}
function test04_resources_by_type() {
// should allow listing resources by type
$result = $this->api->resources(array("type"=>"upload", "context" => true, "tags" => true));
$resource = $this->find_by_attr($result["resources"], "public_id", "api_test");
$context_map = function($resource) {
if (array_key_exists("context", $resource) && array_key_exists("key", $resource["context"]["custom"])) {
return $resource["context"]["custom"]["key"];
} else {
return NULL;
}
};
$tags_map = function($resource) {
if (count($resource["tags"]) > 0) {
return $resource["tags"][0];
} else {
return NULL;
}
};
$context_values = array_map($context_map, $result["resources"]);
$tags = array_map($tags_map, $result["resources"]);
$this->assertNotEquals($resource, NULL);
$this->assertContains("value", $context_values);
$this->assertContains("api_test_tag", $tags);
}
function test05_resources_by_prefix() {
// should allow listing resources by prefix
$result = $this->api->resources(array("type"=>"upload", "prefix"=>"api_test", "context" => true, "tags" => true));
$func = function($resource) {
return $resource["public_id"];
};
$context_map = function($resource) {
if (array_key_exists("context", $resource)) {
return $resource["context"]["custom"]["key"];
} else {
return NULL;
}
};
$tags_map = function($resource) {
if (count($resource["tags"]) > 0) {
return $resource["tags"][0];
} else {
return NULL;
}
};
$context_values = array_map($context_map, $result["resources"]);
$tags = array_map($tags_map, $result["resources"]);
$public_ids = array_map($func, $result["resources"]);
$this->assertContains("api_test", $public_ids);
$this->assertContains("api_test2", $public_ids);
$this->assertContains("value", $context_values);
$this->assertContains("api_test_tag", $tags);
}
function test_resources_by_public_ids() {
// should allow listing resources by public ids
$result = $this->api->resources_by_ids(array("api_test", "api_test2", "api_test3"), array("context" => true, "tags" => true));
$id_map = function($resource) {
return $resource["public_id"];
};
$context_map = function($resource) {
return $resource["context"]["custom"]["key"];
};
$tags_map = function($resource) {
return $resource["tags"][0];
};
$public_ids = array_map($id_map, $result["resources"]);
$context_values = array_map($context_map, $result["resources"]);
$tags = array_map($tags_map, $result["resources"]);
$this->assertContains("api_test", $public_ids);
$this->assertContains("api_test2", $public_ids);
$this->assertContains("value", $context_values);
$this->assertContains("api_test_tag", $tags);
}
function test_resources_direction() {
// should allow listing resources and specify direction
$asc_resources = $this->api->resources(array("type"=>"upload", "prefix"=>"api_test", "direction"=>"asc"))["resources"];
$desc_resources = $this->api->resources(array("type"=>"upload", "prefix"=>"api_test", "direction"=>"desc"))["resources"];
$this->assertEquals(array_reverse($asc_resources), $desc_resources);
$asc_resources_alt = $this->api->resources(array("type"=>"upload", "prefix"=>"api_test", "direction"=>1))["resources"];
$desc_resources_alt = $this->api->resources(array("type"=>"upload", "prefix"=>"api_test", "direction"=>-1))["resources"];
$this->assertEquals(array_reverse($asc_resources_alt), $desc_resources_alt);
$this->assertEquals($asc_resources_alt, $asc_resources);
}
function test06_resources_tag() {
// should allow listing resources by tag
$result = $this->api->resources_by_tag("api_test_tag");
$resource = $this->find_by_attr($result["resources"], "public_id", "api_test");
$this->assertNotEquals($resource, NULL);
}
function test07_resource_metadata() {
// should allow get resource metadata
$resource = $this->api->resource("api_test");
$this->assertNotEquals($resource, NULL);
$this->assertEquals($resource["public_id"], "api_test");
$this->assertEquals($resource["bytes"], 3381);
$this->assertEquals(count($resource["derived"]), 1);
}
function test08_delete_derived() {
// should allow deleting derived resource
\Cloudinary\Uploader::upload("tests/logo.png", array("public_id"=>"api_test3", "eager"=>array("transformation"=>array("width"=> 101,"crop" => "scale"))));
$resource = $this->api->resource("api_test3");
$this->assertNotEquals($resource, NULL);
$this->assertEquals(count($resource["derived"]), 1);
$derived_resource_id = $resource["derived"][0]["id"];
$this->api->delete_derived_resources(array($derived_resource_id));
$resource = $this->api->resource("api_test3");
$this->assertNotEquals($resource, NULL);
$this->assertEquals(count($resource["derived"]), 0);
}
/**
* @expectedException \Cloudinary\Api\NotFound
*/
function test09_delete_resources() {
// should allow deleting resources
\Cloudinary\Uploader::upload("tests/logo.png", array("public_id"=>"api_test3"));
$resource = $this->api->resource("api_test3");
$this->assertNotEquals($resource, NULL);
$this->api->delete_resources(array("apit_test", "api_test2", "api_test3"));
$this->api->resource("api_test3");
}
/**
* @expectedException \Cloudinary\Api\NotFound
*/
function test09a_delete_resources_by_prefix() {
// should allow deleting resources
\Cloudinary\Uploader::upload("tests/logo.png", array("public_id"=>"api_test_by_prefix"));
$resource = $this->api->resource("api_test_by_prefix");
$this->assertNotEquals($resource, NULL);
$this->api->delete_resources_by_prefix("api_test_by");
$this->api->resource("api_test_by_prefix");
}
/**
* @expectedException \Cloudinary\Api\NotFound
*/
function test09b_delete_resources_by_tag() {
// should allow deleting resources
\Cloudinary\Uploader::upload("tests/logo.png", array("public_id"=>"api_test4", "tags"=>array("api_test_tag_for_delete")));
$resource = $this->api->resource("api_test4");
$this->assertNotEquals($resource, NULL);
$this->api->delete_resources_by_tag("api_test_tag_for_delete");
$this->api->resource("api_test4");
}
function test10_tags() {
// should allow listing tags
$result = $this->api->tags();
$tags = $result["tags"];
$this->assertContains("api_test_tag", $tags);
}
function test11_tags_prefix() {
// should allow listing tag by prefix
$result = $this->api->tags(array("prefix"=>"api_test"));
$tags = $result["tags"];
$this->assertContains("api_test_tag", $tags);
$result = $this->api->tags(array("prefix"=>"api_test_no_such_tag"));
$tags = $result["tags"];
$this->assertEquals(count($tags), 0);
}
function test12_transformations() {
// should allow listing transformations
$result = $this->api->transformations();
$transformation = $this->find_by_attr($result["transformations"], "name", "c_scale,w_100");
$this->assertNotEquals($transformation, NULL);
$this->assertEquals($transformation["used"], TRUE);
}
function test13_transformation_metadata() {
// should allow getting transformation metadata
$transformation = $this->api->transformation("c_scale,w_100");
$this->assertNotEquals($transformation, NULL);
$this->assertEquals($transformation["info"], array(array("crop"=> "scale", "width"=> 100)));
$transformation = $this->api->transformation(array("crop"=> "scale", "width"=> 100));
$this->assertNotEquals($transformation, NULL);
$this->assertEquals($transformation["info"], array(array("crop"=> "scale", "width"=> 100)));
}
function test14_transformation_update() {
// should allow updating transformation allowed_for_strict
$this->api->update_transformation("c_scale,w_100", array("allowed_for_strict"=>TRUE));
$transformation = $this->api->transformation("c_scale,w_100");
$this->assertNotEquals($transformation, NULL);
$this->assertEquals($transformation["allowed_for_strict"], TRUE);
$this->api->update_transformation("c_scale,w_100", array("allowed_for_strict"=>FALSE));
$transformation = $this->api->transformation("c_scale,w_100");
$this->assertNotEquals($transformation, NULL);
$this->assertEquals($transformation["allowed_for_strict"], FALSE);
}
function test15_transformation_create() {
// should allow creating named transformation
$this->api->create_transformation("api_test_transformation", array("crop" => "scale", "width" => 102));
$transformation = $this->api->transformation("api_test_transformation");
$this->assertNotEquals($transformation, NULL);
$this->assertEquals($transformation["allowed_for_strict"], TRUE);
$this->assertEquals($transformation["info"], array(array("crop"=> "scale", "width"=> 102)));
$this->assertEquals($transformation["used"], FALSE);
}
function test15a_transformation_unsafe_update() {
// should allow unsafe update of named transformation
$this->api->create_transformation("api_test_transformation3", array("crop" => "scale", "width" => 102));
$this->api->update_transformation("api_test_transformation3", array("unsafe_update"=>array("crop"=>"scale", "width"=>103)));
$transformation = $this->api->transformation("api_test_transformation3");
$this->assertNotEquals($transformation, NULL);
$this->assertEquals($transformation["info"], array(array("crop"=> "scale", "width"=> 103)));
$this->assertEquals($transformation["used"], FALSE);
}
function test16a_transformation_delete() {
// should allow deleting named transformation
$this->api->create_transformation("api_test_transformation2", array("crop" => "scale", "width" => 103));
$this->api->transformation("api_test_transformation2");
$this->api->delete_transformation("api_test_transformation2");
}
/**
* @expectedException \Cloudinary\Api\NotFound
*/
function test16b_transformation_delete() {
$this->api->transformation("api_test_transformation2");
}
function test17a_transformation_delete_implicit() {
// should allow deleting implicit transformation
$this->api->transformation("c_scale,w_100");
$this->api->delete_transformation("c_scale,w_100");
}
/**
* @expectedException \Cloudinary\Api\NotFound
*/
function test17b_transformation_delete_implicit() {
$this->api->transformation("c_scale,w_100");
}
function test18_usage() {
// should allow listing resource_types
$result = $this->api->usage();
$this->assertNotEquals($result["last_updated"], NULL);
}
function test19_delete_derived() {
// should allow deleting all resources
$this->markTestSkipped("Not enabled by default - remove this line to test");
\Cloudinary\Uploader::upload("tests/logo.png", array("public_id"=>"api_test5", "eager"=>array("transformation"=>array("width"=> 101,"crop" => "scale"))));
$resource = $this->api->resource("api_test5");
$this->assertNotEquals($resource, NULL);
$this->assertEquals(count($resource["derived"]), 1);
$this->api->delete_all_resources(array("keep_original" => True));
$resource = $this->api->resource("api_test5");
$this->assertNotEquals($resource, NULL);
$this->assertEquals(count($resource["derived"]), 0);
}
function test20_manual_moderation() {
// should support setting manual moderation status
$resource = \Cloudinary\Uploader::upload("tests/logo.png", array("moderation"=>"manual"));
$this->assertEquals($resource["moderation"][0]["status"], "pending");
$this->assertEquals($resource["moderation"][0]["kind"], "manual");
$api_result = $this->api->update($resource["public_id"], array("moderation_status" => "approved"));
$this->assertEquals($api_result["moderation"][0]["status"], "approved");
$this->assertEquals($api_result["moderation"][0]["kind"], "manual");
}
/**
* @expectedException \Cloudinary\Api\BadRequest
* @expectedExceptionMessage Illegal value
*/
function test21_ocr_info() {
// should support requesting ocr info
$this->api->update("api_test", array("ocr" => "illegal"));
}
/**
* @expectedException \Cloudinary\Api\BadRequest
* @expectedExceptionMessage Illegal value
*/
function test22_raw_conversion() {
// should support requesting raw_convert
$resource = \Cloudinary\Uploader::upload("tests/docx.docx", array("resource_type"=>"raw"));
$this->api->update($resource["public_id"], array("raw_convert" => "illegal", "resource_type"=>"raw"));
}
/**
* @expectedException \Cloudinary\Api\BadRequest
* @expectedExceptionMessage Illegal value
*/
function test23_categorization() {
// should support requesting categorization
$this->api->update("api_test", array("categorization" => "illegal"));
}
/**
* @expectedException \Cloudinary\Api\BadRequest
* @expectedExceptionMessage Illegal value
*/
function test24_detection() {
// should support requesting detection
$this->api->update("api_test", array("detection" => "illegal"));
}
/**
* @expectedException \Cloudinary\Api\BadRequest
* @expectedExceptionMessage Illegal value
*/
function test25_similarity_search() {
// should support requesting similarity_search
$this->api->update("api_test", array("similarity_search" => "illegal"));
}
/**
* @expectedException \Cloudinary\Api\BadRequest
* @expectedExceptionMessage Must use
*/
function test26_auto_tagging() {
// should support requesting auto_tagging
$this->api->update("api_test", array("auto_tagging" => 0.5));
}
}