Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / file / tests / src / Kernel / ComputedFileUrlTest.php
1 <?php
2
3 namespace Drupal\Tests\file\Kernel;
4
5 use Drupal\Core\Field\FieldItemInterface;
6 use Drupal\Core\TypedData\DataDefinitionInterface;
7 use Drupal\file\FileInterface;
8 use Drupal\file\ComputedFileUrl;
9 use Drupal\KernelTests\KernelTestBase;
10
11 /**
12  * @coversDefaultClass \Drupal\file\ComputedFileUrl
13  *
14  * @group file
15  */
16 class ComputedFileUrlTest extends KernelTestBase {
17
18   /**
19    * The test URL to use.
20    *
21    * @var string
22    */
23   protected $testUrl = 'public://druplicon.txt';
24
25   /**
26    * @covers ::getValue
27    */
28   public function testGetValue() {
29     $entity = $this->prophesize(FileInterface::class);
30     $entity->getFileUri()
31       ->willReturn($this->testUrl);
32
33     $parent = $this->prophesize(FieldItemInterface::class);
34     $parent->getEntity()
35       ->shouldBeCalledTimes(2)
36       ->willReturn($entity->reveal());
37
38     $definition = $this->prophesize(DataDefinitionInterface::class);
39
40     $typed_data = new ComputedFileUrl($definition->reveal(), $this->randomMachineName(), $parent->reveal());
41
42     $expected = base_path() . $this->siteDirectory . '/files/druplicon.txt';
43
44     $this->assertSame($expected, $typed_data->getValue());
45     // Do this a second time to confirm the same value is returned but the value
46     // isn't retrieved from the parent entity again.
47     $this->assertSame($expected, $typed_data->getValue());
48   }
49
50   /**
51    * @covers ::setValue
52    */
53   public function testSetValue() {
54     $name = $this->randomMachineName();
55     $parent = $this->prophesize(FieldItemInterface::class);
56     $parent->onChange($name)
57       ->shouldBeCalled();
58
59     $definition = $this->prophesize(DataDefinitionInterface::class);
60     $typed_data = new ComputedFileUrl($definition->reveal(), $name, $parent->reveal());
61
62     // Setting the value explicitly should mean the parent entity is never
63     // called into.
64     $typed_data->setValue($this->testUrl);
65
66     $this->assertSame($this->testUrl, $typed_data->getValue());
67     // Do this a second time to confirm the same value is returned but the value
68     // isn't retrieved from the parent entity again.
69     $this->assertSame($this->testUrl, $typed_data->getValue());
70   }
71
72   /**
73    * @covers ::setValue
74    */
75   public function testSetValueNoNotify() {
76     $name = $this->randomMachineName();
77     $parent = $this->prophesize(FieldItemInterface::class);
78     $parent->onChange($name)
79       ->shouldNotBeCalled();
80
81     $definition = $this->prophesize(DataDefinitionInterface::class);
82     $typed_data = new ComputedFileUrl($definition->reveal(), $name, $parent->reveal());
83
84     // Setting the value should explicitly should mean the parent entity is
85     // never called into.
86     $typed_data->setValue($this->testUrl, FALSE);
87
88     $this->assertSame($this->testUrl, $typed_data->getValue());
89   }
90
91 }