diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-05-22 13:20:31 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-07-23 21:19:21 +0200 |
commit | 2899ebb5b5e82890c877151f5c02045266ac9973 (patch) | |
tree | 0c4e2684c7f6d161f92a21181bfa4b2f78d6a82f /tests/front/controller/visitor/PictureWallControllerTest.php | |
parent | af290059d10319e76d1e7d78b592cab99c26d91a (diff) | |
download | Shaarli-2899ebb5b5e82890c877151f5c02045266ac9973.tar.gz Shaarli-2899ebb5b5e82890c877151f5c02045266ac9973.tar.zst Shaarli-2899ebb5b5e82890c877151f5c02045266ac9973.zip |
Initialize admin Slim controllers
- Reorganize visitor controllers
- Fix redirection with Slim's requests base path
- Fix daily links
Diffstat (limited to 'tests/front/controller/visitor/PictureWallControllerTest.php')
-rw-r--r-- | tests/front/controller/visitor/PictureWallControllerTest.php | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/tests/front/controller/visitor/PictureWallControllerTest.php b/tests/front/controller/visitor/PictureWallControllerTest.php new file mode 100644 index 00000000..7ac842cb --- /dev/null +++ b/tests/front/controller/visitor/PictureWallControllerTest.php | |||
@@ -0,0 +1,125 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Visitor; | ||
6 | |||
7 | use PHPUnit\Framework\TestCase; | ||
8 | use Shaarli\Bookmark\Bookmark; | ||
9 | use Shaarli\Config\ConfigManager; | ||
10 | use Shaarli\Front\Exception\ThumbnailsDisabledException; | ||
11 | use Shaarli\Thumbnailer; | ||
12 | use Slim\Http\Request; | ||
13 | use Slim\Http\Response; | ||
14 | |||
15 | class PictureWallControllerTest extends TestCase | ||
16 | { | ||
17 | use FrontControllerMockHelper; | ||
18 | |||
19 | /** @var PictureWallController */ | ||
20 | protected $controller; | ||
21 | |||
22 | public function setUp(): void | ||
23 | { | ||
24 | $this->createContainer(); | ||
25 | |||
26 | $this->controller = new PictureWallController($this->container); | ||
27 | } | ||
28 | |||
29 | public function testValidControllerInvokeDefault(): void | ||
30 | { | ||
31 | $this->createValidContainerMockSet(); | ||
32 | |||
33 | $request = $this->createMock(Request::class); | ||
34 | $request->expects(static::once())->method('getQueryParams')->willReturn([]); | ||
35 | $response = new Response(); | ||
36 | |||
37 | // ConfigManager: thumbnails are enabled | ||
38 | $this->container->conf = $this->createMock(ConfigManager::class); | ||
39 | $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) { | ||
40 | if ($parameter === 'thumbnails.mode') { | ||
41 | return Thumbnailer::MODE_COMMON; | ||
42 | } | ||
43 | |||
44 | return $default; | ||
45 | }); | ||
46 | |||
47 | // Save RainTPL assigned variables | ||
48 | $assignedVariables = []; | ||
49 | $this->assignTemplateVars($assignedVariables); | ||
50 | |||
51 | // Links dataset: 2 links with thumbnails | ||
52 | $this->container->bookmarkService | ||
53 | ->expects(static::once()) | ||
54 | ->method('search') | ||
55 | ->willReturnCallback(function (array $parameters, ?string $visibility): array { | ||
56 | // Visibility is set through the container, not the call | ||
57 | static::assertNull($visibility); | ||
58 | |||
59 | // No query parameters | ||
60 | if (count($parameters) === 0) { | ||
61 | return [ | ||
62 | (new Bookmark())->setId(1)->setUrl('http://url.tld')->setThumbnail('thumb1'), | ||
63 | (new Bookmark())->setId(2)->setUrl('http://url2.tld'), | ||
64 | (new Bookmark())->setId(3)->setUrl('http://url3.tld')->setThumbnail('thumb2'), | ||
65 | ]; | ||
66 | } | ||
67 | }) | ||
68 | ; | ||
69 | |||
70 | // Make sure that PluginManager hook is triggered | ||
71 | $this->container->pluginManager | ||
72 | ->expects(static::at(0)) | ||
73 | ->method('executeHooks') | ||
74 | ->willReturnCallback(function (string $hook, array $data, array $param): array { | ||
75 | static::assertSame('render_picwall', $hook); | ||
76 | static::assertArrayHasKey('linksToDisplay', $data); | ||
77 | static::assertCount(2, $data['linksToDisplay']); | ||
78 | static::assertSame(1, $data['linksToDisplay'][0]['id']); | ||
79 | static::assertSame(3, $data['linksToDisplay'][1]['id']); | ||
80 | static::assertArrayHasKey('loggedin', $param); | ||
81 | |||
82 | return $data; | ||
83 | }); | ||
84 | |||
85 | $result = $this->controller->index($request, $response); | ||
86 | |||
87 | static::assertSame(200, $result->getStatusCode()); | ||
88 | static::assertSame('picwall', (string) $result->getBody()); | ||
89 | static::assertSame('Picture wall - Shaarli', $assignedVariables['pagetitle']); | ||
90 | static::assertCount(2, $assignedVariables['linksToDisplay']); | ||
91 | |||
92 | $link = $assignedVariables['linksToDisplay'][0]; | ||
93 | |||
94 | static::assertSame(1, $link['id']); | ||
95 | static::assertSame('http://url.tld', $link['url']); | ||
96 | static::assertSame('thumb1', $link['thumbnail']); | ||
97 | |||
98 | $link = $assignedVariables['linksToDisplay'][1]; | ||
99 | |||
100 | static::assertSame(3, $link['id']); | ||
101 | static::assertSame('http://url3.tld', $link['url']); | ||
102 | static::assertSame('thumb2', $link['thumbnail']); | ||
103 | } | ||
104 | |||
105 | public function testControllerWithThumbnailsDisabled(): void | ||
106 | { | ||
107 | $this->expectException(ThumbnailsDisabledException::class); | ||
108 | |||
109 | $this->createValidContainerMockSet(); | ||
110 | |||
111 | $request = $this->createMock(Request::class); | ||
112 | $response = new Response(); | ||
113 | |||
114 | // ConfigManager: thumbnails are disabled | ||
115 | $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) { | ||
116 | if ($parameter === 'thumbnails.mode') { | ||
117 | return Thumbnailer::MODE_NONE; | ||
118 | } | ||
119 | |||
120 | return $default; | ||
121 | }); | ||
122 | |||
123 | $this->controller->index($request, $response); | ||
124 | } | ||
125 | } | ||