Version 1
[yaffs-website] / web / core / modules / field / tests / src / Unit / FieldUninstallValidatorTest.php
1 <?php
2
3 namespace Drupal\Tests\field\Unit;
4
5 use Drupal\simpletest\AssertHelperTrait;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * @coversDefaultClass \Drupal\field\FieldUninstallValidator
10  * @group field
11  */
12 class FieldUninstallValidatorTest extends UnitTestCase {
13
14   use AssertHelperTrait;
15
16   /**
17    * @var \Drupal\field\FieldUninstallValidator|\PHPUnit_Framework_MockObject_MockObject
18    */
19   protected $fieldUninstallValidator;
20
21   /**
22    * The mock field type plugin manager;
23    *
24    * @var \Drupal\Core\Field\FieldTypePluginManagerInterface|\PHPUnit_Framework_MockObject_MockObject
25    */
26   protected $fieldTypePluginManager;
27
28   /**
29    * {@inheritdoc}
30    */
31   protected function setUp() {
32     parent::setUp();
33     $this->fieldUninstallValidator = $this->getMockBuilder('Drupal\field\FieldUninstallValidator')
34       ->disableOriginalConstructor()
35       ->setMethods(['getFieldStoragesByModule', 'getFieldTypeLabel'])
36       ->getMock();
37     $this->fieldUninstallValidator->setStringTranslation($this->getStringTranslationStub());
38   }
39
40   /**
41    * @covers ::validate
42    */
43   public function testValidateNoStorages() {
44     $this->fieldUninstallValidator->expects($this->once())
45       ->method('getFieldStoragesByModule')
46       ->willReturn([]);
47
48     $module = $this->randomMachineName();
49     $expected = [];
50     $reasons = $this->fieldUninstallValidator->validate($module);
51     $this->assertSame($expected, $this->castSafeStrings($reasons));
52   }
53
54   /**
55    * @covers ::validate
56    */
57   public function testValidateDeleted() {
58     $field_storage = $this->getMockBuilder('Drupal\field\Entity\FieldStorageConfig')
59       ->disableOriginalConstructor()
60       ->getMock();
61     $field_storage->expects($this->once())
62       ->method('isDeleted')
63       ->willReturn(TRUE);
64     $this->fieldUninstallValidator->expects($this->once())
65       ->method('getFieldStoragesByModule')
66       ->willReturn([$field_storage]);
67
68     $module = $this->randomMachineName();
69     $expected = ['Fields pending deletion'];
70     $reasons = $this->fieldUninstallValidator->validate($module);
71     $this->assertSame($expected, $this->castSafeStrings($reasons));
72   }
73
74   /**
75    * @covers ::validate
76    */
77   public function testValidateNoDeleted() {
78     $field_storage = $this->getMockBuilder('Drupal\field\Entity\FieldStorageConfig')
79       ->disableOriginalConstructor()
80       ->getMock();
81     $field_storage->expects($this->once())
82       ->method('isDeleted')
83       ->willReturn(FALSE);
84     $field_type = $this->randomMachineName();
85     $field_storage->expects($this->once())
86       ->method('getType')
87       ->willReturn($field_type);
88     $field_name = $this->randomMachineName();
89     $field_storage->expects($this->once())
90       ->method('getLabel')
91       ->willReturn($field_name);
92     $this->fieldUninstallValidator->expects($this->once())
93       ->method('getFieldStoragesByModule')
94       ->willReturn([$field_storage]);
95     $field_type_label = $this->randomMachineName();
96     $this->fieldUninstallValidator->expects($this->once())
97       ->method('getFieldTypeLabel')
98       ->willReturn($field_type_label);
99
100     $module = $this->randomMachineName();
101     $expected = ["The <em class=\"placeholder\">$field_type_label</em> field type is used in the following field: $field_name"];
102     $reasons = $this->fieldUninstallValidator->validate($module);
103     $this->assertSame($expected, $this->castSafeStrings($reasons));
104   }
105
106 }