Avatar.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2020, Daniel Calviño Sánchez (danxuliu@gmail.com)
  4. *
  5. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. use Behat\Gherkin\Node\TableNode;
  24. use PHPUnit\Framework\Assert;
  25. require __DIR__ . '/../../vendor/autoload.php';
  26. trait Avatar {
  27. /** @var string **/
  28. private $lastAvatar;
  29. /** @AfterScenario **/
  30. public function cleanupLastAvatar() {
  31. $this->lastAvatar = null;
  32. }
  33. private function getLastAvatar() {
  34. $this->lastAvatar = '';
  35. $body = $this->response->getBody();
  36. while (!$body->eof()) {
  37. $this->lastAvatar .= $body->read(8192);
  38. }
  39. $body->close();
  40. }
  41. /**
  42. * @When user :user gets avatar for user :userAvatar
  43. *
  44. * @param string $user
  45. * @param string $userAvatar
  46. */
  47. public function userGetsAvatarForUser(string $user, string $userAvatar) {
  48. $this->userGetsAvatarForUserWithSize($user, $userAvatar, '128');
  49. }
  50. /**
  51. * @When user :user gets avatar for user :userAvatar with size :size
  52. *
  53. * @param string $user
  54. * @param string $userAvatar
  55. * @param string $size
  56. */
  57. public function userGetsAvatarForUserWithSize(string $user, string $userAvatar, string $size) {
  58. $this->asAn($user);
  59. $this->sendingToDirectUrl('GET', '/index.php/avatar/' . $userAvatar . '/' . $size);
  60. $this->theHTTPStatusCodeShouldBe('200');
  61. $this->getLastAvatar();
  62. }
  63. /**
  64. * @When user :user gets avatar for guest :guestAvatar
  65. *
  66. * @param string $user
  67. * @param string $guestAvatar
  68. */
  69. public function userGetsAvatarForGuest(string $user, string $guestAvatar) {
  70. $this->asAn($user);
  71. $this->sendingToDirectUrl('GET', '/index.php/avatar/guest/' . $guestAvatar . '/128');
  72. $this->theHTTPStatusCodeShouldBe('201');
  73. $this->getLastAvatar();
  74. }
  75. /**
  76. * @When logged in user gets temporary avatar
  77. */
  78. public function loggedInUserGetsTemporaryAvatar() {
  79. $this->loggedInUserGetsTemporaryAvatarWith('200');
  80. }
  81. /**
  82. * @When logged in user gets temporary avatar with :statusCode
  83. *
  84. * @param string $statusCode
  85. */
  86. public function loggedInUserGetsTemporaryAvatarWith(string $statusCode) {
  87. $this->sendingAToWithRequesttoken('GET', '/index.php/avatar/tmp');
  88. $this->theHTTPStatusCodeShouldBe($statusCode);
  89. $this->getLastAvatar();
  90. }
  91. /**
  92. * @When logged in user posts temporary avatar from file :source
  93. *
  94. * @param string $source
  95. */
  96. public function loggedInUserPostsTemporaryAvatarFromFile(string $source) {
  97. $file = \GuzzleHttp\Psr7\Utils::streamFor(fopen($source, 'r'));
  98. $this->sendingAToWithRequesttoken('POST', '/index.php/avatar',
  99. [
  100. 'multipart' => [
  101. [
  102. 'name' => 'files[]',
  103. 'contents' => $file
  104. ]
  105. ]
  106. ]);
  107. $this->theHTTPStatusCodeShouldBe('200');
  108. }
  109. /**
  110. * @When logged in user posts temporary avatar from internal path :path
  111. *
  112. * @param string $path
  113. */
  114. public function loggedInUserPostsTemporaryAvatarFromInternalPath(string $path) {
  115. $this->sendingAToWithRequesttoken('POST', '/index.php/avatar?path=' . $path);
  116. $this->theHTTPStatusCodeShouldBe('200');
  117. }
  118. /**
  119. * @When logged in user crops temporary avatar
  120. *
  121. * @param TableNode $crop
  122. */
  123. public function loggedInUserCropsTemporaryAvatar(TableNode $crop) {
  124. $this->loggedInUserCropsTemporaryAvatarWith('200', $crop);
  125. }
  126. /**
  127. * @When logged in user crops temporary avatar with :statusCode
  128. *
  129. * @param string $statusCode
  130. * @param TableNode $crop
  131. */
  132. public function loggedInUserCropsTemporaryAvatarWith(string $statusCode, TableNode $crop) {
  133. $parameters = [];
  134. foreach ($crop->getRowsHash() as $key => $value) {
  135. $parameters[] = 'crop[' . $key . ']=' . $value;
  136. }
  137. $this->sendingAToWithRequesttoken('POST', '/index.php/avatar/cropped?' . implode('&', $parameters));
  138. $this->theHTTPStatusCodeShouldBe($statusCode);
  139. }
  140. /**
  141. * @When logged in user deletes the user avatar
  142. */
  143. public function loggedInUserDeletesTheUserAvatar() {
  144. $this->sendingAToWithRequesttoken('DELETE', '/index.php/avatar');
  145. $this->theHTTPStatusCodeShouldBe('200');
  146. }
  147. /**
  148. * @Then last avatar is a square of size :size
  149. *
  150. * @param string size
  151. */
  152. public function lastAvatarIsASquareOfSize(string $size) {
  153. [$width, $height] = getimagesizefromstring($this->lastAvatar);
  154. Assert::assertEquals($width, $height, 'Expected avatar to be a square');
  155. Assert::assertEquals($size, $width);
  156. }
  157. /**
  158. * @Then last avatar is not a square
  159. */
  160. public function lastAvatarIsNotASquare() {
  161. [$width, $height] = getimagesizefromstring($this->lastAvatar);
  162. Assert::assertNotEquals($width, $height, 'Expected avatar to not be a square');
  163. }
  164. /**
  165. * @Then last avatar is not a single color
  166. */
  167. public function lastAvatarIsNotASingleColor() {
  168. Assert::assertEquals(null, $this->getColorFromLastAvatar());
  169. }
  170. /**
  171. * @Then last avatar is a single :color color
  172. *
  173. * @param string $color
  174. * @param string $size
  175. */
  176. public function lastAvatarIsASingleColor(string $color) {
  177. $expectedColor = $this->hexStringToRgbColor($color);
  178. $colorFromLastAvatar = $this->getColorFromLastAvatar();
  179. Assert::assertTrue($this->isSameColor($expectedColor, $colorFromLastAvatar),
  180. $this->rgbColorToHexString($colorFromLastAvatar) . ' does not match expected ' . $color);
  181. }
  182. private function hexStringToRgbColor($hexString) {
  183. // Strip initial "#"
  184. $hexString = substr($hexString, 1);
  185. $rgbColorInt = hexdec($hexString);
  186. // RGBA hex strings are not supported; the given string is assumed to be
  187. // an RGB hex string.
  188. return [
  189. 'red' => ($rgbColorInt >> 16) & 0xFF,
  190. 'green' => ($rgbColorInt >> 8) & 0xFF,
  191. 'blue' => $rgbColorInt & 0xFF,
  192. 'alpha' => 0
  193. ];
  194. }
  195. private function rgbColorToHexString($rgbColor) {
  196. $rgbColorInt = ($rgbColor['red'] << 16) + ($rgbColor['green'] << 8) + ($rgbColor['blue']);
  197. return '#' . str_pad(strtoupper(dechex($rgbColorInt)), 6, '0', STR_PAD_LEFT);
  198. }
  199. private function getColorFromLastAvatar() {
  200. $image = imagecreatefromstring($this->lastAvatar);
  201. $firstPixelColorIndex = imagecolorat($image, 0, 0);
  202. $firstPixelColor = imagecolorsforindex($image, $firstPixelColorIndex);
  203. for ($i = 0; $i < imagesx($image); $i++) {
  204. for ($j = 0; $j < imagesx($image); $j++) {
  205. $currentPixelColorIndex = imagecolorat($image, $i, $j);
  206. $currentPixelColor = imagecolorsforindex($image, $currentPixelColorIndex);
  207. // The colors are compared with a small allowed delta, as even
  208. // on solid color images the resizing can cause some small
  209. // artifacts that slightly modify the color of certain pixels.
  210. if (!$this->isSameColor($firstPixelColor, $currentPixelColor)) {
  211. imagedestroy($image);
  212. return null;
  213. }
  214. }
  215. }
  216. imagedestroy($image);
  217. return $firstPixelColor;
  218. }
  219. private function isSameColor(array $firstColor, array $secondColor, int $allowedDelta = 1) {
  220. if ($this->isSameColorComponent($firstColor['red'], $secondColor['red'], $allowedDelta) &&
  221. $this->isSameColorComponent($firstColor['green'], $secondColor['green'], $allowedDelta) &&
  222. $this->isSameColorComponent($firstColor['blue'], $secondColor['blue'], $allowedDelta) &&
  223. $this->isSameColorComponent($firstColor['alpha'], $secondColor['alpha'], $allowedDelta)) {
  224. return true;
  225. }
  226. return false;
  227. }
  228. private function isSameColorComponent(int $firstColorComponent, int $secondColorComponent, int $allowedDelta) {
  229. if ($firstColorComponent >= ($secondColorComponent - $allowedDelta) &&
  230. $firstColorComponent <= ($secondColorComponent + $allowedDelta)) {
  231. return true;
  232. }
  233. return false;
  234. }
  235. }