avatar.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. use OC\Avatar;
  9. class Test_Avatar extends \Test\TestCase {
  10. private static $trashBinStatus;
  11. /** @var @var string */
  12. private $user;
  13. protected function setUp() {
  14. parent::setUp();
  15. $this->user = $this->getUniqueID();
  16. $storage = new \OC\Files\Storage\Temporary(array());
  17. \OC\Files\Filesystem::mount($storage, array(), '/' . $this->user . '/');
  18. }
  19. public static function setUpBeforeClass() {
  20. self::$trashBinStatus = \OC_App::isEnabled('files_trashbin');
  21. \OC_App::disable('files_trashbin');
  22. }
  23. public static function tearDownAfterClass() {
  24. if (self::$trashBinStatus) {
  25. \OC_App::enable('files_trashbin');
  26. }
  27. }
  28. /**
  29. * @return array
  30. */
  31. public function traversalProvider() {
  32. return [
  33. ['Pot\..\entiallyDangerousUsername'],
  34. ['Pot/..\entiallyDangerousUsername'],
  35. ['PotentiallyDangerousUsername/..'],
  36. ['PotentiallyDangerousUsername\../'],
  37. ['/../PotentiallyDangerousUsername'],
  38. ];
  39. }
  40. /**
  41. * @dataProvider traversalProvider
  42. * @expectedException \Exception
  43. * @expectedExceptionMessage Username may not contain slashes
  44. * @param string $dangerousUsername
  45. */
  46. public function testAvatarTraversal($dangerousUsername) {
  47. new Avatar($dangerousUsername);
  48. }
  49. public function testAvatar() {
  50. $avatar = new Avatar($this->user);
  51. $this->assertEquals(false, $avatar->get());
  52. $expected = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  53. $expected->resize(64);
  54. $avatar->set($expected->data());
  55. $this->assertEquals($expected->data(), $avatar->get()->data());
  56. $avatar->remove();
  57. $this->assertEquals(false, $avatar->get());
  58. }
  59. public function testAvatarApi() {
  60. $avatarManager = \OC::$server->getAvatarManager();
  61. $avatar = $avatarManager->getAvatar($this->user);
  62. $this->assertEquals(false, $avatar->get());
  63. $expected = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  64. $expected->resize(64);
  65. $avatar->set($expected->data());
  66. $this->assertEquals($expected->data(), $avatar->get()->data());
  67. $avatar->remove();
  68. $this->assertEquals(false, $avatar->get());
  69. }
  70. }