-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathAvatar.php
More file actions
213 lines (177 loc) · 5.91 KB
/
Copy pathAvatar.php
File metadata and controls
213 lines (177 loc) · 5.91 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
<?php
/**
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
use PHPUnit\Framework\Assert;
require __DIR__ . '/autoload.php';
trait Avatar {
/** @var string * */
private $lastAvatar;
/** @AfterScenario **/
public function cleanupLastAvatar() {
$this->lastAvatar = null;
}
private function getLastAvatar() {
$this->lastAvatar = '';
$body = $this->response->getBody();
while (!$body->eof()) {
$this->lastAvatar .= $body->read(8192);
}
$body->close();
}
/**
* @When user :user gets avatar for user :userAvatar
*
* @param string $user
* @param string $userAvatar
*/
public function userGetsAvatarForUser(string $user, string $userAvatar) {
$this->userGetsAvatarForUserWithSize($user, $userAvatar, '128');
}
/**
* @When user :user gets avatar for user :userAvatar with size :size
*
* @param string $user
* @param string $userAvatar
* @param string $size
*/
public function userGetsAvatarForUserWithSize(string $user, string $userAvatar, string $size) {
$this->asAn($user);
$this->sendingToDirectUrl('GET', '/index.php/avatar/' . $userAvatar . '/' . $size);
$this->theHTTPStatusCodeShouldBe('200');
$this->getLastAvatar();
}
/**
* @When user :user gets avatar for guest :guestAvatar
*
* @param string $user
* @param string $guestAvatar
*/
public function userGetsAvatarForGuest(string $user, string $guestAvatar) {
$this->asAn($user);
$this->sendingToDirectUrl('GET', '/index.php/avatar/guest/' . $guestAvatar . '/128');
$this->theHTTPStatusCodeShouldBe('201');
$this->getLastAvatar();
}
/**
* @When logged in user posts avatar from file :source
*
* @param string $source
*/
public function loggedInUserPostsAvatarFromFile(string $source) {
$file = \GuzzleHttp\Psr7\Utils::streamFor(fopen($source, 'r'));
$this->sendingAToWithRequesttoken('POST', '/index.php/avatar',
[
'multipart' => [
[
'name' => 'files[]',
'contents' => $file
]
]
]);
$this->theHTTPStatusCodeShouldBe('200');
}
/**
* @When logged in user posts avatar from internal path :path
*
* @param string $path
*/
public function loggedInUserPostsAvatarFromInternalPath(string $path) {
$this->sendingAToWithRequesttoken('POST', '/index.php/avatar?path=' . $path);
$this->theHTTPStatusCodeShouldBe('200');
}
/**
* @When logged in user deletes the user avatar
*/
public function loggedInUserDeletesTheUserAvatar() {
$this->sendingAToWithRequesttoken('DELETE', '/index.php/avatar');
$this->theHTTPStatusCodeShouldBe('200');
}
/**
* @Then last avatar is a square of size :size
*
* @param string size
*/
public function lastAvatarIsASquareOfSize(string $size) {
[$width, $height] = getimagesizefromstring($this->lastAvatar);
Assert::assertEquals($width, $height, 'Expected avatar to be a square');
Assert::assertEquals($size, $width);
}
/**
* @Then last avatar is not a square
*/
public function lastAvatarIsNotASquare() {
[$width, $height] = getimagesizefromstring($this->lastAvatar);
Assert::assertNotEquals($width, $height, 'Expected avatar to not be a square');
}
/**
* @Then last avatar is not a single color
*/
public function lastAvatarIsNotASingleColor() {
Assert::assertEquals(null, $this->getColorFromLastAvatar());
}
/**
* @Then last avatar is a single :color color
*
* @param string $color
* @param string $size
*/
public function lastAvatarIsASingleColor(string $color) {
$expectedColor = $this->hexStringToRgbColor($color);
$colorFromLastAvatar = $this->getColorFromLastAvatar();
Assert::assertTrue($this->isSameColor($expectedColor, $colorFromLastAvatar),
$this->rgbColorToHexString($colorFromLastAvatar) . ' does not match expected ' . $color);
}
private function hexStringToRgbColor($hexString) {
// Strip initial "#"
$hexString = substr($hexString, 1);
$rgbColorInt = hexdec($hexString);
// RGBA hex strings are not supported; the given string is assumed to be
// an RGB hex string.
return [
'red' => ($rgbColorInt >> 16) & 0xFF,
'green' => ($rgbColorInt >> 8) & 0xFF,
'blue' => $rgbColorInt & 0xFF,
'alpha' => 0
];
}
private function rgbColorToHexString($rgbColor) {
$rgbColorInt = ($rgbColor['red'] << 16) + ($rgbColor['green'] << 8) + ($rgbColor['blue']);
return '#' . str_pad(strtoupper(dechex($rgbColorInt)), 6, '0', STR_PAD_LEFT);
}
private function getColorFromLastAvatar() {
$image = imagecreatefromstring($this->lastAvatar);
$firstPixelColorIndex = imagecolorat($image, 0, 0);
$firstPixelColor = imagecolorsforindex($image, $firstPixelColorIndex);
for ($i = 0; $i < imagesx($image); $i++) {
for ($j = 0; $j < imagesx($image); $j++) {
$currentPixelColorIndex = imagecolorat($image, $i, $j);
$currentPixelColor = imagecolorsforindex($image, $currentPixelColorIndex);
// The colors are compared with a small allowed delta, as even
// on solid color images the resizing can cause some small
// artifacts that slightly modify the color of certain pixels.
if (!$this->isSameColor($firstPixelColor, $currentPixelColor)) {
return null;
}
}
}
return $firstPixelColor;
}
private function isSameColor(array $firstColor, array $secondColor, int $allowedDelta = 1) {
if ($this->isSameColorComponent($firstColor['red'], $secondColor['red'], $allowedDelta)
&& $this->isSameColorComponent($firstColor['green'], $secondColor['green'], $allowedDelta)
&& $this->isSameColorComponent($firstColor['blue'], $secondColor['blue'], $allowedDelta)
&& $this->isSameColorComponent($firstColor['alpha'], $secondColor['alpha'], $allowedDelta)) {
return true;
}
return false;
}
private function isSameColorComponent(int $firstColorComponent, int $secondColorComponent, int $allowedDelta) {
if ($firstColorComponent >= ($secondColorComponent - $allowedDelta)
&& $firstColorComponent <= ($secondColorComponent + $allowedDelta)) {
return true;
}
return false;
}
}