Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / datetime / tests / src / Functional / DateTimeTimeAgoFormatterTest.php
1 <?php
2
3 namespace Drupal\Tests\datetime\Functional;
4
5 use Drupal\Core\Entity\Entity\EntityFormDisplay;
6 use Drupal\Core\Entity\Entity\EntityViewDisplay;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\Tests\BrowserTestBase;
10
11 /**
12  * Tests the functionality of DateTimeTimeAgoFormatter field formatter.
13  *
14  * @group field
15  */
16 class DateTimeTimeAgoFormatterTest extends BrowserTestBase {
17
18   /**
19    * An array of display options to pass to entity_get_display().
20    *
21    * @var array
22    */
23   protected $displayOptions;
24
25   /**
26    * A field storage to use in this test class.
27    *
28    * @var \Drupal\field\Entity\FieldStorageConfig
29    */
30   protected $fieldStorage;
31
32   /**
33    * The field used in this test class.
34    *
35    * @var \Drupal\field\Entity\FieldConfig
36    */
37   protected $field;
38
39   /**
40    * {@inheritdoc}
41    */
42   public static $modules = ['datetime', 'entity_test', 'field_ui'];
43
44   /**
45    * {@inheritdoc}
46    */
47   protected function setUp() {
48     parent::setUp();
49
50     $web_user = $this->drupalCreateUser([
51       'access administration pages',
52       'view test entity',
53       'administer entity_test content',
54       'administer entity_test fields',
55       'administer entity_test display',
56       'administer entity_test form display',
57       'view the administration theme',
58     ]);
59     $this->drupalLogin($web_user);
60
61     $field_name = 'field_datetime';
62     $type = 'datetime';
63     $widget_type = 'datetime_default';
64     $formatter_type = 'datetime_time_ago';
65
66     $this->fieldStorage = FieldStorageConfig::create([
67       'field_name' => $field_name,
68       'entity_type' => 'entity_test',
69       'type' => $type,
70     ]);
71     $this->fieldStorage->save();
72     $this->field = FieldConfig::create([
73       'field_storage' => $this->fieldStorage,
74       'bundle' => 'entity_test',
75       'required' => TRUE,
76     ]);
77     $this->field->save();
78
79     EntityFormDisplay::load('entity_test.entity_test.default')
80       ->setComponent($field_name, ['type' => $widget_type])
81       ->save();
82
83     $this->displayOptions = [
84       'type' => $formatter_type,
85       'label' => 'hidden',
86     ];
87
88     EntityViewDisplay::create([
89       'targetEntityType' => $this->field->getTargetEntityTypeId(),
90       'bundle' => $this->field->getTargetBundle(),
91       'mode' => 'full',
92       'status' => TRUE,
93     ])->setComponent($field_name, $this->displayOptions)
94       ->save();
95   }
96
97   /**
98    * Tests the formatter settings.
99    */
100   public function testSettings() {
101     $this->drupalGet('entity_test/structure/entity_test/display');
102
103     $edit = [
104       'fields[field_datetime][region]' => 'content',
105       'fields[field_datetime][type]' => 'datetime_time_ago',
106     ];
107     $this->drupalPostForm(NULL, $edit, t('Save'));
108
109     $this->drupalPostForm(NULL, [], 'field_datetime_settings_edit');
110     $edit = [
111       'fields[field_datetime][settings_edit_form][settings][future_format]' => 'ends in @interval',
112       'fields[field_datetime][settings_edit_form][settings][past_format]' => 'started @interval ago',
113       'fields[field_datetime][settings_edit_form][settings][granularity]' => 3,
114     ];
115     $this->drupalPostForm(NULL, $edit, 'Update');
116     $this->drupalPostForm(NULL, [], 'Save');
117
118     $this->assertSession()->pageTextContains('ends in 1 year 1 month 1 week');
119     $this->assertSession()->pageTextContains('started 1 year 1 month 1 week ago');
120   }
121
122 }