diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:05:08 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:05:08 +0200 |
commit | b6f678a5a1d15acf284ebcec16c905e976671ce1 (patch) | |
tree | 33c7da831482ed79c44896ef19c73c72ada84f2e /tests/front/controller/visitor/PictureWallControllerTest.php | |
parent | b14687036b9b800681197f51fdc47e62f0c88e2e (diff) | |
parent | 1c1520b6b98ab20201bfe15577782a52320339df (diff) | |
download | Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.gz Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.zst Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.zip |
Merge branch 'v0.12' into latest
Diffstat (limited to 'tests/front/controller/visitor/PictureWallControllerTest.php')
-rw-r--r-- | tests/front/controller/visitor/PictureWallControllerTest.php | 123 |
1 files changed, 123 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..b868231d --- /dev/null +++ b/tests/front/controller/visitor/PictureWallControllerTest.php | |||
@@ -0,0 +1,123 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Visitor; | ||
6 | |||
7 | use Shaarli\Bookmark\Bookmark; | ||
8 | use Shaarli\Config\ConfigManager; | ||
9 | use Shaarli\Front\Exception\ThumbnailsDisabledException; | ||
10 | use Shaarli\TestCase; | ||
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 | $request = $this->createMock(Request::class); | ||
32 | $request->expects(static::once())->method('getQueryParams')->willReturn([]); | ||
33 | $response = new Response(); | ||
34 | |||
35 | // ConfigManager: thumbnails are enabled | ||
36 | $this->container->conf = $this->createMock(ConfigManager::class); | ||
37 | $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) { | ||
38 | if ($parameter === 'thumbnails.mode') { | ||
39 | return Thumbnailer::MODE_COMMON; | ||
40 | } | ||
41 | |||
42 | return $default; | ||
43 | }); | ||
44 | |||
45 | // Save RainTPL assigned variables | ||
46 | $assignedVariables = []; | ||
47 | $this->assignTemplateVars($assignedVariables); | ||
48 | |||
49 | // Links dataset: 2 links with thumbnails | ||
50 | $this->container->bookmarkService | ||
51 | ->expects(static::once()) | ||
52 | ->method('search') | ||
53 | ->willReturnCallback(function (array $parameters, ?string $visibility): array { | ||
54 | // Visibility is set through the container, not the call | ||
55 | static::assertNull($visibility); | ||
56 | |||
57 | // No query parameters | ||
58 | if (count($parameters) === 0) { | ||
59 | return [ | ||
60 | (new Bookmark())->setId(1)->setUrl('http://url.tld')->setThumbnail('thumb1'), | ||
61 | (new Bookmark())->setId(2)->setUrl('http://url2.tld'), | ||
62 | (new Bookmark())->setId(3)->setUrl('http://url3.tld')->setThumbnail('thumb2'), | ||
63 | ]; | ||
64 | } | ||
65 | }) | ||
66 | ; | ||
67 | |||
68 | // Make sure that PluginManager hook is triggered | ||
69 | $this->container->pluginManager | ||
70 | ->expects(static::atLeastOnce()) | ||
71 | ->method('executeHooks') | ||
72 | ->withConsecutive(['render_picwall']) | ||
73 | ->willReturnCallback(function (string $hook, array $data, array $param): array { | ||
74 | if ('render_picwall' === $hook) { | ||
75 | static::assertArrayHasKey('linksToDisplay', $data); | ||
76 | static::assertCount(2, $data['linksToDisplay']); | ||
77 | static::assertSame(1, $data['linksToDisplay'][0]['id']); | ||
78 | static::assertSame(3, $data['linksToDisplay'][1]['id']); | ||
79 | static::assertArrayHasKey('loggedin', $param); | ||
80 | } | ||
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 | $request = $this->createMock(Request::class); | ||
110 | $response = new Response(); | ||
111 | |||
112 | // ConfigManager: thumbnails are disabled | ||
113 | $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) { | ||
114 | if ($parameter === 'thumbnails.mode') { | ||
115 | return Thumbnailer::MODE_NONE; | ||
116 | } | ||
117 | |||
118 | return $default; | ||
119 | }); | ||
120 | |||
121 | $this->controller->index($request, $response); | ||
122 | } | ||
123 | } | ||