ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
LegacyGotoHandlerTest.php
Go to the documentation of this file.
1<?php
2
19namespace ILIAS\StaticURL\Tests;
20
21use PHPUnit\Framework\MockObject\MockObject;
23use PHPUnit\Framework\Attributes\DataProvider;
26use Psr\Http\Message\ServerRequestInterface;
32
33require_once "Base.php";
34
39{
40 public MockObject $request_mock;
41 public MockObject $component_factory_mock;
42 public $refinery;
44 private \ilCtrlInterface|MockObject $ctrl;
49 private Services|MockObject $http_mock;
50 private array $dic_backup = [];
51
52 protected function setUp(): void
53 {
54 $this->ctrl = $this->createMock(\ilCtrlInterface::class);
55 $this->http_mock = $this->createMock(Services::class);
56 $this->request_mock = $this->createMock(ServerRequestInterface::class);
57 $this->http_mock->method('request')->willReturn($this->request_mock);
58
59 $this->component_factory_mock = $this->createMock(\ilComponentFactory::class);
60
61 $this->refinery = new Factory(
62 new \ILIAS\Data\Factory(),
63 $this->createMock(\ilLanguage::class),
64 );
65
66 $this->request_builder = new LegacyRequestBuilder();
67 $this->subject = new LegacyGotoHandler();
68 }
69
70 private function updateRequestAndWrapperMockWithParams(array $params): void
71 {
72 $this->request_mock->method('getQueryParams')->willReturn($params);
73 $this->http_mock->method('wrapper')->willReturn(new WrapperFactory($this->request_mock));
74 }
75
76 public static function urlProvider(): \Iterator
77 {
78 yield ['https://ilias.domain/goto.php?client_id=unittest&target=impr', 'impr'];
79 yield ['https://ilias.domain/goto.php?target=root_1&client_id=unittest', 'root_1'];
80 yield ['https://ilias.domain/go/root/1', 'root_1'];
81 yield ['https://ilias.domain/goto.php?target=root_1&client_id=unittest&lang=de', 'root_1'];
82 yield ['https://ilias.domain/sub/goto.php?target=root_1&client_id=unittest&lang=de', 'root_1'];
83 yield ['https://ilias.domain/goto.php?target=crs_256&client_id=unittest&lang=de', 'crs_256'];
84 yield ['https://ilias.domain/goto.php?target=lorem_256&client_id=unittest&lang=de', 'lorem_256'];
85 yield ['https://ilias.domain/goto.php?target=wiki_wpage_4826_86154&client_id=unittest&lang=de', 'wiki_wpage_4826_86154'];
86 yield ['https://ilias.domain/sub/goto.php?target=wiki_wpage_4826_86154&client_id=unittest&lang=de', 'wiki_wpage_4826_86154'];
87 yield ['https://ilias.domain/goto.php/wiki/wpage_4826_86154', 'wiki_wpage_4826_86154'];
88 yield ['https://ilias.domain/go/wiki/wpage_4826_86154', 'wiki_wpage_4826_86154'];
89 yield ['https://ilias.domain/sub/goto.php/wiki/wpage_4826_86154', 'wiki_wpage_4826_86154'];
90 yield ['https://ilias.domain/sub/go/wiki/wpage_4826_86154', 'wiki_wpage_4826_86154'];
91 }
92
93 #[DataProvider('urlProvider')]
94 public function testBase(string $called_url, string $target): void
95 {
96 $this->http_mock->request()->method('getUri')->willReturn(new \GuzzleHttp\Psr7\Uri($called_url));
97 $this->assertEquals($called_url, $this->http_mock->request()->getUri());
98
99 $uri = new URI($called_url);
100 //$this->assertGreaterThanOrEqual(2, count($uri->getParameters()));
101
102 $this->updateRequestAndWrapperMockWithParams($uri->getParameters());
103
104 $request = $this->request_builder->buildRequest(
105 $this->http_mock,
106 $this->refinery,
107 []
108 );
109
110 $this->assertInstanceOf(Request::class, $request);
111 $this->assertTrue($this->subject->canHandle($request));
112 $this->assertEquals($target, $request->getAdditionalParameters()[LegacyGotoHandler::TARGET]);
113
114 }
115
116 private function insertDIC(string $key, object $value): void
117 {
118 global $DIC;
119 $DIC = $DIC instanceof Container ? $DIC : new Container();
120 if (isset($DIC[$key])) {
121 $this->dic_backup[$key] = clone $DIC[$key];
122 }
123 $GLOBALS[$key] = $value;
124 $DIC->offsetUnset($key);
125 $DIC[$key] = (static fn(): \ilCtrlInterface|Services|MockObject => $value);
126 }
127
128 protected function tearDown(): void
129 {
130 global $DIC;
131 $DIC = $DIC instanceof Container ? $DIC : new Container();
132 foreach ($this->dic_backup as $key => $value) {
133 $DIC->offsetUnset($key);
134 $DIC[$key] = $value;
135 $GLOBALS[$key] = $value;
136 }
137 }
138
139 protected function buildDependecies(): void
140 {
141 $this->insertDIC('component.factory', $this->component_factory_mock);
142 $this->insertDIC('ctrl', $this->ctrl);
143 $access_mock = $this->createMock(\ilAccessHandler::class);
144 $access_mock->method('checkAccess')->willReturn(true);
145 $this->insertDIC('ilAccess', $access_mock);
146 $this->insertDIC('objDefinition', $this->createMock(\ilObjectDefinition::class));
147 $user_mock = $this->createMock(\ilObjUser::class);
148 $user_mock->method('getId')->willReturn(42);
149 $this->insertDIC('ilUser', $user_mock);
150 $this->insertDIC('http', $this->http_mock);
151 $this->insertDIC('tpl', $this->createMock(\ilGlobalTemplateInterface::class));
152 $this->insertDIC('lng', $this->createMock(\ilLanguage::class));
153 $this->insertDIC('ilObjDataCache', $this->createMock(\ilObjectDataCache::class));
154 $this->insertDIC('ilDB', $this->createMock(\ilDBInterface::class));
155 $this->insertDIC('tree', $this->createMock(\ilTree::class));
156 $this->insertDIC('rbacreview', $this->createMock(\ilRbacReview::class));
157 $this->insertDIC('ilSetting', $this->createMock(\ilSetting::class));
158 $this->insertDIC('ilErr', $this->createMock(\ilErrorHandling::class));
159 $this->insertDIC('ilCtrl', $this->ctrl);
160
161 if (!defined('ROOT_FOLDER_ID')) {
162 define('ROOT_FOLDER_ID', 1);
163 }
164
165 if (!defined('ANONYMOUS_USER_ID')) {
166 define('ANONYMOUS_USER_ID', 13);
167 }
168 }
169
170}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
Builds data types.
Definition: Factory.php:36
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
Class Services.
Definition: Services.php:38
testBase(string $called_url, string $target)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if(! $DIC->user() ->getId()||!ilLTIConsumerAccess::hasCustomProviderCreationAccess()) $params
Definition: ltiregstart.php:31
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Base.php:19
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $DIC
Definition: shib_login.php:26
$GLOBALS["DIC"]
Definition: wac.php:54