ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
COPageTestBase.php
Go to the documentation of this file.
1<?php
2
19use PHPUnit\Framework\TestCase;
21use PHPUnit\Framework\MockObject\MockObject;
22
26class COPageTestBase extends TestCase
27{
28 protected int $pc_cnt;
29
33 protected function setGlobalVariable(string $name, $value): void
34 {
35 global $DIC;
36
37 $GLOBALS[$name] = $value;
38
39 unset($DIC[$name]);
40 $DIC[$name] = static function (\ILIAS\DI\Container $c) use ($value) {
41 return $value;
42 };
43 }
44
45 protected function setUp(): void
46 {
48 $GLOBALS['DIC'] = $dic;
49
50 if (!defined("ILIAS_LOG_ENABLED")) {
51 define("ILIAS_LOG_ENABLED", false);
52 }
53 if (!defined("IL_INST_ID")) {
54 define("IL_INST_ID", 0);
55 }
56 if (!defined("COPAGE_TEST")) {
57 define("COPAGE_TEST", "1");
58 }
59 if (!defined("ILIAS_LOG_DIR")) {
60 define("ILIAS_LOG_DIR", "/var/log");
61 }
62 if (!defined("ILIAS_LOG_FILE")) {
63 define("ILIAS_LOG_FILE", "/var/log/ilias.log");
64 }
65
66 parent::setUp();
67
68 $def_mock = $this->getMockBuilder(ilObjectDefinition::class)
69 ->disableOriginalConstructor()
70 ->getMock();
71
72 $def_mock
73 ->method('getAllRepositoryTypes')
74 ->willReturn(["crs", "grp", "cat"]);
75 $this->setGlobalVariable(
76 "objDefinition",
77 $def_mock
78 );
79
80 $db_mock = $this->createMock(ilDBInterface::class);
81 $this->setGlobalVariable(
82 "ilDB",
83 $db_mock
84 );
85
86 $this->setGlobalVariable(
87 "ilAccess",
88 $this->createConfiguredMock(
89 ilAccess::class,
90 [
91 "checkAccess" => true
92 ]
93 )
94 );
95
96 $ctrl = $this->getMockBuilder('ilCtrl')->disableOriginalConstructor()->onlyMethods(
97 ['setParameterByClass', 'redirectByClass', 'forwardCommand']
98 )->getMock();
99 $ctrl->method('setParameterByClass');
100 $ctrl->method('redirectByClass');
101 $this->setGlobalVariable('ilCtrl', $ctrl);
102
103 $languageMock = $this->getMockBuilder(ilLanguage::class)
104 ->disableOriginalConstructor()
105 ->getMock();
106 $this->setGlobalVariable(
107 "lng",
108 $languageMock
109 );
110
111 $userMock = $this->getMockBuilder(ilObjUser::class)
112 ->disableOriginalConstructor()
113 ->getMock();
114 $this->setGlobalVariable(
115 "ilUser",
116 $userMock
117 );
118
119 $treeMock = $this->getMockBuilder(ilTree::class)
120 ->disableOriginalConstructor()
121 ->getMock();
122 $this->setGlobalVariable(
123 "tree",
124 $treeMock
125 );
126
127 $refinery_mock = $this->createMock(ILIAS\Refinery\Factory::class);
128 $this->setGlobalVariable(
129 "refinery",
130 $refinery_mock
131 );
132
133 $this->pc_cnt = 1;
134 }
135
139 protected function getIdGeneratorMock(): mixed
140 {
141 $gen = $this->createMock(\ILIAS\COPage\ID\ContentIdGenerator::class);
142 $gen->method("generate")
143 ->willReturnCallback(function () {
144 return str_pad(
145 (string) $this->pc_cnt++,
146 32,
147 "0",
148 STR_PAD_LEFT
149 );
150 });
151 return $gen;
152 }
153
155 {
156 return new ilUnitTestPCDefinition();
157 }
158
159 protected function setPCIdCnt(int $cnt): void
160 {
161 $this->pc_cnt = $cnt;
162 }
163
164 protected function getIDManager(\ilPageObject $page): \ILIAS\COPage\ID\ContentIdManager
165 {
166 return new \ILIAS\COPage\ID\ContentIdManager(
167 $page,
168 $this->getIdGeneratorMock()
169 );
170 }
171
172 protected function insertParagraphAt(
173 \ilPageObject $page,
174 string $hier_id,
175 string $text = ""
176 ): void {
177 $pc = new \ilPCParagraph($page);
178 $pc->create($page, $hier_id);
179 $pc->setLanguage("en");
180 if ($text !== "") {
181 $pc->setText($text);
182 }
183 $page->addHierIDs();
184 }
185
186 protected function tearDown(): void
187 {
188 }
189
190 protected function normalize(string $html): string
191 {
192 return trim(str_replace(["\n", "\r"], "", $html));
193 }
194
195 protected function assertXmlEquals(string $expected_xml_as_string, string $html_xml_string): void
196 {
197 $html = new DOMDocument();
198 $html->formatOutput = true;
199 $html->preserveWhiteSpace = false;
200 $expected = new DOMDocument();
201 $expected->formatOutput = true;
202 $expected->preserveWhiteSpace = false;
203 $html->loadXML($this->normalize($html_xml_string));
204 $expected->loadXML($this->normalize($expected_xml_as_string));
205 $this->assertEquals($expected->saveHTML(), $html->saveHTML());
206 }
207
208
210 {
211 $page = new ilUnitTestPageObject(0);
212 $page->setContentIdManager($this->getIDManager($page));
213 $page->setXMLContent("<PageObject></PageObject>");
214 $page->buildDom();
215 $page->addHierIDs();
216 return $page;
217 }
218
219 // see saveJs in ilPCParagraph
220 protected function legacyHtmlToXml(string $content): string
221 {
222 $content = str_replace("<br>", "<br />", $content);
223 $content = ilPCParagraph::handleAjaxContent($content);
224 $content = ilPCParagraph::_input2xml($content["text"], true, false);
225 $content = ilPCParagraph::handleAjaxContentPost($content);
226 return $content;
227 }
228
232 protected function getMediaObjectMock(): MockObject|ilObjMediaObject
233 {
234 $media_item = new ilMediaItem();
235 $media_item->setWidth("100");
236 $media_item->setHeight("50");
237 $media_object = $this->getMockBuilder(ilObjMediaObject::class)
238 ->disableOriginalConstructor()
239 ->getMock();
240 $media_object->method("getMediaItem")
241 ->willReturnCallback(fn() => $media_item);
242 return $media_object;
243 }
244}
legacyHtmlToXml(string $content)
insertParagraphAt(\ilPageObject $page, string $hier_id, string $text="")
getIDManager(\ilPageObject $page)
setGlobalVariable(string $name, $value)
normalize(string $html)
assertXmlEquals(string $expected_xml_as_string, string $html_xml_string)
setPCIdCnt(int $cnt)
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
Class ilMediaItem Media Item, component of a media object (file or reference)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
create(ilPageObject $a_pg_obj, string $a_hier_id, string $a_pc_id="", bool $from_placeholder=false)
Create paragraph node (incl.
static handleAjaxContentPost(string $text)
Post input2xml handling of ajax content.
static _input2xml(string $a_text, string $a_lang, bool $a_wysiwyg=false, bool $a_handle_lists=true)
Converts user input to xml User input comes as bb code information, e.g.
static handleAjaxContent(string $a_content)
Handle ajax content.
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
setContentIdManager(\ILIAS\COPage\ID\ContentIdManager $content_id_manager)
addHierIDs()
Add hierarchical ID (e.g.
setXMLContent(string $a_xml, string $a_encoding="UTF-8")
set xml content of page, start with <PageObject...>, end with </PageObject>, comply with ILIAS DTD,...
buildDom(bool $a_force=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$c
Definition: deliver.php:25
$dic
Definition: ltiresult.php:33
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $DIC
Definition: shib_login.php:26
$GLOBALS["DIC"]
Definition: wac.php:54
$text
Definition: xapiexit.php:21