ComputedFileUrlTest.php in Drupal 8
File
core/modules/file/tests/src/Kernel/ComputedFileUrlTest.php
View source
<?php
namespace Drupal\Tests\file\Kernel;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\file\FileInterface;
use Drupal\file\ComputedFileUrl;
use Drupal\KernelTests\KernelTestBase;
class ComputedFileUrlTest extends KernelTestBase {
protected $testUrl = 'public://druplicon.txt';
public function testGetValue() {
$entity = $this
->prophesize(FileInterface::class);
$entity
->getFileUri()
->willReturn($this->testUrl);
$parent = $this
->prophesize(FieldItemInterface::class);
$parent
->getEntity()
->shouldBeCalledTimes(2)
->willReturn($entity
->reveal());
$definition = $this
->prophesize(DataDefinitionInterface::class);
$typed_data = new ComputedFileUrl($definition
->reveal(), $this
->randomMachineName(), $parent
->reveal());
$expected = base_path() . $this->siteDirectory . '/files/druplicon.txt';
$this
->assertSame($expected, $typed_data
->getValue());
$this
->assertSame($expected, $typed_data
->getValue());
}
public function testSetValue() {
$name = $this
->randomMachineName();
$parent = $this
->prophesize(FieldItemInterface::class);
$parent
->onChange($name)
->shouldBeCalled();
$definition = $this
->prophesize(DataDefinitionInterface::class);
$typed_data = new ComputedFileUrl($definition
->reveal(), $name, $parent
->reveal());
$typed_data
->setValue($this->testUrl);
$this
->assertSame($this->testUrl, $typed_data
->getValue());
$this
->assertSame($this->testUrl, $typed_data
->getValue());
}
public function testSetValueNoNotify() {
$name = $this
->randomMachineName();
$parent = $this
->prophesize(FieldItemInterface::class);
$parent
->onChange($name)
->shouldNotBeCalled();
$definition = $this
->prophesize(DataDefinitionInterface::class);
$typed_data = new ComputedFileUrl($definition
->reveal(), $name, $parent
->reveal());
$typed_data
->setValue($this->testUrl, FALSE);
$this
->assertSame($this->testUrl, $typed_data
->getValue());
}
}