ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
LegacyGotoHandlerTest.php
Go to the documentation of this file.
1 <?php
2 
19 namespace ILIAS\StaticURL\Tests;
20 
29 use ILIAS\Data\URI;
32 
33 require_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(): array
77  {
78  return [
79  ['https://ilias.domain/goto.php?client_id=unittest&target=impr', 'impr'],
80  ['https://ilias.domain/goto.php?target=root_1&client_id=unittest', 'root_1'],
81  ['https://ilias.domain/go/root/1', 'root_1'],
82  ['https://ilias.domain/goto.php?target=root_1&client_id=unittest&lang=de', 'root_1'],
83  ['https://ilias.domain/sub/goto.php?target=root_1&client_id=unittest&lang=de', 'root_1'],
84  ['https://ilias.domain/goto.php?target=crs_256&client_id=unittest&lang=de', 'crs_256'],
85  ['https://ilias.domain/goto.php?target=lorem_256&client_id=unittest&lang=de', 'lorem_256'],
86  ['https://ilias.domain/goto.php?target=wiki_wpage_4826_86154&client_id=unittest&lang=de', 'wiki_wpage_4826_86154'],
87  ['https://ilias.domain/sub/goto.php?target=wiki_wpage_4826_86154&client_id=unittest&lang=de', 'wiki_wpage_4826_86154'],
88  ['https://ilias.domain/goto.php/wiki/wpage_4826_86154', 'wiki_wpage_4826_86154'],
89  ['https://ilias.domain/go/wiki/wpage_4826_86154', 'wiki_wpage_4826_86154'],
90  ['https://ilias.domain/sub/goto.php/wiki/wpage_4826_86154', 'wiki_wpage_4826_86154'],
91  ['https://ilias.domain/sub/go/wiki/wpage_4826_86154', 'wiki_wpage_4826_86154'],
92  ];
93  }
94 
95  #[DataProvider('urlProvider')]
96  public function testBase(string $called_url, string $target): void
97  {
98  $this->http_mock->request()->method('getUri')->willReturn(new \GuzzleHttp\Psr7\Uri($called_url));
99  $this->assertEquals($called_url, $this->http_mock->request()->getUri());
100 
101  $uri = new URI($called_url);
102  //$this->assertGreaterThanOrEqual(2, count($uri->getParameters()));
103 
104  $this->updateRequestAndWrapperMockWithParams($uri->getParameters());
105 
106  $request = $this->request_builder->buildRequest(
107  $this->http_mock,
108  $this->refinery,
109  []
110  );
111 
112  $this->assertInstanceOf(Request::class, $request);
113  $this->assertTrue($this->subject->canHandle($request));
114  $this->assertEquals($target, $request->getAdditionalParameters()[LegacyGotoHandler::TARGET]);
115 
116  }
117 
118  private function insertDIC(string $key, object $value): void
119  {
120  global $DIC;
121  $DIC = $DIC instanceof Container ? $DIC : new Container();
122  if (isset($DIC[$key])) {
123  $this->dic_backup[$key] = clone $DIC[$key];
124  }
125  $GLOBALS[$key] = $value;
126  $DIC->offsetUnset($key);
127  $DIC[$key] = (static fn(): \ilCtrlInterface|Services|MockObject => $value);
128  }
129 
130  protected function tearDown(): void
131  {
132  global $DIC;
133  $DIC = $DIC instanceof Container ? $DIC : new Container();
134  foreach ($this->dic_backup as $key => $value) {
135  $DIC->offsetUnset($key);
136  $DIC[$key] = $value;
137  $GLOBALS[$key] = $value;
138  }
139  }
140 
141  protected function buildDependecies(): void
142  {
143  $this->insertDIC('component.factory', $this->component_factory_mock);
144  $this->insertDIC('ctrl', $this->ctrl);
145  $access_mock = $this->createMock(\ilAccessHandler::class);
146  $access_mock->method('checkAccess')->willReturn(true);
147  $this->insertDIC('ilAccess', $access_mock);
148  $this->insertDIC('objDefinition', $this->createMock(\ilObjectDefinition::class));
149  $user_mock = $this->createMock(\ilObjUser::class);
150  $user_mock->method('getId')->willReturn(42);
151  $this->insertDIC('ilUser', $user_mock);
152  $this->insertDIC('http', $this->http_mock);
153  $this->insertDIC('tpl', $this->createMock(\ilGlobalTemplateInterface::class));
154  $this->insertDIC('lng', $this->createMock(\ilLanguage::class));
155  $this->insertDIC('ilObjDataCache', $this->createMock(\ilObjectDataCache::class));
156  $this->insertDIC('ilDB', $this->createMock(\ilDBInterface::class));
157  $this->insertDIC('tree', $this->createMock(\ilTree::class));
158  $this->insertDIC('rbacreview', $this->createMock(\ilRbacReview::class));
159  $this->insertDIC('ilSetting', $this->createMock(\ilSetting::class));
160  $this->insertDIC('ilErr', $this->createMock(\ilErrorHandling::class));
161  $this->insertDIC('ilCtrl', $this->ctrl);
162 
163  if (!defined('ROOT_FOLDER_ID')) {
164  define('ROOT_FOLDER_ID', 1);
165  }
166 
167  if (!defined('ANONYMOUS_USER_ID')) {
168  define('ANONYMOUS_USER_ID', 13);
169  }
170  }
171 
172 }
if(! $DIC->user() ->getId()||!ilLTIConsumerAccess::hasCustomProviderCreationAccess()) $params
Definition: ltiregstart.php:31
Interface Observer Contains several chained tasks and infos about them.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Base.php:19
testBase(string $called_url, string $target)
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
$GLOBALS["DIC"]
Definition: wac.php:53
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:22