diff --git a/core/modules/link/src/Tests/LinkFieldTest.php b/core/modules/link/tests/src/Functional/LinkFieldTest.php similarity index 83% rename from core/modules/link/src/Tests/LinkFieldTest.php rename to core/modules/link/tests/src/Functional/LinkFieldTest.php index 0b034d177653da10ef24e64707783c7b4c693613..104a38612678ad16f80d90dad359a379c022ae3c 100644 --- a/core/modules/link/src/Tests/LinkFieldTest.php +++ b/core/modules/link/tests/src/Functional/LinkFieldTest.php @@ -1,6 +1,6 @@ $uri, ]; $this->drupalPostForm('entity_test/add', $edit, t('Save')); - preg_match('|entity_test/manage/(\d+)|', $this->url, $match); + preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match); $id = $match[1]; $this->assertText(t('entity_test @id has been created.', ['@id' => $id])); $this->assertRaw($string); @@ -313,13 +313,13 @@ public function testLinkTitle() { "{$field_name}[0][title]" => '', ]; $this->drupalPostForm(NULL, $edit, t('Save')); - preg_match('|entity_test/manage/(\d+)|', $this->url, $match); + preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match); $id = $match[1]; $this->assertText(t('entity_test @id has been created.', ['@id' => $id])); - $this->renderTestEntity($id); - $expected_link = \Drupal::l($value, Url::fromUri($value)); - $this->assertRaw($expected_link); + $output = $this->renderTestEntity($id); + $expected_link = (string) \Drupal::l($value, Url::fromUri($value)); + $this->assertContains($expected_link, $output); // Verify that a link with text is rendered using the link text. $title = $this->randomMachineName(); @@ -329,9 +329,9 @@ public function testLinkTitle() { $this->drupalPostForm("entity_test/manage/$id/edit", $edit, t('Save')); $this->assertText(t('entity_test @id has been updated.', ['@id' => $id])); - $this->renderTestEntity($id); - $expected_link = \Drupal::l($title, Url::fromUri($value)); - $this->assertRaw($expected_link); + $output = $this->renderTestEntity($id); + $expected_link = (string) \Drupal::l($title, Url::fromUri($value)); + $this->assertContains($expected_link, $output); } /** @@ -395,7 +395,7 @@ public function testLinkFormatter() { // Assert label is shown. $this->assertText('Read more about this entity'); $this->drupalPostForm(NULL, $edit, t('Save')); - preg_match('|entity_test/manage/(\d+)|', $this->url, $match); + preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match); $id = $match[1]; $this->assertText(t('entity_test @id has been created.', ['@id' => $id])); @@ -428,56 +428,56 @@ public function testLinkFormatter() { ->setComponent($field_name, $display_options) ->save(); - $this->renderTestEntity($id); + $output = $this->renderTestEntity($id); switch ($setting) { case 'trim_length': $url = $url1; $title = isset($new_value) ? Unicode::truncate($title1, $new_value, FALSE, TRUE) : $title1; - $this->assertRaw('' . Html::escape($title) . ''); + $this->assertContains('' . Html::escape($title) . '', $output); $url = $url2; $title = isset($new_value) ? Unicode::truncate($title2, $new_value, FALSE, TRUE) : $title2; - $this->assertRaw('' . Html::escape($title) . ''); + $this->assertContains('' . Html::escape($title) . '', $output); $url = $url3; $title = isset($new_value) ? Unicode::truncate($title3, $new_value, FALSE, TRUE) : $title3; - $this->assertRaw('' . Html::escape($title) . ''); + $this->assertContains('' . Html::escape($title) . '', $output); break; case 'rel': $rel = isset($new_value) ? ' rel="' . $new_value . '"' : ''; - $this->assertRaw('' . Html::escape($title1) . ''); - $this->assertRaw('' . Html::escape($title2) . ''); - $this->assertRaw('' . Html::escape($title3) . ''); + $this->assertContains('' . Html::escape($title1) . '', $output); + $this->assertContains('' . Html::escape($title2) . '', $output); + $this->assertContains('' . Html::escape($title3) . '', $output); break; case 'target': $target = isset($new_value) ? ' target="' . $new_value . '"' : ''; - $this->assertRaw('' . Html::escape($title1) . ''); - $this->assertRaw('' . Html::escape($title2) . ''); - $this->assertRaw('' . Html::escape($title3) . ''); + $this->assertContains('' . Html::escape($title1) . '', $output); + $this->assertContains('' . Html::escape($title2) . '', $output); + $this->assertContains('' . Html::escape($title3) . '', $output); break; case 'url_only': // In this case, $new_value is an array. if (!$new_value['url_only']) { - $this->assertRaw('' . Html::escape($title1) . ''); - $this->assertRaw('' . Html::escape($title2) . ''); - $this->assertRaw('' . Html::escape($title3) . ''); + $this->assertContains('' . Html::escape($title1) . '', $output); + $this->assertContains('' . Html::escape($title2) . '', $output); + $this->assertContains('' . Html::escape($title3) . '', $output); } else { if (empty($new_value['url_plain'])) { - $this->assertRaw('' . Html::escape($url1) . ''); - $this->assertRaw('' . Html::escape($url2) . ''); - $this->assertRaw('' . Html::escape($url3) . ''); + $this->assertContains('' . Html::escape($url1) . '', $output); + $this->assertContains('' . Html::escape($url2) . '', $output); + $this->assertContains('' . Html::escape($url3) . '', $output); } else { - $this->assertNoRaw('' . Html::escape($url1) . ''); - $this->assertNoRaw('' . Html::escape($url2) . ''); - $this->assertNoRaw('' . Html::escape($url3) . ''); - $this->assertEscaped($url1); - $this->assertEscaped($url2); - $this->assertEscaped($url3); + $this->assertNotContains('' . Html::escape($url1) . '', $output); + $this->assertNotContains('' . Html::escape($url2) . '', $output); + $this->assertNotContains('' . Html::escape($url3) . '', $output); + $this->assertContains(Html::escape($url1), $output); + $this->assertContains(Html::escape($url2), $output); + $this->assertContains(Html::escape($url3), $output); } } break; @@ -544,7 +544,7 @@ public function testLinkSeparateFormatter() { "{$field_name}[2][title]" => $title3, ]; $this->drupalPostForm(NULL, $edit, t('Save')); - preg_match('|entity_test/manage/(\d+)|', $this->url, $match); + preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match); $id = $match[1]; $this->assertText(t('entity_test @id has been created.', ['@id' => $id])); @@ -562,7 +562,7 @@ public function testLinkSeparateFormatter() { ->setComponent($field_name, $display_options) ->save(); - $this->renderTestEntity($id); + $output = $this->renderTestEntity($id); switch ($setting) { case 'trim_length': $url = $url1; @@ -570,7 +570,7 @@ public function testLinkSeparateFormatter() { $expected = ''; - $this->assertRaw($expected); + $this->assertContains($expected, $output); $url = $url2; $url_title = isset($new_value) ? Unicode::truncate($url, $new_value, FALSE, TRUE) : $url; @@ -579,7 +579,7 @@ public function testLinkSeparateFormatter() { $expected .= ''; $expected .= ''; $expected .= ''; - $this->assertRaw($expected); + $this->assertContains($expected, $output); $url = $url3; $url_title = isset($new_value) ? Unicode::truncate($url, $new_value, FALSE, TRUE) : $url; @@ -588,21 +588,21 @@ public function testLinkSeparateFormatter() { $expected .= ''; $expected .= ''; $expected .= ''; - $this->assertRaw($expected); + $this->assertContains($expected, $output); break; case 'rel': $rel = isset($new_value) ? ' rel="' . $new_value . '"' : ''; - $this->assertRaw(''); - $this->assertRaw(''); - $this->assertRaw(''); + $this->assertContains('', $output); + $this->assertContains('', $output); + $this->assertContains('', $output); break; case 'target': $target = isset($new_value) ? ' target="' . $new_value . '"' : ''; - $this->assertRaw(''); - $this->assertRaw(''); - $this->assertRaw(''); + $this->assertContains('', $output); + $this->assertContains('', $output); + $this->assertContains('', $output); break; } } @@ -653,7 +653,7 @@ public function testLinkTypeOnLinkWidget() { } /** - * Renders a test_entity and sets the output in the internal browser. + * Renders a test_entity and returns the output. * * @param int $id * The test_entity ID to render. @@ -662,6 +662,9 @@ public function testLinkTypeOnLinkWidget() { * @param bool $reset * (optional) Whether to reset the entity_test storage cache. Defaults to * TRUE to simplify testing. + * + * @return string + * The rendered HTML output. */ protected function renderTestEntity($id, $view_mode = 'full', $reset = TRUE) { if ($reset) { @@ -671,8 +674,9 @@ protected function renderTestEntity($id, $view_mode = 'full', $reset = TRUE) { $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), $view_mode); $content = $display->build($entity); $output = \Drupal::service('renderer')->renderRoot($content); - $this->setRawContent($output); + $output = (string) $output; $this->verbose($output); + return $output; } }