Avatar.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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\stream_for(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, 'Avatar is not a square');
  155. Assert::assertEquals($size, $width);
  156. }
  157. /**
  158. * @Then last avatar is not a single color
  159. */
  160. public function lastAvatarIsNotASingleColor() {
  161. Assert::assertEquals(null, $this->getColorFromLastAvatar());
  162. }
  163. /**
  164. * @Then last avatar is a single :color color
  165. *
  166. * @param string $color
  167. * @param string $size
  168. */
  169. public function lastAvatarIsASingleColor(string $color) {
  170. $expectedColor = $this->hexStringToRgbColor($color);
  171. $colorFromLastAvatar = $this->getColorFromLastAvatar();
  172. Assert::assertTrue($this->isSameColor($expectedColor, $colorFromLastAvatar),
  173. $this->rgbColorToHexString($colorFromLastAvatar) . ' does not match expected ' . $color);
  174. }
  175. private function hexStringToRgbColor($hexString) {
  176. // Strip initial "#"
  177. $hexString = substr($hexString, 1);
  178. $rgbColorInt = hexdec($hexString);
  179. // RGBA hex strings are not supported; the given string is assumed to be
  180. // an RGB hex string.
  181. return [
  182. 'red' => ($rgbColorInt >> 16) & 0xFF,
  183. 'green' => ($rgbColorInt >> 8) & 0xFF,
  184. 'blue' => $rgbColorInt & 0xFF,
  185. 'alpha' => 0
  186. ];
  187. }
  188. private function rgbColorToHexString($rgbColor) {
  189. $rgbColorInt = ($rgbColor['red'] << 16) + ($rgbColor['green'] << 8) + ($rgbColor['blue']);
  190. return '#' . str_pad(strtoupper(dechex($rgbColorInt)), 6, '0', STR_PAD_LEFT);
  191. }
  192. private function getColorFromLastAvatar() {
  193. $image = imagecreatefromstring($this->lastAvatar);
  194. $firstPixelColorIndex = imagecolorat($image, 0, 0);
  195. $firstPixelColor = imagecolorsforindex($image, $firstPixelColorIndex);
  196. for ($i = 0; $i < imagesx($image); $i++) {
  197. for ($j = 0; $j < imagesx($image); $j++) {
  198. $currentPixelColorIndex = imagecolorat($image, $i, $j);
  199. $currentPixelColor = imagecolorsforindex($image, $currentPixelColorIndex);
  200. // The colors are compared with a small allowed delta, as even
  201. // on solid color images the resizing can cause some small
  202. // artifacts that slightly modify the color of certain pixels.
  203. if (!$this->isSameColor($firstPixelColor, $currentPixelColor)) {
  204. imagedestroy($image);
  205. return null;
  206. }
  207. }
  208. }
  209. imagedestroy($image);
  210. return $firstPixelColor;
  211. }
  212. private function isSameColor(array $firstColor, array $secondColor, int $allowedDelta = 1) {
  213. if ($this->isSameColorComponent($firstColor['red'], $secondColor['red'], $allowedDelta) &&
  214. $this->isSameColorComponent($firstColor['green'], $secondColor['green'], $allowedDelta) &&
  215. $this->isSameColorComponent($firstColor['blue'], $secondColor['blue'], $allowedDelta) &&
  216. $this->isSameColorComponent($firstColor['alpha'], $secondColor['alpha'], $allowedDelta)) {
  217. return true;
  218. }
  219. return false;
  220. }
  221. private function isSameColorComponent(int $firstColorComponent, int $secondColorComponent, int $allowedDelta) {
  222. if ($firstColorComponent >= ($secondColorComponent - $allowedDelta) &&
  223. $firstColorComponent <= ($secondColorComponent + $allowedDelta)) {
  224. return true;
  225. }
  226. return false;
  227. }
  228. }