ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
InterruptiveTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 require_once(__DIR__ . '/ModalBase.php');
22 
23 use ILIAS\UI\Component as C;
25 
30 {
31  public function testGetTitle(): void
32  {
33  $interruptive = $this->getModalFactory()->interruptive('myTitle', 'myMessage', 'myFormAction');
34  $this->assertEquals('myTitle', $interruptive->getTitle());
35  }
36 
37  public function testGetMessage(): void
38  {
39  $interruptive = $this->getModalFactory()->interruptive('myTitle', 'myMessage', 'myFormAction');
40  $this->assertEquals('myMessage', $interruptive->getMessage());
41  }
42 
43  public function testGetFormAction(): void
44  {
45  $interruptive = $this->getModalFactory()->interruptive('myTitle', 'myMessage', 'myFormAction');
46  $this->assertEquals('myFormAction', $interruptive->getFormAction());
47  }
48 
49  public function testGetAffectedItems(): void
50  {
51  $interruptive = $this->getModalFactory()->interruptive('myTitle', 'myMessage', 'myFormAction');
52  $items = [$this->getInterruptiveItem(), $this->getInterruptiveItem()];
53  $interruptive = $interruptive->withAffectedItems($items);
54  $this->assertEquals($items, $interruptive->getAffectedItems());
55  }
56 
57  public function testWithFormAction(): void
58  {
59  $interruptive = $this->getModalFactory()->interruptive('myTitle', 'myMessage', 'myFormAction');
60  $interruptive2 = $interruptive->withFormAction('myFormAction2');
61  $this->assertEquals('myFormAction', $interruptive->getFormAction());
62  $this->assertEquals('myFormAction2', $interruptive2->getFormAction());
63  }
64 
65  public function testWithAffectedItems(): void
66  {
67  $interruptive = $this->getModalFactory()->interruptive('myTitle', 'myMessage', 'myFormAction');
68  $items = [$this->getInterruptiveItem(), $this->getInterruptiveItem()];
69  $interruptive2 = $interruptive->withAffectedItems($items);
70  $this->assertEquals(0, count($interruptive->getAffectedItems()));
71  $this->assertEquals($items, $interruptive2->getAffectedItems());
72  }
73 
74  public function testSimpleRendering(): void
75  {
76  $interruptive = $this->getModalFactory()->interruptive('Title', 'Message', 'myAction.php');
77  $expected = $this->brutallyTrimHTML($this->getExpectedHTML());
78  $actual = $this->brutallyTrimHTML($this->getDefaultRenderer()->render($interruptive));
79  $this->assertEquals($expected, $actual);
80  }
81 
82  public function testRenderingWithItems(): void
83  {
84  $interruptive = $this->getModalFactory()->interruptive('Title', 'Message', 'myAction.php');
85  $items = [
86  $this->getKeyValueInterruptiveItem('keyvalue1'),
87  $this->getStandardInterruptiveItem('standard1'),
88  $this->getKeyValueInterruptiveItem('keyvalue2'),
89  $this->getKeyValueInterruptiveItem('keyvalue3'),
90  $this->getStandardInterruptiveItem('standard2')
91  ];
92  $interruptive = $interruptive->withAffectedItems($items);
93  $expected = $this->normalizeHTML($this->getExpectedHTML(true));
94  $actual = $this->normalizeHTML($this->getDefaultRenderer(null, $items)->render($interruptive));
95  $this->assertEquals($expected, $actual);
96  }
97 
99  {
100  return new InterruptiveItemMock();
101  }
102 
103  protected function getStandardInterruptiveItem(string $canonical_name): StandardItemMock
104  {
105  return new StandardItemMock($canonical_name);
106  }
107 
108  protected function getKeyValueInterruptiveItem(string $canonical_name): KeyValueItemMock
109  {
110  return new KeyValueItemMock($canonical_name);
111  }
112 
113  protected function getExpectedHTML(bool $with_items = false): string
114  {
115  $expected_start = <<<EOT
116 <div class="modal fade c-modal--interruptive" tabindex="-1" role="dialog" id="id_1">
117  <div class="modal-dialog" role="document">
118  <form action="myAction.php" method="POST">
119  <div class="modal-content">
120  <div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-label="cancel">
121  <span aria-hidden="true">&times;</span></button><h1 class="modal-title">Title</h1>
122  </div>
123  <div class="modal-body">
124  <div class="alert alert-warning c-modal--interruptive__message" role="alert">Message</div>
125 EOT;
126  $expected_items = <<<EOT
127  <div class="c-modal--interruptive__items">
128  <table>
129  standard1
130  standard2
131  </table>
132  </div>
133  <div class="c-modal--interruptive__items">
134  <dl>
135  keyvalue1
136  keyvalue2
137  keyvalue3
138  </dl>
139  </div>
140 EOT;
141  $expected_end = <<<EOT
142  </div>
143  <div class="modal-footer">
144  <input type="submit" class="btn btn-primary" value="delete"/>
145  <button class="btn btn-default" data-dismiss="modal">cancel</button>
146  </div>
147  </div>
148  </form>
149  </div>
150 </div>
151 EOT;
152  if ($with_items) {
153  return $expected_start . $expected_items . $expected_end;
154  }
155  return $expected_start . $expected_end;
156  }
157 
159  public function testLabels(): void
160  {
161  $action_label = 'actionlabel';
162  $cancel_label = 'cancellabel';
163  $interruptive = $this->getModalFactory()->interruptive('Title', 'Message', 'someaction')
164  ->withActionButtonLabel($action_label)
165  ->withCancelButtonLabel($cancel_label);
167  $this->assertEquals(
168  $action_label,
169  $interruptive->getActionButtonLabel()
170  );
171  $this->assertEquals(
172  $cancel_label,
173  $interruptive->getCancelButtonLabel()
174  );
175  }
176 }
177 
178 class InterruptiveItemMock implements C\Modal\InterruptiveItem\InterruptiveItem
179 {
180  protected string $canonical_name;
181 
182  public function __construct(string $canonical_name = '')
183  {
184  $this->canonical_name = $canonical_name;
185  }
186 
187  public function getId(): string
188  {
189  return '1';
190  }
191 
192  public function getCanonicalName(): string
193  {
194  return $this->canonical_name ?: 'InterruptiveItem';
195  }
196 }
197 
198 class StandardItemMock extends InterruptiveItemMock implements C\Modal\InterruptiveItem\Standard
199 {
200  public function getTitle(): string
201  {
202  return 'title';
203  }
204 
205  public function getDescription(): string
206  {
207  return 'description';
208  }
209 
210  public function getIcon(): C\Image\Image
211  {
212  return new I\Component\Image\Image(C\Image\Image::STANDARD, '', '');
213  }
214 }
215 
216 class KeyValueItemMock extends InterruptiveItemMock implements C\Modal\InterruptiveItem\KeyValue
217 {
218  public function getKey(): string
219  {
220  return 'key';
221  }
222 
223  public function getValue(): string
224  {
225  return 'value';
226  }
227 }
getExpectedHTML(bool $with_items=false)
getDefaultRenderer(JavaScriptBinding $js_binding=null, array $with_stub_renderings=[])
Definition: Base.php:377
button(string $caption, string $cmd)
Tests on implementation for the interruptive modal.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getValue()
Get the value that is displayed in the input client side.
Definition: Group.php:46
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Title class.
Definition: Title.php:26
brutallyTrimHTML(string $html)
A more radical version of normalizeHTML.
Definition: Base.php:475
__construct(VocabulariesInterface $vocabularies)
Base class for modal tests.
Definition: ModalBase.php:34
getKey()
Get the consumer key.
Definition: System.php:233
normalizeHTML(string $html)
Definition: ModalBase.php:76
getModalFactory()
Definition: ModalBase.php:51
getKeyValueInterruptiveItem(string $canonical_name)
form( $class_path, string $cmd, string $submit_caption="")
getStandardInterruptiveItem(string $canonical_name)