function TypedDataTest::testGetAndSet

Tests the basics around constructing and working with typed data objects.

File

core/tests/Drupal/KernelTests/Core/TypedData/TypedDataTest.php, line 77

Class

TypedDataTest
Tests the functionality of all core data types.

Namespace

Drupal\KernelTests\Core\TypedData

Code

public function testGetAndSet() : void {
  // Boolean type.
  $typed_data = $this->createTypedData([
    'type' => 'boolean',
  ], TRUE);
  $this->assertInstanceOf(BooleanInterface::class, $typed_data);
  $this->assertTrue($typed_data->getValue(), 'Boolean value was fetched.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue(FALSE);
  $this->assertFalse($typed_data->getValue(), 'Boolean value was changed.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $this->assertIsString($typed_data->getString());
  $typed_data->setValue(NULL);
  $this->assertNull($typed_data->getValue(), 'Boolean wrapper is null-able.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue('invalid');
  $this->assertEquals(1, $typed_data->validate()
    ->count(), 'Validation detected invalid value.');
  // String type.
  $value = $this->randomString();
  $typed_data = $this->createTypedData([
    'type' => 'string',
  ], $value);
  $this->assertInstanceOf(StringInterface::class, $typed_data);
  $this->assertSame($value, $typed_data->getValue(), 'String value was fetched.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $new_value = $this->randomString();
  $typed_data->setValue($new_value);
  $this->assertSame($new_value, $typed_data->getValue(), 'String value was changed.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  // Funky test.
  $this->assertIsString($typed_data->getString());
  $typed_data->setValue(NULL);
  $this->assertNull($typed_data->getValue(), 'String wrapper is null-able.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue([
    'no string',
  ]);
  $this->assertEquals(1, $typed_data->validate()
    ->count(), 'Validation detected invalid value.');
  // Integer type.
  $value = rand();
  $typed_data = $this->createTypedData([
    'type' => 'integer',
  ], $value);
  $this->assertInstanceOf(IntegerInterface::class, $typed_data);
  $this->assertSame($value, $typed_data->getValue(), 'Integer value was fetched.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $new_value = rand();
  $typed_data->setValue($new_value);
  $this->assertSame($new_value, $typed_data->getValue(), 'Integer value was changed.');
  $this->assertIsString($typed_data->getString());
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue(NULL);
  $this->assertNull($typed_data->getValue(), 'Integer wrapper is null-able.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue('invalid');
  $this->assertEquals(1, $typed_data->validate()
    ->count(), 'Validation detected invalid value.');
  // Decimal type.
  $value = (string) (mt_rand(1, 10000) / 100);
  $typed_data = $this->createTypedData([
    'type' => 'decimal',
  ], $value);
  $this->assertInstanceOf(DecimalInterface::class, $typed_data);
  $this->assertSame($value, $typed_data->getValue(), 'Decimal value was fetched.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $new_value = (string) (mt_rand(1, 10000) / 100);
  $typed_data->setValue($new_value);
  $this->assertSame($new_value, $typed_data->getValue(), 'Decimal value was changed.');
  $this->assertIsString($typed_data->getString());
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue(NULL);
  $this->assertNull($typed_data->getValue(), 'Decimal wrapper is null-able.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue(0);
  $this->assertSame('0.0', $typed_data->getCastedValue(), '0.0 casted value was fetched.');
  $typed_data->setValue('1337e0');
  $this->assertEquals(1, $typed_data->validate()
    ->count(), 'Scientific notation is not allowed in numeric type.');
  $typed_data->setValue('invalid');
  $this->assertEquals(1, $typed_data->validate()
    ->count(), 'Validation detected invalid value.');
  // Float type.
  $value = 123.45;
  $typed_data = $this->createTypedData([
    'type' => 'float',
  ], $value);
  $this->assertInstanceOf(FloatInterface::class, $typed_data);
  $this->assertSame($value, $typed_data->getValue(), 'Float value was fetched.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $new_value = 678.9;
  $typed_data->setValue($new_value);
  $this->assertSame($new_value, $typed_data->getValue(), 'Float value was changed.');
  $this->assertIsString($typed_data->getString());
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue(NULL);
  $this->assertNull($typed_data->getValue(), 'Float wrapper is null-able.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue('invalid');
  $this->assertEquals(1, $typed_data->validate()
    ->count(), 'Validation detected invalid value.');
  // Date Time type; values with timezone offset.
  $value = '2014-01-01T20:00:00+00:00';
  $typed_data = $this->createTypedData([
    'type' => 'datetime_iso8601',
  ], $value);
  $this->assertInstanceOf(DateTimeInterface::class, $typed_data);
  $this->assertSame($value, $typed_data->getValue());
  $this->assertEquals($typed_data->getDateTime()
    ->format('c'), $typed_data->getValue(), 'Value representation of a date is ISO 8601');
  $this->assertSame('+00:00', $typed_data->getDateTime()
    ->getTimezone()
    ->getName());
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $new_value = '2014-01-02T20:00:00+00:00';
  $typed_data->setValue($new_value);
  $this->assertSame($new_value, $typed_data->getDateTime()
    ->format('c'), 'Date value was changed and set by an ISO8601 date.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $this->assertSame('2014-01-02', $typed_data->getDateTime()
    ->format('Y-m-d'), 'Date value was changed and set by date string.');
  $this->assertSame('+00:00', $typed_data->getDateTime()
    ->getTimezone()
    ->getName());
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue(NULL);
  $this->assertNull($typed_data->getDateTime(), 'Date wrapper is null-able.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue('invalid');
  $this->assertEquals(1, $typed_data->validate()
    ->count(), 'Validation detected invalid value.');
  // Check implementation of DateTimeInterface.
  $typed_data = $this->createTypedData([
    'type' => 'datetime_iso8601',
  ], '2014-01-01T20:00:00+00:00');
  $this->assertInstanceOf(DrupalDateTime::class, $typed_data->getDateTime());
  $this->assertSame('+00:00', $typed_data->getDateTime()
    ->getTimezone()
    ->getName());
  $typed_data->setDateTime(new DrupalDateTime('2014-01-02T20:00:00+00:00'));
  $this->assertSame('+00:00', $typed_data->getDateTime()
    ->getTimezone()
    ->getName());
  $this->assertEquals('2014-01-02T20:00:00+00:00', $typed_data->getValue());
  $typed_data->setValue(NULL);
  $this->assertNull($typed_data->getDateTime());
  // Date Time type; values without timezone offset.
  $value = '2014-01-01T20:00';
  $typed_data = $this->createTypedData([
    'type' => 'datetime_iso8601',
  ], $value);
  $this->assertInstanceOf(DateTimeInterface::class, $typed_data);
  $this->assertSame($value, $typed_data->getValue(), 'Date value was fetched.');
  // @todo Uncomment this assertion in
  //   https://www.drupal.org/project/drupal/issues/2716891.
  // phpcs:ignore
  // $this->assertEquals($typed_data->getDateTime()->format('c'), $typed_data->getValue(), 'Value representation of a date is ISO 8601');
  $this->assertSame('UTC', $typed_data->getDateTime()
    ->getTimezone()
    ->getName());
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $new_value = '2014-01-02T20:00';
  $typed_data->setValue($new_value);
  // @todo Uncomment this assertion in
  //   https://www.drupal.org/project/drupal/issues/2716891.
  // phpcs:ignore
  // $this->assertTrue($typed_data->getDateTime()->format('c') === $new_value, 'Date value was changed and set by an ISO8601 date.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $this->assertSame('2014-01-02', $typed_data->getDateTime()
    ->format('Y-m-d'), 'Date value was changed and set by date string.');
  $this->assertSame('UTC', $typed_data->getDateTime()
    ->getTimezone()
    ->getName());
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue(NULL);
  $this->assertNull($typed_data->getDateTime(), 'Date wrapper is null-able.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue('invalid');
  $this->assertEquals(1, $typed_data->validate()
    ->count(), 'Validation detected invalid value.');
  // Check implementation of DateTimeInterface.
  $typed_data = $this->createTypedData([
    'type' => 'datetime_iso8601',
  ], '2014-01-01T20:00:00');
  $this->assertInstanceOf(DrupalDateTime::class, $typed_data->getDateTime());
  $this->assertSame('UTC', $typed_data->getDateTime()
    ->getTimezone()
    ->getName());
  // When setting datetime without a timezone offset, the default timezone is
  // used (Australia/Sydney). DateTimeIso8601::setDateTime() converts this
  // DrupalDateTime object to a string using ::format('c'), it gets converted
  // to an offset. The offset for Australia/Sydney is +11:00.
  $typed_data->setDateTime(new DrupalDateTime('2014-01-02T20:00:00'));
  $this->assertSame('+11:00', $typed_data->getDateTime()
    ->getTimezone()
    ->getName());
  $this->assertEquals('2014-01-02T20:00:00+11:00', $typed_data->getValue());
  $typed_data->setValue(NULL);
  $this->assertNull($typed_data->getDateTime());
  // Timestamp type.
  $requestTime = \Drupal::time()->getRequestTime();
  $value = $requestTime;
  $typed_data = $this->createTypedData([
    'type' => 'timestamp',
  ], $value);
  $this->assertInstanceOf(DateTimeInterface::class, $typed_data);
  $this->assertSame($typed_data->getValue(), $value, 'Timestamp value was fetched.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $new_value = $requestTime + 1;
  $typed_data->setValue($new_value);
  $this->assertSame($typed_data->getValue(), $new_value, 'Timestamp value was changed and set.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue(NULL);
  $this->assertNull($typed_data->getDateTime(), 'Timestamp wrapper is null-able.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue('invalid');
  $this->assertEquals(1, $typed_data->validate()
    ->count(), 'Validation detected invalid value.');
  // Check implementation of DateTimeInterface.
  $typed_data = $this->createTypedData([
    'type' => 'timestamp',
  ], $requestTime);
  $this->assertInstanceOf(DrupalDateTime::class, $typed_data->getDateTime());
  $typed_data->setDateTime(DrupalDateTime::createFromTimestamp($requestTime + 1));
  $this->assertEquals($requestTime + 1, $typed_data->getValue());
  $typed_data->setValue(NULL);
  $this->assertNull($typed_data->getDateTime());
  // DurationIso8601 type.
  $value = 'PT20S';
  $typed_data = $this->createTypedData([
    'type' => 'duration_iso8601',
  ], $value);
  $this->assertInstanceOf(DurationInterface::class, $typed_data);
  $this->assertSame($value, $typed_data->getValue(), 'DurationIso8601 value was fetched.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue('P40D');
  $this->assertEquals(40, $typed_data->getDuration()->d, 'DurationIso8601 value was changed and set by duration string.');
  $this->assertIsString($typed_data->getString());
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue(NULL);
  $this->assertNull($typed_data->getValue(), 'DurationIso8601 wrapper is null-able.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue('invalid');
  $this->assertEquals(1, $typed_data->validate()
    ->count(), 'Validation detected invalid value.');
  // Check implementation of DurationInterface.
  $typed_data = $this->createTypedData([
    'type' => 'duration_iso8601',
  ], 'PT20S');
  $this->assertInstanceOf(\DateInterval::class, $typed_data->getDuration());
  $typed_data->setDuration(new \DateInterval('P40D'));
  // @todo Should we make this "nicer"?
  $this->assertEquals('P0Y0M40DT0H0M0S', $typed_data->getValue());
  $typed_data->setValue(NULL);
  $this->assertNull($typed_data->getDuration());
  // Time span type.
  $value = 20;
  $typed_data = $this->createTypedData([
    'type' => 'timespan',
  ], $value);
  $this->assertInstanceOf(DurationInterface::class, $typed_data);
  $this->assertSame($value, $typed_data->getValue(), 'Time span value was fetched.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue(60 * 60 * 4);
  $this->assertEquals(14400, $typed_data->getDuration()->s, 'Time span was changed');
  $this->assertIsString($typed_data->getString());
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue(NULL);
  $this->assertNull($typed_data->getValue(), 'Time span wrapper is null-able.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue('invalid');
  $this->assertEquals(1, $typed_data->validate()
    ->count(), 'Validation detected invalid value.');
  // Check implementation of DurationInterface.
  $typed_data = $this->createTypedData([
    'type' => 'timespan',
  ], 20);
  $this->assertInstanceOf(\DateInterval::class, $typed_data->getDuration());
  $typed_data->setDuration(new \DateInterval('PT4H'));
  $this->assertEquals(60 * 60 * 4, $typed_data->getValue());
  $typed_data->setValue(NULL);
  $this->assertNull($typed_data->getDuration());
  // URI type.
  $uri = 'http://example.com/foo/';
  $typed_data = $this->createTypedData([
    'type' => 'uri',
  ], $uri);
  $this->assertInstanceOf(UriInterface::class, $typed_data);
  $this->assertSame($uri, $typed_data->getValue(), 'URI value was fetched.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue($uri . 'bar.txt');
  $this->assertSame($uri . 'bar.txt', $typed_data->getValue(), 'URI value was changed.');
  $this->assertIsString($typed_data->getString());
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue(NULL);
  $this->assertNull($typed_data->getValue(), 'URI wrapper is null-able.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue('invalid');
  $this->assertEquals(1, $typed_data->validate()
    ->count(), 'Validation detected invalid value.');
  $typed_data->setValue('public://field/image/Photo on 4-28-14 at 12.01 PM.jpg');
  $this->assertEquals(0, $typed_data->validate()
    ->count(), 'Filename with spaces is valid.');
  // Generate some files that will be used to test the binary data type.
  $files = [];
  for ($i = 0; $i < 3; $i++) {
    $path = "public://example_{$i}.png";
    \Drupal::service('file_system')->copy($this->root . '/core/misc/druplicon.png', $path);
    $image = File::create([
      'uri' => $path,
    ]);
    $image->save();
    $files[] = $image;
  }
  // Email type.
  $value = $this->randomString();
  $typed_data = $this->createTypedData([
    'type' => 'email',
  ], $value);
  $this->assertInstanceOf(StringInterface::class, $typed_data);
  $this->assertSame($value, $typed_data->getValue(), 'Email value was fetched.');
  $new_value = 'test@example.com';
  $typed_data->setValue($new_value);
  $this->assertSame($new_value, $typed_data->getValue(), 'Email value was changed.');
  $this->assertIsString($typed_data->getString());
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue(NULL);
  $this->assertNull($typed_data->getValue(), 'Email wrapper is null-able.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue('invalidAtExample.com');
  $this->assertEquals(1, $typed_data->validate()
    ->count(), 'Validation detected invalid value.');
  // Binary type.
  $typed_data = $this->createTypedData([
    'type' => 'binary',
  ], $files[0]->getFileUri());
  $this->assertInstanceOf(BinaryInterface::class, $typed_data);
  $this->assertIsResource($typed_data->getValue());
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  // Try setting by URI.
  $typed_data->setValue($files[1]->getFileUri());
  $this->assertEquals(fgets(fopen($files[1]->getFileUri(), 'r')), fgets($typed_data->getValue()), 'Binary value was changed.');
  $this->assertIsString($typed_data->getString());
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  // Try setting by resource.
  $typed_data->setValue(fopen($files[2]->getFileUri(), 'r'));
  $this->assertEquals(fgets($typed_data->getValue()), fgets(fopen($files[2]->getFileUri(), 'r')), 'Binary value was changed.');
  $this->assertIsString($typed_data->getString());
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue(NULL);
  $this->assertNull($typed_data->getValue(), 'Binary wrapper is null-able.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue('invalid');
  $this->assertEquals(1, $typed_data->validate()
    ->count(), 'Validation detected invalid value.');
  // Any type.
  $value = [
    'foo',
  ];
  $typed_data = $this->createTypedData([
    'type' => 'any',
  ], $value);
  $this->assertSame($value, $typed_data->getValue(), 'Any value was fetched.');
  $new_value = 'test@example.com';
  $typed_data->setValue($new_value);
  $this->assertSame($new_value, $typed_data->getValue(), 'Any value was changed.');
  $this->assertIsString($typed_data->getString());
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue(NULL);
  $this->assertNull($typed_data->getValue(), 'Any wrapper is null-able.');
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  // We cannot test invalid values as everything is valid for the any type,
  // but make sure an array or object value passes validation also.
  $typed_data->setValue([
    'entry',
  ]);
  $this->assertEquals(0, $typed_data->validate()
    ->count());
  $typed_data->setValue((object) [
    'entry',
  ]);
  $this->assertEquals(0, $typed_data->validate()
    ->count());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.