ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
COPageTestBase.php
Go to the documentation of this file.
1<?php
2
19use PHPUnit\Framework\TestCase;
20
24class COPageTestBase extends TestCase
25{
26 protected int $pc_cnt;
27
31 protected function setGlobalVariable(string $name, $value): void
32 {
33 global $DIC;
34
35 $GLOBALS[$name] = $value;
36
37 unset($DIC[$name]);
38 $DIC[$name] = static function (\ILIAS\DI\Container $c) use ($value) {
39 return $value;
40 };
41 }
42
43 protected function setUp(): void
44 {
46 $GLOBALS['DIC'] = $dic;
47
48 if (!defined("ILIAS_LOG_ENABLED")) {
49 define("ILIAS_LOG_ENABLED", false);
50 }
51 if (!defined("IL_INST_ID")) {
52 define("IL_INST_ID", 0);
53 }
54 if (!defined("COPAGE_TEST")) {
55 define("COPAGE_TEST", "1");
56 }
57 if (!defined("ILIAS_LOG_DIR")) {
58 define("ILIAS_LOG_DIR", "/var/log");
59 }
60 if (!defined("ILIAS_LOG_FILE")) {
61 define("ILIAS_LOG_FILE", "/var/log/ilias.log");
62 }
63
64 parent::setUp();
65
66 $def_mock = $this->getMockBuilder(ilObjectDefinition::class)
67 ->disableOriginalConstructor()
68 ->getMock();
69
70 $def_mock
71 ->method('getAllRepositoryTypes')
72 ->willReturn(["crs", "grp", "cat"]);
73 $this->setGlobalVariable(
74 "objDefinition",
75 $def_mock
76 );
77
78 $db_mock = $this->createMock(ilDBInterface::class);
79 $this->setGlobalVariable(
80 "ilDB",
81 $db_mock
82 );
83
84 $this->setGlobalVariable(
85 "ilAccess",
86 $this->createConfiguredMock(
87 ilAccess::class,
88 [
89 "checkAccess" => true
90 ]
91 )
92 );
93
94 $ctrl = $this->getMockBuilder('ilCtrl')->disableOriginalConstructor()->onlyMethods(
95 ['setParameterByClass', 'redirectByClass', 'forwardCommand']
96 )->getMock();
97 $ctrl->method('setParameterByClass');
98 $ctrl->method('redirectByClass');
99 $this->setGlobalVariable('ilCtrl', $ctrl);
100
101 $languageMock = $this->getMockBuilder(ilLanguage::class)
102 ->disableOriginalConstructor()
103 ->getMock();
104 $this->setGlobalVariable(
105 "lng",
106 $languageMock
107 );
108
109 $userMock = $this->getMockBuilder(ilObjUser::class)
110 ->disableOriginalConstructor()
111 ->getMock();
112 $this->setGlobalVariable(
113 "ilUser",
114 $userMock
115 );
116
117 $treeMock = $this->getMockBuilder(ilTree::class)
118 ->disableOriginalConstructor()
119 ->getMock();
120 $this->setGlobalVariable(
121 "tree",
122 $treeMock
123 );
124
125 $refinery_mock = $this->createMock(ILIAS\Refinery\Factory::class);
126 $this->setGlobalVariable(
127 "refinery",
128 $refinery_mock
129 );
130
131 $this->pc_cnt = 1;
132 }
133
137 protected function getIdGeneratorMock()
138 {
139 $gen = $this->createMock(\ILIAS\COPage\ID\ContentIdGenerator::class);
140 $gen->method("generate")
141 ->willReturnCallback(function () {
142 return str_pad(
143 (string) $this->pc_cnt++,
144 32,
145 "0",
146 STR_PAD_LEFT
147 );
148 });
149 return $gen;
150 }
151
153 {
154 return new ilUnitTestPCDefinition();
155 }
156
157 protected function setPCIdCnt(int $cnt): void
158 {
159 $this->pc_cnt = $cnt;
160 }
161
162 protected function getIDManager(\ilPageObject $page): \ILIAS\COPage\ID\ContentIdManager
163 {
164 return new \ILIAS\COPage\ID\ContentIdManager(
165 $page,
166 $this->getIdGeneratorMock()
167 );
168 }
169
170 protected function insertParagraphAt(
171 \ilPageObject $page,
172 string $hier_id,
173 string $text = ""
174 ) {
175 $pc = new \ilPCParagraph($page);
176 $pc->create($page, $hier_id);
177 $pc->setLanguage("en");
178 if ($text !== "") {
179 $pc->setText($text);
180 }
181 $page->addHierIDs();
182 }
183
184 protected function tearDown(): void
185 {
186 }
187
188 protected function normalize(string $html): string
189 {
190 return trim(str_replace(["\n", "\r"], "", $html));
191 }
192
193 protected function assertXmlEquals(string $expected_xml_as_string, string $html_xml_string): void
194 {
195 $html = new DOMDocument();
196 $html->formatOutput = true;
197 $html->preserveWhiteSpace = false;
198 $expected = new DOMDocument();
199 $expected->formatOutput = true;
200 $expected->preserveWhiteSpace = false;
201 $html->loadXML($this->normalize($html_xml_string));
202 $expected->loadXML($this->normalize($expected_xml_as_string));
203 $this->assertEquals($expected->saveHTML(), $html->saveHTML());
204 }
205
206
208 {
209 $page = new ilUnitTestPageObject(0);
210 $page->setContentIdManager($this->getIDManager($page));
211 $page->setXMLContent("<PageObject></PageObject>");
212 $page->buildDom();
213 $page->addHierIDs();
214 return $page;
215 }
216
217 // see saveJs in ilPCParagraph
218 protected function legacyHtmlToXml(string $content): string
219 {
220 $content = str_replace("<br>", "<br />", $content);
221 $content = ilPCParagraph::handleAjaxContent($content);
222 $content = ilPCParagraph::_input2xml($content["text"], true, false);
223 $content = ilPCParagraph::handleAjaxContentPost($content);
224 return $content;
225 }
226
230 protected function getMediaObjectMock()
231 {
232 $media_item = new ilMediaItem();
233 $media_item->setWidth("100");
234 $media_item->setHeight("50");
235 $media_object = $this->getMockBuilder(ilObjMediaObject::class)
236 ->disableOriginalConstructor()
237 ->getMock();
238 $media_object->method("getMediaItem")
239 ->willReturnCallback(fn() => $media_item);
240 return $media_object;
241 }
242}
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)
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)
addHierIDs()
Add hierarchical ID (e.g.
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