You are here

public function TypedDataTest::testGetAndSet in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/system/src/Tests/TypedData/TypedDataTest.php \Drupal\system\Tests\TypedData\TypedDataTest::testGetAndSet()

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

File

core/modules/system/src/Tests/TypedData/TypedDataTest.php, line 63
Contains \Drupal\system\Tests\TypedData\TypedDataTest.

Class

TypedDataTest
Tests the functionality of all core data types.

Namespace

Drupal\system\Tests\TypedData

Code

public function testGetAndSet() {

  // Boolean type.
  $typed_data = $this
    ->createTypedData(array(
    'type' => 'boolean',
  ), TRUE);
  $this
    ->assertTrue($typed_data instanceof \Drupal\Core\TypedData\Type\BooleanInterface, 'Typed data object is an instance of BooleanInterface.');
  $this
    ->assertTrue($typed_data
    ->getValue() === TRUE, 'Boolean value was fetched.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue(FALSE);
  $this
    ->assertTrue($typed_data
    ->getValue() === FALSE, 'Boolean value was changed.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $this
    ->assertTrue(is_string($typed_data
    ->getString()), 'Boolean value was converted to string');
  $typed_data
    ->setValue(NULL);
  $this
    ->assertNull($typed_data
    ->getValue(), 'Boolean wrapper is null-able.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue('invalid');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 1, 'Validation detected invalid value.');

  // String type.
  $value = $this
    ->randomString();
  $typed_data = $this
    ->createTypedData(array(
    'type' => 'string',
  ), $value);
  $this
    ->assertTrue($typed_data instanceof \Drupal\Core\TypedData\Type\StringInterface, 'Typed data object is an instance of StringInterface.');
  $this
    ->assertTrue($typed_data
    ->getValue() === $value, 'String value was fetched.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $new_value = $this
    ->randomString();
  $typed_data
    ->setValue($new_value);
  $this
    ->assertTrue($typed_data
    ->getValue() === $new_value, 'String value was changed.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);

  // Funky test.
  $this
    ->assertTrue(is_string($typed_data
    ->getString()), 'String value was converted to string');
  $typed_data
    ->setValue(NULL);
  $this
    ->assertNull($typed_data
    ->getValue(), 'String wrapper is null-able.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue(array(
    'no string',
  ));
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 1, 'Validation detected invalid value.');

  // Integer type.
  $value = rand();
  $typed_data = $this
    ->createTypedData(array(
    'type' => 'integer',
  ), $value);
  $this
    ->assertTrue($typed_data instanceof \Drupal\Core\TypedData\Type\IntegerInterface, 'Typed data object is an instance of IntegerInterface.');
  $this
    ->assertTrue($typed_data
    ->getValue() === $value, 'Integer value was fetched.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $new_value = rand();
  $typed_data
    ->setValue($new_value);
  $this
    ->assertTrue($typed_data
    ->getValue() === $new_value, 'Integer value was changed.');
  $this
    ->assertTrue(is_string($typed_data
    ->getString()), 'Integer value was converted to string');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue(NULL);
  $this
    ->assertNull($typed_data
    ->getValue(), 'Integer wrapper is null-able.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue('invalid');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 1, 'Validation detected invalid value.');

  // Float type.
  $value = 123.45;
  $typed_data = $this
    ->createTypedData(array(
    'type' => 'float',
  ), $value);
  $this
    ->assertTrue($typed_data instanceof \Drupal\Core\TypedData\Type\FloatInterface, 'Typed data object is an instance of FloatInterface.');
  $this
    ->assertTrue($typed_data
    ->getValue() === $value, 'Float value was fetched.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $new_value = 678.9;
  $typed_data
    ->setValue($new_value);
  $this
    ->assertTrue($typed_data
    ->getValue() === $new_value, 'Float value was changed.');
  $this
    ->assertTrue(is_string($typed_data
    ->getString()), 'Float value was converted to string');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue(NULL);
  $this
    ->assertNull($typed_data
    ->getValue(), 'Float wrapper is null-able.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue('invalid');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 1, 'Validation detected invalid value.');

  // Date Time type.
  $value = '2014-01-01T20:00:00+00:00';
  $typed_data = $this
    ->createTypedData(array(
    'type' => 'datetime_iso8601',
  ), $value);
  $this
    ->assertTrue($typed_data instanceof \Drupal\Core\TypedData\Type\DateTimeInterface, 'Typed data object is an instance of DateTimeInterface.');
  $this
    ->assertTrue($typed_data
    ->getValue() == $value, 'Date value was fetched.');
  $this
    ->assertEqual($typed_data
    ->getValue(), $typed_data
    ->getDateTime()
    ->format('c'), 'Value representation of a date is ISO 8601');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $new_value = '2014-01-02T20:00:00+00:00';
  $typed_data
    ->setValue($new_value);
  $this
    ->assertTrue($typed_data
    ->getDateTime()
    ->format('c') === $new_value, 'Date value was changed and set by an ISO8601 date.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $this
    ->assertTrue($typed_data
    ->getDateTime()
    ->format('Y-m-d') == '2014-01-02', 'Date value was changed and set by date string.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue(NULL);
  $this
    ->assertNull($typed_data
    ->getDateTime(), 'Date wrapper is null-able.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue('invalid');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 1, 'Validation detected invalid value.');

  // Check implementation of DateTimeInterface.
  $typed_data = $this
    ->createTypedData(array(
    'type' => 'datetime_iso8601',
  ), '2014-01-01T20:00:00+00:00');
  $this
    ->assertTrue($typed_data
    ->getDateTime() instanceof DrupalDateTime);
  $typed_data
    ->setDateTime(new DrupalDateTime('2014-01-02T20:00:00+00:00'));
  $this
    ->assertEqual($typed_data
    ->getValue(), '2014-01-02T20:00:00+00:00');
  $typed_data
    ->setValue(NULL);
  $this
    ->assertNull($typed_data
    ->getDateTime());

  // Timestamp type.
  $value = REQUEST_TIME;
  $typed_data = $this
    ->createTypedData(array(
    'type' => 'timestamp',
  ), $value);
  $this
    ->assertTrue($typed_data instanceof \Drupal\Core\TypedData\Type\DateTimeInterface, 'Typed data object is an instance of DateTimeInterface.');
  $this
    ->assertTrue($typed_data
    ->getValue() == $value, 'Timestamp value was fetched.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $new_value = REQUEST_TIME + 1;
  $typed_data
    ->setValue($new_value);
  $this
    ->assertTrue($typed_data
    ->getValue() === $new_value, 'Timestamp value was changed and set.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue(NULL);
  $this
    ->assertNull($typed_data
    ->getDateTime(), 'Timestamp wrapper is null-able.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue('invalid');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 1, 'Validation detected invalid value.');

  // Check implementation of DateTimeInterface.
  $typed_data = $this
    ->createTypedData(array(
    'type' => 'timestamp',
  ), REQUEST_TIME);
  $this
    ->assertTrue($typed_data
    ->getDateTime() instanceof DrupalDateTime);
  $typed_data
    ->setDateTime(DrupalDateTime::createFromTimestamp(REQUEST_TIME + 1));
  $this
    ->assertEqual($typed_data
    ->getValue(), REQUEST_TIME + 1);
  $typed_data
    ->setValue(NULL);
  $this
    ->assertNull($typed_data
    ->getDateTime());

  // DurationIso8601 type.
  $value = 'PT20S';
  $typed_data = $this
    ->createTypedData(array(
    'type' => 'duration_iso8601',
  ), $value);
  $this
    ->assertTrue($typed_data instanceof \Drupal\Core\TypedData\Type\DurationInterface, 'Typed data object is an instance of DurationInterface.');
  $this
    ->assertIdentical($typed_data
    ->getValue(), $value, 'DurationIso8601 value was fetched.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue('P40D');
  $this
    ->assertEqual($typed_data
    ->getDuration()->d, 40, 'DurationIso8601 value was changed and set by duration string.');
  $this
    ->assertTrue(is_string($typed_data
    ->getString()), 'DurationIso8601 value was converted to string');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue(NULL);
  $this
    ->assertNull($typed_data
    ->getValue(), 'DurationIso8601 wrapper is null-able.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue('invalid');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 1, 'Validation detected invalid value.');

  // Check implementation of DurationInterface.
  $typed_data = $this
    ->createTypedData(array(
    'type' => 'duration_iso8601',
  ), 'PT20S');
  $this
    ->assertTrue($typed_data
    ->getDuration() instanceof \DateInterval);
  $typed_data
    ->setDuration(new \DateInterval('P40D'));

  // @todo: Should we make this "nicer"?
  $this
    ->assertEqual($typed_data
    ->getValue(), 'P0Y0M40DT0H0M0S');
  $typed_data
    ->setValue(NULL);
  $this
    ->assertNull($typed_data
    ->getDuration());

  // Time span type.
  $value = 20;
  $typed_data = $this
    ->createTypedData(array(
    'type' => 'timespan',
  ), $value);
  $this
    ->assertTrue($typed_data instanceof \Drupal\Core\TypedData\Type\DurationInterface, 'Typed data object is an instance of DurationInterface.');
  $this
    ->assertIdentical($typed_data
    ->getValue(), $value, 'Time span value was fetched.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue(60 * 60 * 4);
  $this
    ->assertEqual($typed_data
    ->getDuration()->s, 14400, 'Time span was changed');
  $this
    ->assertTrue(is_string($typed_data
    ->getString()), 'Time span value was converted to string');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue(NULL);
  $this
    ->assertNull($typed_data
    ->getValue(), 'Time span wrapper is null-able.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue('invalid');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 1, 'Validation detected invalid value.');

  // Check implementation of DurationInterface.
  $typed_data = $this
    ->createTypedData(array(
    'type' => 'timespan',
  ), 20);
  $this
    ->assertTrue($typed_data
    ->getDuration() instanceof \DateInterval);
  $typed_data
    ->setDuration(new \DateInterval('PT4H'));
  $this
    ->assertEqual($typed_data
    ->getValue(), 60 * 60 * 4);
  $typed_data
    ->setValue(NULL);
  $this
    ->assertNull($typed_data
    ->getDuration());

  // URI type.
  $uri = 'http://example.com/foo/';
  $typed_data = $this
    ->createTypedData(array(
    'type' => 'uri',
  ), $uri);
  $this
    ->assertTrue($typed_data instanceof \Drupal\Core\TypedData\Type\UriInterface, 'Typed data object is an instance of UriInterface.');
  $this
    ->assertTrue($typed_data
    ->getValue() === $uri, 'URI value was fetched.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue($uri . 'bar.txt');
  $this
    ->assertTrue($typed_data
    ->getValue() === $uri . 'bar.txt', 'URI value was changed.');
  $this
    ->assertTrue(is_string($typed_data
    ->getString()), 'URI value was converted to string');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue(NULL);
  $this
    ->assertNull($typed_data
    ->getValue(), 'URI wrapper is null-able.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue('invalid');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 1, 'Validation detected invalid value.');
  $typed_data
    ->setValue('public://field/image/Photo on 4-28-14 at 12.01 PM.jpg');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0, 'Filename with spaces is valid.');

  // Generate some files that will be used to test the binary data type.
  $files = array();
  for ($i = 0; $i < 3; $i++) {
    $path = "public://example_{$i}.png";
    file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', $path);
    $image = entity_create('file', array(
      'uri' => $path,
    ));
    $image
      ->save();
    $files[] = $image;
  }

  // Email type.
  $value = $this
    ->randomString();
  $typed_data = $this
    ->createTypedData(array(
    'type' => 'email',
  ), $value);
  $this
    ->assertTrue($typed_data instanceof \Drupal\Core\TypedData\Type\StringInterface, 'Typed data object is an instance of StringInterface.');
  $this
    ->assertIdentical($typed_data
    ->getValue(), $value, 'Email value was fetched.');
  $new_value = 'test@example.com';
  $typed_data
    ->setValue($new_value);
  $this
    ->assertIdentical($typed_data
    ->getValue(), $new_value, 'Email value was changed.');
  $this
    ->assertTrue(is_string($typed_data
    ->getString()), 'Email value was converted to string');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue(NULL);
  $this
    ->assertNull($typed_data
    ->getValue(), 'Email wrapper is null-able.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue('invalidATexample.com');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 1, 'Validation detected invalid value.');

  // Binary type.
  $typed_data = $this
    ->createTypedData(array(
    'type' => 'binary',
  ), $files[0]
    ->getFileUri());
  $this
    ->assertTrue($typed_data instanceof \Drupal\Core\TypedData\Type\BinaryInterface, 'Typed data object is an instance of BinaryInterface.');
  $this
    ->assertTrue(is_resource($typed_data
    ->getValue()), 'Binary value was fetched.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);

  // Try setting by URI.
  $typed_data
    ->setValue($files[1]
    ->getFileUri());
  $this
    ->assertEqual(fgets($typed_data
    ->getValue()), fgets(fopen($files[1]
    ->getFileUri(), 'r')), 'Binary value was changed.');
  $this
    ->assertTrue(is_string($typed_data
    ->getString()), 'Binary value was converted to string');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);

  // Try setting by resource.
  $typed_data
    ->setValue(fopen($files[2]
    ->getFileUri(), 'r'));
  $this
    ->assertEqual(fgets($typed_data
    ->getValue()), fgets(fopen($files[2]
    ->getFileUri(), 'r')), 'Binary value was changed.');
  $this
    ->assertTrue(is_string($typed_data
    ->getString()), 'Binary value was converted to string');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue(NULL);
  $this
    ->assertNull($typed_data
    ->getValue(), 'Binary wrapper is null-able.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue('invalid');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 1, 'Validation detected invalid value.');

  // Any type.
  $value = array(
    'foo',
  );
  $typed_data = $this
    ->createTypedData(array(
    'type' => 'any',
  ), $value);
  $this
    ->assertIdentical($typed_data
    ->getValue(), $value, 'Any value was fetched.');
  $new_value = 'test@example.com';
  $typed_data
    ->setValue($new_value);
  $this
    ->assertIdentical($typed_data
    ->getValue(), $new_value, 'Any value was changed.');
  $this
    ->assertTrue(is_string($typed_data
    ->getString()), 'Any value was converted to string');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue(NULL);
  $this
    ->assertNull($typed_data
    ->getValue(), 'Any wrapper is null-able.');
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);

  // 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(array(
    'entry',
  ));
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
  $typed_data
    ->setValue((object) array(
    'entry',
  ));
  $this
    ->assertEqual($typed_data
    ->validate()
    ->count(), 0);
}