]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/front/controller/admin/ThumbnailsControllerTest.php
e5749654bec896f8866e4daee2b79a692cbe2d80
[github/shaarli/Shaarli.git] / tests / front / controller / admin / ThumbnailsControllerTest.php
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Shaarli\Front\Controller\Admin;
6
7 use Shaarli\Bookmark\Bookmark;
8 use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
9 use Shaarli\TestCase;
10 use Shaarli\Thumbnailer;
11 use Slim\Http\Request;
12 use Slim\Http\Response;
13
14 class ThumbnailsControllerTest extends TestCase
15 {
16 use FrontAdminControllerMockHelper;
17
18 /** @var ThumbnailsController */
19 protected $controller;
20
21 public function setUp(): void
22 {
23 $this->createContainer();
24
25 $this->controller = new ThumbnailsController($this->container);
26 }
27
28 /**
29 * Test displaying the thumbnails update page
30 * Note that only non-note and HTTP bookmarks should be returned.
31 */
32 public function testIndex(): void
33 {
34 $assignedVariables = [];
35 $this->assignTemplateVars($assignedVariables);
36
37 $request = $this->createMock(Request::class);
38 $response = new Response();
39
40 $this->container->bookmarkService
41 ->expects(static::once())
42 ->method('search')
43 ->willReturn([
44 (new Bookmark())->setId(1)->setUrl('http://url1.tld')->setTitle('Title 1'),
45 (new Bookmark())->setId(2)->setUrl('?abcdef')->setTitle('Note 1'),
46 (new Bookmark())->setId(3)->setUrl('http://url2.tld')->setTitle('Title 2'),
47 (new Bookmark())->setId(4)->setUrl('ftp://domain.tld', ['ftp'])->setTitle('FTP'),
48 ])
49 ;
50
51 $result = $this->controller->index($request, $response);
52
53 static::assertSame(200, $result->getStatusCode());
54 static::assertSame('thumbnails', (string) $result->getBody());
55
56 static::assertSame('Thumbnails update - Shaarli', $assignedVariables['pagetitle']);
57 static::assertSame([1, 3], $assignedVariables['ids']);
58 }
59
60 /**
61 * Test updating a bookmark thumbnail with valid parameters
62 */
63 public function testAjaxUpdateValid(): void
64 {
65 $request = $this->createMock(Request::class);
66 $response = new Response();
67
68 $bookmark = (new Bookmark())
69 ->setId($id = 123)
70 ->setUrl($url = 'http://url1.tld')
71 ->setTitle('Title 1')
72 ->setThumbnail(false)
73 ;
74
75 $this->container->thumbnailer = $this->createMock(Thumbnailer::class);
76 $this->container->thumbnailer
77 ->expects(static::once())
78 ->method('get')
79 ->with($url)
80 ->willReturn($thumb = 'http://img.tld/pic.png')
81 ;
82
83 $this->container->bookmarkService
84 ->expects(static::once())
85 ->method('get')
86 ->with($id)
87 ->willReturn($bookmark)
88 ;
89 $this->container->bookmarkService
90 ->expects(static::once())
91 ->method('set')
92 ->willReturnCallback(function (Bookmark $bookmark) use ($thumb): Bookmark {
93 static::assertSame($thumb, $bookmark->getThumbnail());
94
95 return $bookmark;
96 })
97 ;
98
99 $result = $this->controller->ajaxUpdate($request, $response, ['id' => (string) $id]);
100
101 static::assertSame(200, $result->getStatusCode());
102
103 $payload = json_decode((string) $result->getBody(), true);
104
105 static::assertSame($id, $payload['id']);
106 static::assertSame($url, $payload['url']);
107 static::assertSame($thumb, $payload['thumbnail']);
108 }
109
110 /**
111 * Test updating a bookmark thumbnail - Invalid ID
112 */
113 public function testAjaxUpdateInvalidId(): void
114 {
115 $request = $this->createMock(Request::class);
116 $response = new Response();
117
118 $result = $this->controller->ajaxUpdate($request, $response, ['id' => 'nope']);
119
120 static::assertSame(400, $result->getStatusCode());
121 }
122
123 /**
124 * Test updating a bookmark thumbnail - No ID
125 */
126 public function testAjaxUpdateNoId(): void
127 {
128 $request = $this->createMock(Request::class);
129 $response = new Response();
130
131 $result = $this->controller->ajaxUpdate($request, $response, []);
132
133 static::assertSame(400, $result->getStatusCode());
134 }
135
136 /**
137 * Test updating a bookmark thumbnail with valid parameters
138 */
139 public function testAjaxUpdateBookmarkNotFound(): void
140 {
141 $id = 123;
142 $request = $this->createMock(Request::class);
143 $response = new Response();
144
145 $this->container->bookmarkService
146 ->expects(static::once())
147 ->method('get')
148 ->with($id)
149 ->willThrowException(new BookmarkNotFoundException())
150 ;
151
152 $result = $this->controller->ajaxUpdate($request, $response, ['id' => (string) $id]);
153
154 static::assertSame(404, $result->getStatusCode());
155 }
156 }