ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
BadgeParentTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Badge\test;
22 
28 use ilBadge;
29 use ILIAS\UI\Factory as UI;
31 use ilLanguage;
32 use ilAccess;
37 
38 class BadgeParentTest extends TestCase
39 {
40  public function testConstruct(): void
41  {
42  $container = $this->getMockBuilder(Container::class)->disableOriginalConstructor()->getMock();
43  $parent = new BadgeParent($container);
44  $this->assertInstanceOf(BadgeParent::class, $parent);
45  }
46 
47  public function testShowWithParentReadRight(): void
48  {
49  $rendered = 'Rendered components';
50 
51  $access = $this->getMockBuilder(ilAccess::class)->disableOriginalConstructor()->getMock();
52  $badge = $this->getMockBuilder(ilBadge::class)->disableOriginalConstructor()->getMock();
53  $container = $this->getMockBuilder(Container::class)->disableOriginalConstructor()->getMock();
54  $descriptive = $this->getMockBuilder(Descriptive::class)->getMock();
55  $factory = $this->getMockBuilder(UI::class)->disableOriginalConstructor()->getMock();
56  $language = $this->getMockBuilder(ilLanguage::class)->disableOriginalConstructor()->getMock();
57  $legacy = $this->getMockBuilder(Legacy::class)->disableOriginalConstructor()->getMock();
58  $link = $this->getMockBuilder(Link::class)->disableOriginalConstructor()->getMock();
59  $listing = $this->getMockBuilder(Listing::class)->disableOriginalConstructor()->getMock();
60  $renderer = $this->getMockBuilder(Renderer::class)->disableOriginalConstructor()->getMock();
61  $standard_link = $this->getMockBuilder(StandardLink::class)->disableOriginalConstructor()->getMock();
62  $ui = $this->getMockBuilder(UIServices::class)->disableOriginalConstructor()->getMock();
63 
64  $language->method('txt')->willReturnCallback(static fn(string $name): string => $name . ' translated');
65 
66  $listing->expects(self::once())->method('descriptive')->with([
67  'object translated' => $legacy,
68  ])->willReturn($descriptive);
69 
70  $link->method('standard')->with('Lorem', 'Some URL.')->willReturn($standard_link);
71 
72  $factory->method('listing')->willReturn($listing);
73  $factory->method('link')->willReturn($link);
74  $factory->expects(self::once())->method('legacy')->with($rendered)->willReturn($legacy);
75 
76  $renderer->expects(self::once())->method('render')->willReturn($rendered);
77 
78  $ui->method('factory')->willReturn($factory);
79  $ui->method('renderer')->willReturn($renderer);
80 
81  $access->expects(self::once())->method('checkAccess')->with('read', '', 89)->willReturn(true);
82 
83  $container->method('ui')->willReturn($ui);
84  $container->method('language')->willReturn($language);
85  $container->method('access')->willReturn($access);
86 
87  $parent_id = 6879;
88  $badge->method('getParentId')->willReturn($parent_id);
89  $badge->method('getParentMeta')->willReturn([
90  'id' => $parent_id,
91  'type' => 'crs',
92  'title' => 'Lorem',
93  ]);
94 
95  $icon = function (int $id, string $size, string $type): string {
96  $this->assertSame([6879, 'big', 'crs'], [$id, $size, $type]);
97  return 'Some image path.';
98  };
99 
100  $references_of = static fn(): array => [89];
101 
102  $link_to = function (int $ref_id): string {
103  $this->assertSame(89, $ref_id);
104  return 'Some URL.';
105  };
106 
107  $parent = new BadgeParent($container, $icon, $references_of, $link_to);
108  $this->assertSame($descriptive, $parent->asComponent($badge));
109  }
110 
111  public function testShowWithoutParentReadRight(): void
112  {
113  $rendered = 'Rendered components';
114 
115  $access = $this->getMockBuilder(ilAccess::class)->disableOriginalConstructor()->getMock();
116  $badge = $this->getMockBuilder(ilBadge::class)->disableOriginalConstructor()->getMock();
117  $container = $this->getMockBuilder(Container::class)->disableOriginalConstructor()->getMock();
118  $descriptive = $this->getMockBuilder(Descriptive::class)->getMock();
119  $factory = $this->getMockBuilder(UI::class)->disableOriginalConstructor()->getMock();
120  $language = $this->getMockBuilder(ilLanguage::class)->disableOriginalConstructor()->getMock();
121  $legacy = $this->getMockBuilder(Legacy::class)->disableOriginalConstructor()->getMock();
122  $listing = $this->getMockBuilder(Listing::class)->disableOriginalConstructor()->getMock();
123  $parent_link = $this->getMockBuilder(Legacy::class)->disableOriginalConstructor()->getMock();
124  $renderer = $this->getMockBuilder(Renderer::class)->disableOriginalConstructor()->getMock();
125  $ui = $this->getMockBuilder(UIServices::class)->disableOriginalConstructor()->getMock();
126 
127  $language->method('txt')->willReturnCallback(static fn(string $name): string => $name . ' translated');
128 
129  $listing->expects(self::once())->method('descriptive')->with([
130  'object translated' => $legacy,
131  ])->willReturn($descriptive);
132 
133  $factory->method('listing')->willReturn($listing);
134  $factory->method('legacy')->withConsecutive(['Lorem'], [$rendered])->willReturnOnConsecutiveCalls($parent_link, $legacy);
135 
136  $renderer->expects(self::once())->method('render')->willReturn($rendered);
137 
138  $ui->method('factory')->willReturn($factory);
139  $ui->method('renderer')->willReturn($renderer);
140 
141  $access->expects(self::once())->method('checkAccess')->with('read', '', 89)->willReturn(false);
142 
143  $container->method('ui')->willReturn($ui);
144  $container->method('language')->willReturn($language);
145  $container->method('access')->willReturn($access);
146 
147  $parent_id = 6879;
148  $badge->method('getParentId')->willReturn($parent_id);
149  $badge->method('getParentMeta')->willReturn([
150  'id' => $parent_id,
151  'type' => 'crs',
152  'title' => 'Lorem',
153  ]);
154 
155  $icon = function (int $id, string $size, string $type): string {
156  $this->assertSame([6879, 'big', 'crs'], [$id, $size, $type]);
157  return 'Some image path.';
158  };
159 
160  $references_of = static fn(): array => [89];
161 
162  $link_to = function (int $ref_id): string {
163  $this->assertSame(89, $ref_id);
164  return 'Some URL.';
165  };
166 
167  $parent = new BadgeParent($container, $icon, $references_of, $link_to);
168  $this->assertSame($descriptive, $parent->asComponent($badge));
169  }
170 }
$container
Definition: wac.php:14
$ref_id
Definition: ltiauth.php:67
This is how a factory for listings looks like.
Definition: Factory.php:26
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23