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