ILIAS  trunk Revision v12.0_alpha-1338-g8f7e531aa3c
ContextTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\StaticURL\Tests;
22
25use PHPUnit\Framework\MockObject\MockObject;
26
27require_once "Base.php";
28
32class ContextTest extends Base
33{
34 private Container|MockObject $container;
35 private \ilTree|MockObject $tree;
36 private \ilAccessHandler|MockObject $access;
37
38 protected function setUp(): void
39 {
40 $this->container = $this->createMock(Container::class);
41 $this->tree = $this->createMock(\ilTree::class);
42 $this->access = $this->createMock(\ilAccessHandler::class);
43 $this->container->method('repositoryTree')->willReturn($this->tree);
44 $this->container->method('access')->willReturn($this->access);
45 }
46
48 {
49 $this->tree->method('isInTree')->willReturn(false);
50 $context = new Context($this->container);
51
52 $this->assertNull($context->findFirstAccessibleParentRefId(0));
53 $this->assertNull($context->findFirstAccessibleParentRefId(-5));
54 $this->assertNull($context->findFirstAccessibleParentRefId(42));
55 }
56
58 {
59 $this->tree->method('isInTree')->willReturn(true);
60 $this->tree->method('getRootId')->willReturn(1);
61 $this->tree->method('getParentId')->willReturnMap([
62 [100, 50],
63 ]);
64 $this->access->method('checkAccess')->willReturnCallback(
65 static fn(string $perm, string $_, int $ref_id): bool => $perm === 'read' && $ref_id === 50
66 );
67
68 $context = new Context($this->container);
69
70 $this->assertSame(50, $context->findFirstAccessibleParentRefId(100));
71 }
72
74 {
75 $this->tree->method('isInTree')->willReturn(true);
76 $this->tree->method('getRootId')->willReturn(1);
77 $this->tree->method('getParentId')->willReturnMap([
78 [100, 80],
79 [80, 60],
80 [60, 40],
81 ]);
82 $this->access->method('checkAccess')->willReturnCallback(
83 static fn(string $perm, string $_, int $ref_id): bool => $perm === 'read' && $ref_id === 40
84 );
85
86 $context = new Context($this->container);
87
88 $this->assertSame(40, $context->findFirstAccessibleParentRefId(100));
89 }
90
92 {
93 $this->tree->method('isInTree')->willReturn(true);
94 $this->tree->method('getRootId')->willReturn(1);
95 $this->tree->method('getParentId')->willReturnMap([
96 [100, 80],
97 [80, 1],
98 [1, 0],
99 ]);
100 $this->access->method('checkAccess')->willReturn(false);
101
102 $context = new Context($this->container);
103
104 $this->assertNull($context->findFirstAccessibleParentRefId(100));
105 }
106
108 {
109 $this->tree->method('isInTree')->willReturn(true);
110 $this->tree->method('getRootId')->willReturn(1);
111 $this->tree->method('getParentId')->willReturnMap([
112 [100, 1],
113 ]);
114 $this->access->method('checkAccess')->willReturn(false);
115
116 $context = new Context($this->container);
117
118 $this->assertNull($context->findFirstAccessibleParentRefId(100));
119 }
120
122 {
123 $this->tree->method('isInTree')->willReturn(true);
124 $this->tree->method('getRootId')->willReturn(1);
125 $this->tree->method('getParentId')->willReturnMap([
126 [100, 80],
127 [80, 100],
128 ]);
129 $this->access->method('checkAccess')->willReturn(false);
130
131 $context = new Context($this->container);
132
133 $this->assertNull($context->findFirstAccessibleParentRefId(100));
134 }
135
137 {
138 $this->tree->method('isInTree')->willReturn(true);
139 $this->tree->method('getRootId')->willReturn(1);
140 $this->tree->method('getParentId')->willReturnMap([
141 [100, 80],
142 ]);
143 $this->access->method('checkAccess')->willReturnCallback(
144 static fn(string $perm, string $_, int $ref_id): bool => $perm === 'visible' && $ref_id === 80
145 );
146
147 $context = new Context($this->container);
148
149 $this->assertNull($context->findFirstAccessibleParentRefId(100));
150 $this->assertSame(80, $context->findFirstAccessibleParentRefId(100, 'visible'));
151 }
152}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
ilAccessHandler MockObject $access
Definition: ContextTest.php:36
Container MockObject $container
Definition: ContextTest.php:34
$ref_id
Definition: ltiauth.php:66
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Base.php:19