ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
ilLTIToolTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22use PHPUnit\Framework\Attributes\DataProvider;
23use PHPUnit\Framework\TestCase;
24
28final class ilLTIToolTest extends TestCase
29{
30 public static function keywordProvider(): array
31 {
32 return [
33 'list' => ['quiz;video', ['quiz', 'video']],
34 'spaces and empty entries' => [' quiz ; ;video;', ['quiz', 'video']],
35 'empty' => ['', []],
36 ];
37 }
38
39 #[DataProvider('keywordProvider')]
40 public function testSplitKeywords(string $keywords, array $expected): void
41 {
42 $this->assertSame($expected, ilLTITool::splitKeywords($keywords));
43 }
44
45 public static function customParamsProvider(): array
46 {
47 return [
48 'pairs' => ['color=blue;size=10', ['color' => 'blue', 'size' => '10']],
49 'space after the separator' => ['color=blue; size=10', ['color' => 'blue', 'size' => '10']],
50 'equals sign in the value' => ['filter=a=b', ['filter' => 'a=b']],
51 'name without value' => ['debug', ['debug' => '']],
52 'empty' => ['', []],
53 ];
54 }
55
56 #[DataProvider('customParamsProvider')]
57 public function testParseCustomParams(string $params, array $expected): void
58 {
59 $this->assertSame($expected, ilObjLTITool::parseCustomParams($params));
60 }
61
62 public function testNewToolHasDefaults(): void
63 {
64 $tool = new ilLTITool();
65
66 $this->assertSame(0, $tool->getId());
67 $this->assertSame(ilLTITool::VERSION_1P1, $tool->getLtiVersion());
68 $this->assertSame(ilLTITool::AVAILABILITY_NONE, $tool->getAvailability());
69 $this->assertSame(0.8, $tool->getMasteryScore());
70 $this->assertTrue($tool->isKeyCustomizable());
71 $this->assertSame([], $tool->getCustomParamsArray());
72 $this->assertFalse($tool->isSelectableBy(0));
73 }
74
75 public function testToolIsReadFromItsRow(): void
76 {
77 $tool = $this->readTool([
78 'title' => 'Video tool',
79 'creator' => 6,
80 'keywords' => 'video;media',
81 'provider_url' => 'https://tool.example.org/launch',
82 'custom_params' => 'color=blue',
83 'mastery_score' => '0.5',
84 'provider_key' => 'key1',
85 'provider_secret' => 'secret1',
86 'provider_key_customizable' => '0',
87 'xapi_launch_url' => 'https://lrs.example.org/data/xAPI',
88 ]);
89
90 $this->assertSame(7, $tool->getId());
91 $this->assertSame('Video tool', $tool->getTitle());
92 $this->assertTrue($tool->isOwnedBy(6));
93 $this->assertSame(['video', 'media'], $tool->getKeywords());
94 $this->assertSame('https://tool.example.org/launch', $tool->getUrl());
95 $this->assertSame(['color' => 'blue'], $tool->getCustomParamsArray());
96 $this->assertSame(0.5, $tool->getMasteryScore());
97 $this->assertSame('key1', $tool->getKey());
98 $this->assertSame('secret1', $tool->getSecret());
99 $this->assertFalse($tool->isKeyCustomizable());
100 $this->assertSame('https://lrs.example.org/api/statements/aggregate', $tool->getXapiAggregateEndpoint());
101 }
102
103 public function testRedirectionUrisAreSplit(): void
104 {
105 $tool = $this->readTool([
106 'redirection_uris' => 'https://tool.example.org/launch, https://tool.example.org/deep,',
107 ]);
108
109 $this->assertSame(
110 ['https://tool.example.org/launch', 'https://tool.example.org/deep'],
111 $tool->getRedirectionUris()
112 );
113 }
114
115 public static function keyTypeProvider(): array
116 {
117 return [
118 'PEM key' => [ilLTITool::KEY_TYPE_RSA, 'PEM', ''],
119 'key set' => [ilLTITool::KEY_TYPE_JWK, '', 'https://tool.example.org/jwks'],
120 'no key type with a key set' => ['', '', 'https://tool.example.org/jwks'],
121 'no key type with a PEM key' => ['', 'PEM', ''],
122 ];
123 }
124
125 #[DataProvider('keyTypeProvider')]
126 public function testOnlyTheKeyOfItsTypeIsUsed(string $key_type, string $public_key, string $public_keyset): void
127 {
128 $tool = $this->readTool([
129 'key_type' => $key_type,
130 'public_key' => 'PEM',
131 'public_keyset' => $public_keyset === '' ? '' : 'https://tool.example.org/jwks',
132 ]);
133
134 $this->assertSame($public_key, $tool->getPublicKey());
135 $this->assertSame($public_keyset, $tool->getPublicKeysetUrl());
136 }
137
138 public static function deepLinkingProvider(): array
139 {
140 return [
141 'LTI Advantage with a URL' => [ilLTITool::VERSION_ADVANTAGE, true, 'https://tool.example.org/deep', true],
142 'LTI Advantage without a URL' => [ilLTITool::VERSION_ADVANTAGE, true, '', false],
143 'LTI Advantage switched off' => [ilLTITool::VERSION_ADVANTAGE, false, 'https://tool.example.org/deep', false],
144 'LTI 1.1' => [ilLTITool::VERSION_1P1, true, 'https://tool.example.org/deep', false],
145 ];
146 }
147
148 #[DataProvider('deepLinkingProvider')]
149 public function testOffersDeepLinking(string $version, bool $content_item, string $url, bool $expected): void
150 {
151 $tool = $this->readTool([
152 'lti_version' => $version,
153 'content_item' => $content_item ? '1' : '0',
154 'content_item_url' => $url,
155 ]);
156
157 $this->assertSame($expected, $tool->offersDeepLinking());
158 }
159
160 public static function selectableProvider(): array
161 {
162 return [
163 'global tool to create' => [ilLTITool::AVAILABILITY_CREATE, true, 5, true],
164 'own tool to create' => [ilLTITool::AVAILABILITY_CREATE, false, 6, true],
165 'tool of another user' => [ilLTITool::AVAILABILITY_CREATE, false, 5, false],
166 'global tool for existing objects only' => [ilLTITool::AVAILABILITY_EXISTING, true, 5, false],
167 'global tool not available' => [ilLTITool::AVAILABILITY_NONE, true, 6, false],
168 ];
169 }
170
171 #[DataProvider('selectableProvider')]
172 public function testIsSelectableBy(int $availability, bool $global, int $user_id, bool $expected): void
173 {
174 $tool = $this->readTool([
175 'availability' => (string) $availability,
176 'global' => $global ? '1' : '0',
177 'creator' => '6',
178 ]);
179
180 $this->assertSame($expected, $tool->isSelectableBy($user_id));
181 }
182
183 private function readTool(array $row): ilLTITool
184 {
185 $statement = $this->createStub(ilDBStatement::class);
186 $db = $this->createMock(ilDBInterface::class);
187 $db->expects($this->once())
188 ->method('quote')
189 ->with(7, 'integer')
190 ->willReturn('7');
191 $db->expects($this->once())
192 ->method('query')
193 ->with($this->stringContains(ilLTITool::TABLE_NAME))
194 ->willReturn($statement);
195 $db->expects($this->once())
196 ->method('fetchAssoc')
197 ->with($statement)
198 ->willReturn(['id' => '7'] + $row);
199
200 $dic = new Container();
201 $dic['ilDB'] = static fn(): ilDBInterface => $db;
202 $GLOBALS['DIC'] = $dic;
203
204 return new ilLTITool(7);
205 }
206}
$version
Definition: plugin.php:24
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:38
testSplitKeywords(string $keywords, array $expected)
testOffersDeepLinking(string $version, bool $content_item, string $url, bool $expected)
static keywordProvider()
testIsSelectableBy(int $availability, bool $global, int $user_id, bool $expected)
testOnlyTheKeyOfItsTypeIsUsed(string $key_type, string $public_key, string $public_keyset)
static selectableProvider()
static keyTypeProvider()
testParseCustomParams(string $params, array $expected)
static customParamsProvider()
readTool(array $row)
static deepLinkingProvider()
An external tool ILIAS can launch, stored in lti_ext_provider.
const string VERSION_ADVANTAGE
const int AVAILABILITY_NONE
The tool is no longer available, neither for new nor for existing objects.
const string TABLE_NAME
const string VERSION_1P1
const string KEY_TYPE_JWK
const string KEY_TYPE_RSA
How an LTI Advantage tool publishes the key its messages are signed with: a PEM public key,...
const int AVAILABILITY_CREATE
static splitKeywords(string $keywords)
const int AVAILABILITY_EXISTING
static parseCustomParams(string $params)
Interface ilDBInterface.
$GLOBALS["DIC"]
Definition: wac.php:66