3 namespace Drupal\Tests\file\Kernel;
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;
12 * @coversDefaultClass \Drupal\file\ComputedFileUrl
16 class ComputedFileUrlTest extends KernelTestBase {
19 * The test URL to use.
23 protected $testUrl = 'public://druplicon.txt';
28 public function testGetValue() {
29 $entity = $this->prophesize(FileInterface::class);
31 ->willReturn($this->testUrl);
33 $parent = $this->prophesize(FieldItemInterface::class);
35 ->shouldBeCalledTimes(2)
36 ->willReturn($entity->reveal());
38 $definition = $this->prophesize(DataDefinitionInterface::class);
40 $typed_data = new ComputedFileUrl($definition->reveal(), $this->randomMachineName(), $parent->reveal());
42 $expected = base_path() . $this->siteDirectory . '/files/druplicon.txt';
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());
53 public function testSetValue() {
54 $name = $this->randomMachineName();
55 $parent = $this->prophesize(FieldItemInterface::class);
56 $parent->onChange($name)
59 $definition = $this->prophesize(DataDefinitionInterface::class);
60 $typed_data = new ComputedFileUrl($definition->reveal(), $name, $parent->reveal());
62 // Setting the value explicitly should mean the parent entity is never
64 $typed_data->setValue($this->testUrl);
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());
75 public function testSetValueNoNotify() {
76 $name = $this->randomMachineName();
77 $parent = $this->prophesize(FieldItemInterface::class);
78 $parent->onChange($name)
79 ->shouldNotBeCalled();
81 $definition = $this->prophesize(DataDefinitionInterface::class);
82 $typed_data = new ComputedFileUrl($definition->reveal(), $name, $parent->reveal());
84 // Setting the value should explicitly should mean the parent entity is
86 $typed_data->setValue($this->testUrl, FALSE);
88 $this->assertSame($this->testUrl, $typed_data->getValue());