avatar.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. /**
  10. * Class Test_Avatar
  11. *
  12. * @group DB
  13. */
  14. class Test_Avatar extends \Test\TestCase {
  15. private static $trashBinStatus;
  16. /** @var @var string */
  17. private $user;
  18. protected function setUp() {
  19. parent::setUp();
  20. $this->user = $this->getUniqueID();
  21. $storage = new \OC\Files\Storage\Temporary(array());
  22. \OC\Files\Filesystem::mount($storage, array(), '/' . $this->user . '/');
  23. }
  24. public static function setUpBeforeClass() {
  25. self::$trashBinStatus = \OC_App::isEnabled('files_trashbin');
  26. \OC_App::disable('files_trashbin');
  27. }
  28. public static function tearDownAfterClass() {
  29. if (self::$trashBinStatus) {
  30. \OC_App::enable('files_trashbin');
  31. }
  32. }
  33. /**
  34. * @return array
  35. */
  36. public function traversalProvider() {
  37. return [
  38. ['Pot\..\entiallyDangerousUsername'],
  39. ['Pot/..\entiallyDangerousUsername'],
  40. ['PotentiallyDangerousUsername/..'],
  41. ['PotentiallyDangerousUsername\../'],
  42. ['/../PotentiallyDangerousUsername'],
  43. ];
  44. }
  45. /**
  46. * @dataProvider traversalProvider
  47. * @expectedException \Exception
  48. * @expectedExceptionMessage Username may not contain slashes
  49. * @param string $dangerousUsername
  50. */
  51. public function testAvatarTraversal($dangerousUsername) {
  52. new Avatar($dangerousUsername);
  53. }
  54. public function testAvatar() {
  55. $avatar = new Avatar($this->user);
  56. $this->assertEquals(false, $avatar->get());
  57. $expected = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  58. $expected->resize(64);
  59. $avatar->set($expected->data());
  60. $this->assertEquals($expected->data(), $avatar->get()->data());
  61. $avatar->remove();
  62. $this->assertEquals(false, $avatar->get());
  63. }
  64. public function testAvatarApi() {
  65. $avatarManager = \OC::$server->getAvatarManager();
  66. $avatar = $avatarManager->getAvatar($this->user);
  67. $this->assertEquals(false, $avatar->get());
  68. $expected = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
  69. $expected->resize(64);
  70. $avatar->set($expected->data());
  71. $this->assertEquals($expected->data(), $avatar->get()->data());
  72. $avatar->remove();
  73. $this->assertEquals(false, $avatar->get());
  74. }
  75. }