forked from cloudinary/cloudinary_php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUploaderTest.php
More file actions
212 lines (184 loc) · 9.86 KB
/
Copy pathUploaderTest.php
File metadata and controls
212 lines (184 loc) · 9.86 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
<?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 UploaderTest extends PHPUnit_Framework_TestCase {
public function setUp() {
Cloudinary::reset_config();
if (!Cloudinary::config_get("api_secret")) {
$this->markTestSkipped('Please setup environment for Upload test to run');
}
}
public function test_upload() {
$result = Cloudinary\Uploader::upload("tests/logo.png");
$this->assertEquals($result["width"], 241);
$this->assertEquals($result["height"], 51);
$expected_signature = Cloudinary::api_sign_request(array("public_id"=>$result["public_id"], "version"=>$result["version"]), Cloudinary::config_get("api_secret"));
$this->assertEquals($result["signature"], $expected_signature);
}
public function test_rename() {
$result = Cloudinary\Uploader::upload("tests/logo.png");
Cloudinary\Uploader::rename($result["public_id"], $result["public_id"]."2");
$api = new \Cloudinary\Api();
$this->assertNotNull($api->resource($result["public_id"]."2"));
$result2 = Cloudinary\Uploader::upload("tests/favicon.ico");
$error_thrown = FALSE;
try {
Cloudinary\Uploader::rename($result2["public_id"], $result["public_id"]."2");
$error_thrown = TRUE;
} catch (Exception $e) {}
$this->assertFalse($error_thrown);
Cloudinary\Uploader::rename($result2["public_id"], $result["public_id"]."2", array("overwrite"=>TRUE));
$resource = $api->resource($result["public_id"]."2");
$this->assertEquals($resource["format"], "ico");
}
public function test_explicit() {
$result = Cloudinary\Uploader::explicit("cloudinary", array("type"=>"twitter_name", "eager"=>array("crop"=>"scale", "width"=>"2.0")));
$url = cloudinary_url("cloudinary", array("type"=>"twitter_name", "crop"=>"scale", "width"=>"2.0", "format"=>"png", "version"=>$result["version"]));
$this->assertEquals($result["eager"][0]["url"], $url);
}
public function test_eager() {
Cloudinary\Uploader::upload("tests/logo.png", array("eager"=>array("crop"=>"scale", "width"=>"2.0")));
}
public function test_headers() {
Cloudinary\Uploader::upload("tests/logo.png", array("headers"=>array("Link: 1")));
Cloudinary\Uploader::upload("tests/logo.png", array("headers"=>array("Link" => "1")));
}
public function test_text() {
$result = Cloudinary\Uploader::text("hello world");
$this->assertGreaterThan(1, $result["width"]);
$this->assertGreaterThan(1, $result["height"]);
}
public function test_tags() {
$api = new \Cloudinary\Api();
$result = Cloudinary\Uploader::upload("tests/logo.png");
Cloudinary\Uploader::add_tag("tag1", $result["public_id"]);
Cloudinary\Uploader::add_tag("tag2", $result["public_id"]);
$info = $api->resource($result["public_id"]);
$this->assertEquals($info["tags"], array("tag1", "tag2"));
Cloudinary\Uploader::remove_tag("tag1", $result["public_id"]);
$info = $api->resource($result["public_id"]);
$this->assertEquals($info["tags"], array("tag2"));
Cloudinary\Uploader::replace_tag("tag3", $result["public_id"]);
$info = $api->resource($result["public_id"]);
$this->assertEquals($info["tags"], array("tag3"));
}
public function test_use_filename() {
$api = new \Cloudinary\Api();
$result = Cloudinary\Uploader::upload("tests/logo.png", array("use_filename"=>TRUE));
$this->assertRegExp('/logo_[a-zA-Z0-9]{6}/', $result["public_id"]);
$result = Cloudinary\Uploader::upload("tests/logo.png", array("use_filename"=>TRUE, "unique_filename"=>FALSE));
$this->assertEquals("logo", $result["public_id"]);
}
public function test_allowed_formats() {
//should allow whitelisted formats if allowed_formats
$formats = array("png");
$result = Cloudinary\Uploader::upload("tests/logo.png", array("allowed_formats" => $formats));
$this->assertEquals($result["format"], "png");
}
public function test_allowed_formats_with_illegal_format() {
//should prevent non whitelisted formats from being uploaded if allowed_formats is specified
$error_found = FALSE;
$formats = array("jpg");
try{
Cloudinary\Uploader::upload("tests/logo.png", array("allowed_formats" => $formats));
} catch (Exception $e) {
$error_found = TRUE;
}
$this->assertTrue($error_found);
}
public function test_allowed_formats_with_format() {
//should allow non whitelisted formats if type is specified and convert to that type
$formats = array("jpg");
$result = Cloudinary\Uploader::upload("tests/logo.png", array("allowed_formats" => $formats, "format" => "jpg"));
$this->assertEquals("jpg", $result["format"]);
}
public function test_face_coordinates() {
//should allow sending face coordinates
$coordinates = array(array(120, 30, 109, 150), array(121, 31, 110, 151));
$result = Cloudinary\Uploader::upload("tests/logo.png", array("face_coordinates" => $coordinates, "faces" => TRUE));
$this->assertEquals($coordinates, $result["faces"]);
$different_coordinates = array(array(122, 32, 111, 152));
Cloudinary\Uploader::explicit($result["public_id"], array("face_coordinates" => $different_coordinates, "faces" => TRUE, "type" => "upload"));
$api = new \Cloudinary\Api();
$info = $api->resource($result["public_id"], array("faces" => true));
$this->assertEquals($different_coordinates, $info["faces"]);
}
public function test_context() {
//should allow sending context
$context = array("caption" => "some caption", "alt" => "alternative");
$result = Cloudinary\Uploader::upload("tests/logo.png", array("context" => $context));
$api = new \Cloudinary\Api();
$info = $api->resource($result["public_id"], array("context" => true));
$this->assertEquals(array("custom" => $context), $info["context"]);
}
public function test_cl_form_tag() {
Cloudinary::config(array("cloud_name"=>"test123", "secure_distribution" => NULL, "private_cdn" => FALSE, "api_key" => "1234"));
$form = cl_form_tag("http://callback.com", array("public_id"=>"hello", "form"=>array("class"=>"uploader")));
$this->assertRegexp("/<form enctype='multipart\/form-data' action='https:\/\/api.cloudinary.com\/v1_1\/test123\/image\/upload' method='POST' class='uploader'>\n<input name='timestamp' type='hidden' value='\d+'\/>\n<input name='public_id' type='hidden' value='hello'\/>\n<input name='signature' type='hidden' value='[0-9a-f]+'\/>\n<input name='api_key' type='hidden' value='1234'\/>\n<\/form>/", $form);
}
public function test_cl_image_upload_tag() {
Cloudinary::config(array("cloud_name"=>"test123", "secure_distribution" => NULL, "private_cdn" => FALSE, "api_key" => "1234"));
$tag = cl_image_upload_tag("image", array("public_id"=>"hello", "html"=>array("class"=>"uploader")));
$this->assertRegexp("/<input class='uploader cloudinary-fileupload' data-cloudinary-field='image' data-form-data='{\"timestamp\":\d+,\"public_id\":\"hello\",\"signature\":\"[0-9a-f]+\",\"api_key\":\"1234\"}' data-url='https:\/\/api.cloudinary.com\/v1_1\/test123\/auto\/upload' name='file' type='file'\/>/", $tag);
}
function test_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");
}
/**
* @expectedException \Cloudinary\Error
* @expectedExceptionMessage Illegal value
*/
function test_ocr_info() {
// should support requesting ocr info
\Cloudinary\Uploader::upload("tests/logo.png", array("ocr" => "illegal"));
}
/**
* @expectedException \Cloudinary\Error
* @expectedExceptionMessage Illegal value
*/
function test_raw_conversion() {
// should support requesting raw_convert
\Cloudinary\Uploader::upload("tests/docx.docx", array("resource_type"=>"raw", "raw_convert" => "illegal"));
}
/**
* @expectedException \Cloudinary\Error
* @expectedExceptionMessage Illegal value
*/
function test_categorization() {
// should support requesting categorization
\Cloudinary\Uploader::upload("tests/logo.png", array("categorization" => "illegal"));
}
/**
* @expectedException \Cloudinary\Error
* @expectedExceptionMessage Illegal value
*/
function test_detection() {
// should support requesting detection
\Cloudinary\Uploader::upload("tests/logo.png", array("detection" => "illegal"));
}
/**
* @expectedException \Cloudinary\Error
* @expectedExceptionMessage Illegal value
*/
function test_similarity() {
// should support requesting similarity_search
\Cloudinary\Uploader::upload("tests/logo.png", array("similarity_search" => "illegal"));
}
/**
* @expectedException \Cloudinary\Error
* @expectedExceptionMessage Must use
*/
function test_auto_tagging() {
// should support requesting auto_tagging
\Cloudinary\Uploader::upload("tests/logo.png", array("auto_tagging" => 0.5));
}
function test_large_upload() {
\Cloudinary\Uploader::upload_large("tests/docx.docx");
}
}
?>