comparison core/modules/file/tests/src/Functional/FileFieldDisplayTest.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
1 <?php 1 <?php
2 2
3 namespace Drupal\Tests\file\Functional; 3 namespace Drupal\Tests\file\Functional;
4 4
5 use Drupal\Component\Render\FormattableMarkup;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface; 6 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\file\Entity\File; 7 use Drupal\file\Entity\File;
7 use Drupal\node\Entity\Node; 8 use Drupal\node\Entity\Node;
8 9
9 /** 10 /**
105 $this->drupalPostForm(NULL, $edit, t('Preview')); 106 $this->drupalPostForm(NULL, $edit, t('Preview'));
106 $this->clickLink(t('Back to content editing')); 107 $this->clickLink(t('Back to content editing'));
107 $this->assertRaw($field_name . '[0][display]', 'First file appears as expected.'); 108 $this->assertRaw($field_name . '[0][display]', 'First file appears as expected.');
108 $this->assertRaw($field_name . '[1][display]', 'Second file appears as expected.'); 109 $this->assertRaw($field_name . '[1][display]', 'Second file appears as expected.');
109 $this->assertSession()->responseContains($field_name . '[1][description]', 'Description of second file appears as expected.'); 110 $this->assertSession()->responseContains($field_name . '[1][description]', 'Description of second file appears as expected.');
111
112 // Check that the file fields don't contain duplicate HTML IDs.
113 $this->assertNoDuplicateIds();
110 } 114 }
111 115
112 /** 116 /**
113 * Tests default display of File Field. 117 * Tests default display of File Field.
114 */ 118 */
205 \Drupal::entityTypeManager()->getStorage('node')->resetCache([$nid]); 209 \Drupal::entityTypeManager()->getStorage('node')->resetCache([$nid]);
206 $node = Node::load($nid); 210 $node = Node::load($nid);
207 211
208 // Test default formatter. 212 // Test default formatter.
209 $this->drupalGet('node/' . $nid); 213 $this->drupalGet('node/' . $nid);
210 $this->assertFieldByXPath('//a[@href="' . $node->{$field_name}->entity->url() . '"]', $description); 214 $this->assertFieldByXPath('//a[@href="' . $node->{$field_name}->entity->createFileUrl(FALSE) . '"]', $description);
211 215
212 // Change formatter to "Table of files". 216 // Change formatter to "Table of files".
213 $display = \Drupal::entityTypeManager()->getStorage('entity_view_display')->load('node.' . $type_name . '.default'); 217 $display = \Drupal::entityTypeManager()->getStorage('entity_view_display')->load('node.' . $type_name . '.default');
214 $display->setComponent($field_name, [ 218 $display->setComponent($field_name, [
215 'label' => 'hidden', 219 'label' => 'hidden',
216 'type' => 'file_table', 220 'type' => 'file_table',
217 ])->save(); 221 ])->save();
218 222
219 $this->drupalGet('node/' . $nid); 223 $this->drupalGet('node/' . $nid);
220 $this->assertFieldByXPath('//a[@href="' . $node->{$field_name}->entity->url() . '"]', $description); 224 $this->assertFieldByXPath('//a[@href="' . $node->{$field_name}->entity->createFileUrl(FALSE) . '"]', $description);
225 }
226
227 /**
228 * Asserts that each HTML ID is used for just a single element on the page.
229 *
230 * @param string $message
231 * (optional) A message to display with the assertion.
232 */
233 protected function assertNoDuplicateIds($message = '') {
234 $args = ['@url' => $this->getUrl()];
235
236 if (!$elements = $this->xpath('//*[@id]')) {
237 $this->fail(new FormattableMarkup('The page @url contains no HTML IDs.', $args));
238 return;
239 }
240
241 $message = $message ?: new FormattableMarkup('The page @url does not contain duplicate HTML IDs', $args);
242
243 $seen_ids = [];
244 foreach ($elements as $element) {
245 $id = $element->getAttribute('id');
246 if (isset($seen_ids[$id])) {
247 $this->fail($message);
248 return;
249 }
250 $seen_ids[$id] = TRUE;
251 }
252 $this->assertTrue(TRUE, $message);
221 } 253 }
222 254
223 } 255 }