AvatarHomeTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017, ownCloud GmbH
  4. *
  5. * @author Georg Ehrke <oc.list@georgehrke.com>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\DAV\Tests\Unit\Avatars;
  25. use OCA\DAV\Avatars\AvatarHome;
  26. use OCA\DAV\Avatars\AvatarNode;
  27. use OCP\IAvatar;
  28. use OCP\IAvatarManager;
  29. use Sabre\DAV\Exception\MethodNotAllowed;
  30. use Sabre\DAV\Exception\NotFound;
  31. use Test\TestCase;
  32. class AvatarHomeTest extends TestCase {
  33. /** @var AvatarHome */
  34. private $home;
  35. /** @var IAvatarManager | \PHPUnit_Framework_MockObject_MockObject */
  36. private $avatarManager;
  37. protected function setUp(): void {
  38. parent::setUp();
  39. $this->avatarManager = $this->createMock(IAvatarManager::class);
  40. $this->home = new AvatarHome(['uri' => 'principals/users/admin'], $this->avatarManager);
  41. }
  42. /**
  43. * @dataProvider providesForbiddenMethods
  44. */
  45. public function testForbiddenMethods($method) {
  46. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  47. $this->home->$method('');
  48. }
  49. public function providesForbiddenMethods() {
  50. return [
  51. ['createFile'],
  52. ['createDirectory'],
  53. ['delete'],
  54. ['setName']
  55. ];
  56. }
  57. public function testGetName() {
  58. $n = $this->home->getName();
  59. self::assertEquals('admin', $n);
  60. }
  61. public function providesTestGetChild() {
  62. return [
  63. [MethodNotAllowed::class, false, ''],
  64. [MethodNotAllowed::class, false, 'bla.foo'],
  65. [MethodNotAllowed::class, false, 'bla.png'],
  66. [NotFound::class, false, '512.png'],
  67. [null, true, '512.png'],
  68. ];
  69. }
  70. /**
  71. * @dataProvider providesTestGetChild
  72. */
  73. public function testGetChild($expectedException, $hasAvatar, $path) {
  74. if ($expectedException !== null) {
  75. $this->expectException($expectedException);
  76. }
  77. $avatar = $this->createMock(IAvatar::class);
  78. $avatar->method('exists')->willReturn($hasAvatar);
  79. $this->avatarManager->expects($this->any())->method('getAvatar')->with('admin')->willReturn($avatar);
  80. $avatarNode = $this->home->getChild($path);
  81. $this->assertInstanceOf(AvatarNode::class, $avatarNode);
  82. }
  83. public function testGetChildren() {
  84. $avatarNodes = $this->home->getChildren();
  85. self::assertEquals(0, count($avatarNodes));
  86. $avatar = $this->createMock(IAvatar::class);
  87. $avatar->expects($this->once())->method('exists')->willReturn(true);
  88. $this->avatarManager->expects($this->any())->method('getAvatar')->with('admin')->willReturn($avatar);
  89. $avatarNodes = $this->home->getChildren();
  90. self::assertEquals(1, count($avatarNodes));
  91. }
  92. /**
  93. * @dataProvider providesTestGetChild
  94. */
  95. public function testChildExists($expectedException, $hasAvatar, $path) {
  96. $avatar = $this->createMock(IAvatar::class);
  97. $avatar->method('exists')->willReturn($hasAvatar);
  98. $this->avatarManager->expects($this->any())->method('getAvatar')->with('admin')->willReturn($avatar);
  99. $childExists = $this->home->childExists($path);
  100. $this->assertEquals($hasAvatar, $childExists);
  101. }
  102. public function testGetLastModified() {
  103. self::assertNull($this->home->getLastModified());
  104. }
  105. }