Version 1
[yaffs-website] / web / core / modules / field / tests / src / Kernel / String / UuidFormatterTest.php
diff --git a/web/core/modules/field/tests/src/Kernel/String/UuidFormatterTest.php b/web/core/modules/field/tests/src/Kernel/String/UuidFormatterTest.php
new file mode 100644 (file)
index 0000000..0eeb778
--- /dev/null
@@ -0,0 +1,58 @@
+<?php
+
+namespace Drupal\Tests\field\Kernel\String;
+
+use Drupal\entity_test\Entity\EntityTest;
+use Drupal\KernelTests\KernelTestBase;
+
+/**
+ * Tests the output of a UUID field.
+ *
+ * @group field
+ */
+class UuidFormatterTest extends KernelTestBase {
+
+
+  /**
+   * Modules to enable.
+   *
+   * @var array
+   */
+  public static $modules = ['field', 'entity_test', 'system', 'user'];
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function setUp() {
+    parent::setUp();
+
+    $this->installConfig(['system', 'field']);
+    \Drupal::service('router.builder')->rebuild();
+    $this->installEntitySchema('entity_test');
+  }
+
+  /**
+   * Tests string formatter output.
+   */
+  public function testUuidStringFormatter() {
+    $entity = EntityTest::create([]);
+    $entity->save();
+
+    $uuid_field = $entity->get('uuid');
+
+    // Verify default render.
+    $render_array = $uuid_field->view([]);
+    $this->assertIdentical($render_array[0]['#context']['value'], $entity->uuid(), 'The rendered UUID matches the entity UUID.');
+    $this->assertTrue(strpos($this->render($render_array), $entity->uuid()), 'The rendered UUID found.');
+
+    // Verify customized render.
+    $render_array = $uuid_field->view(['settings' => ['link_to_entity' => TRUE]]);
+    $this->assertIdentical($render_array[0]['#type'], 'link');
+    $this->assertIdentical($render_array[0]['#title']['#context']['value'], $entity->uuid());
+    $this->assertIdentical($render_array[0]['#url']->toString(), $entity->url());
+    $rendered = $this->render($render_array);
+    $this->assertTrue(strpos($rendered, $entity->uuid()), 'The rendered UUID found.');
+    $this->assertTrue(strpos($rendered, $entity->url()), 'The rendered entity URL found.');
+  }
+
+}