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