/**
  * Tests deriving metadata about list items.
  */
 public function testLists()
 {
     $list_definition = ListDataDefinition::create('string');
     $this->assertTrue($list_definition instanceof ListDataDefinitionInterface);
     $item_definition = $list_definition->getItemDefinition();
     $this->assertTrue($item_definition instanceof DataDefinitionInterface);
     $this->assertEqual($item_definition->getDataType(), 'string');
     // Test using the definition factory.
     $list_definition2 = $this->typedDataManager->createListDataDefinition('string');
     $this->assertTrue($list_definition2 instanceof ListDataDefinitionInterface);
     $this->assertEqual($list_definition, $list_definition2);
     // Test creating the definition of data with type 'list', which is the same
     // as creating a definition of a list of items of type 'any'.
     $list_definition = $this->typedDataManager->createDataDefinition('list');
     $this->assertTrue($list_definition instanceof ListDataDefinitionInterface);
     $this->assertEqual($list_definition->getDataType(), 'list');
     $this->assertEqual($list_definition->getItemDefinition()->getDataType(), 'any');
 }
Exemplo n.º 2
0
 /**
  * Tests the filter() method on typed data lists.
  */
 public function testTypedDataListsFilter()
 {
     // Check that an all-pass filter leaves the list untouched.
     $value = array('zero', 'one');
     $typed_data = $this->createTypedData(ListDataDefinition::create('string'), $value);
     $typed_data->filter(function (TypedDataInterface $item) {
         return TRUE;
     });
     $this->assertEqual($typed_data->count(), 2);
     $this->assertEqual($typed_data[0]->getValue(), 'zero');
     $this->assertEqual($typed_data[0]->getName(), 0);
     $this->assertEqual($typed_data[1]->getValue(), 'one');
     $this->assertEqual($typed_data[1]->getName(), 1);
     // Check that a none-pass filter empties the list.
     $value = array('zero', 'one');
     $typed_data = $this->createTypedData(ListDataDefinition::create('string'), $value);
     $typed_data->filter(function (TypedDataInterface $item) {
         return FALSE;
     });
     $this->assertEqual($typed_data->count(), 0);
     // Check that filtering correctly renumbers elements.
     $value = array('zero', 'one', 'two');
     $typed_data = $this->createTypedData(ListDataDefinition::create('string'), $value);
     $typed_data->filter(function (TypedDataInterface $item) {
         return $item->getValue() !== 'one';
     });
     $this->assertEqual($typed_data->count(), 2);
     $this->assertEqual($typed_data[0]->getValue(), 'zero');
     $this->assertEqual($typed_data[0]->getName(), 0);
     $this->assertEqual($typed_data[1]->getValue(), 'two');
     $this->assertEqual($typed_data[1]->getName(), 1);
 }
Exemplo n.º 3
0
 /**
  * Tests using typed data lists.
  */
 public function testTypedDataLists()
 {
     // Test working with an existing list of strings.
     $value = array('one', 'two', 'three');
     $typed_data = $this->createTypedData(ListDataDefinition::create('string'), $value);
     $this->assertEqual($typed_data->getValue(), $value, 'List value has been set.');
     // Test iterating.
     $count = 0;
     foreach ($typed_data as $item) {
         $this->assertTrue($item instanceof \Drupal\Core\TypedData\TypedDataInterface);
         $count++;
     }
     $this->assertEqual($count, 3);
     // Test getting the string representation.
     $this->assertEqual($typed_data->getString(), 'one, two, three');
     $typed_data[1] = '';
     $this->assertEqual($typed_data->getString(), 'one, three');
     // Test using array access.
     $this->assertEqual($typed_data[0]->getValue(), 'one');
     $typed_data[4] = 'four';
     $this->assertEqual($typed_data[4]->getValue(), 'four');
     $typed_data[] = 'five';
     $this->assertEqual($typed_data[5]->getValue(), 'five');
     $this->assertEqual($typed_data->count(), 5);
     $this->assertTrue(isset($typed_data[0]));
     $this->assertTrue(!isset($typed_data[6]));
     // Test isEmpty and cloning.
     $this->assertFalse($typed_data->isEmpty());
     $clone = clone $typed_data;
     $this->assertTrue($typed_data->getValue() === $clone->getValue());
     $this->assertTrue($typed_data[0] !== $clone[0]);
     $clone->setValue(array());
     $this->assertTrue($clone->isEmpty());
     // Make sure that resetting the value using NULL results in an empty array.
     $clone->setValue(array());
     $typed_data->setValue(NULL);
     $this->assertIdentical($typed_data->getValue(), array());
     $this->assertIdentical($clone->getValue(), array());
     // Test dealing with NULL items.
     $typed_data[] = NULL;
     $this->assertTrue($typed_data->isEmpty());
     $this->assertEqual(count($typed_data), 1);
     $typed_data[] = '';
     $this->assertFalse($typed_data->isEmpty());
     $this->assertEqual(count($typed_data), 2);
     $typed_data[] = 'three';
     $this->assertFalse($typed_data->isEmpty());
     $this->assertEqual(count($typed_data), 3);
     $this->assertEqual($typed_data->getValue(), array(NULL, '', 'three'));
     // Test unsetting.
     unset($typed_data[2]);
     $this->assertEqual(count($typed_data), 2);
     $this->assertNull($typed_data[3]->getValue());
     // Getting a not set list item sets it.
     $this->assertNull($typed_data[4]->getValue());
     $this->assertEqual(count($typed_data), 4);
     // Test setting the list with less values.
     $typed_data->setValue(array('one'));
     $this->assertEqual($typed_data->count(), 1);
     // Test setting invalid values.
     try {
         $typed_data->setValue(array('not a list' => 'one'));
         $this->fail('No exception has been thrown when setting an invalid value.');
     } catch (\Exception $e) {
         $this->pass('Exception thrown:' . $e->getMessage());
     }
     try {
         $typed_data->setValue('string');
         $this->fail('No exception has been thrown when setting an invalid value.');
     } catch (\Exception $e) {
         $this->pass('Exception thrown:' . $e->getMessage());
     }
 }