6beecb88d3d6073cfe28f54e66f045a32f73f9fb
[yaffs-website] / web / core / modules / user / tests / src / Kernel / Field / UserNameFormatterTest.php
1 <?php
2
3 namespace Drupal\Tests\user\Kernel\Field;
4
5 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
6 use Drupal\Core\Entity\FieldableEntityInterface;
7 use Drupal\KernelTests\KernelTestBase;
8 use Drupal\user\Entity\User;
9
10 /**
11  * Tests the user_name formatter.
12  *
13  * @group field
14  */
15 class UserNameFormatterTest extends KernelTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['field', 'user', 'system'];
23
24   /**
25    * @var string
26    */
27   protected $entityType;
28
29   /**
30    * @var string
31    */
32   protected $bundle;
33
34   /**
35    * @var string
36    */
37   protected $fieldName;
38
39   /**
40    * {@inheritdoc}
41    */
42   protected function setUp() {
43     parent::setUp();
44
45     $this->installConfig(['field']);
46     $this->installEntitySchema('user');
47     $this->installSchema('system', ['sequences']);
48
49     $this->entityType = 'user';
50     $this->bundle = $this->entityType;
51     $this->fieldName = 'name';
52   }
53
54   /**
55    * Renders fields of a given entity with a given display.
56    *
57    * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
58    *   The entity object with attached fields to render.
59    * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
60    *   The display to render the fields in.
61    *
62    * @return string
63    *   The rendered entity fields.
64    */
65   protected function renderEntityFields(FieldableEntityInterface $entity, EntityViewDisplayInterface $display) {
66     $content = $display->build($entity);
67     $content = $this->render($content);
68     return $content;
69   }
70
71   /**
72    * Tests the formatter output.
73    */
74   public function testFormatter() {
75     $user = User::create([
76       'name' => 'test name',
77     ]);
78     $user->save();
79
80     $result = $user->{$this->fieldName}->view(['type' => 'user_name']);
81     $this->assertEqual('username', $result[0]['#theme']);
82     $this->assertEqual(spl_object_hash($user), spl_object_hash($result[0]['#account']));
83
84     $result = $user->{$this->fieldName}->view(['type' => 'user_name', 'settings' => ['link_to_entity' => FALSE]]);
85     $this->assertEqual($user->getDisplayName(), $result[0]['#markup']);
86
87     $user = User::getAnonymousUser();
88
89     $result = $user->{$this->fieldName}->view(['type' => 'user_name']);
90     $this->assertEqual('username', $result[0]['#theme']);
91     $this->assertEqual(spl_object_hash($user), spl_object_hash($result[0]['#account']));
92
93     $result = $user->{$this->fieldName}->view(['type' => 'user_name', 'settings' => ['link_to_entity' => FALSE]]);
94     $this->assertEqual($user->getDisplayName(), $result[0]['#markup']);
95     $this->assertEqual($this->config('user.settings')->get('anonymous'), $result[0]['#markup']);
96   }
97
98 }