/**
  * Converts a ContextInterface object into a TypedDataInterface object.
  *
  * This method respects values on the Context object and will ensure they're
  * maintained on the returned typed data object.
  *
  * @param \Drupal\Core\Plugin\Context\ContextInterface $context
  *   The context object to convert to a typed data object.
  *
  * @return \Drupal\Core\TypedData\TypedDataInterface
  *   A typed data representation of the supplied context object.
  */
 public function getTypedDataFromContext(ContextInterface $context)
 {
     $data_type = $context->getContextDefinition()->getDataType();
     $definition = $this->manager->createDataDefinition($data_type);
     $typed_data = $context->hasContextValue() ? $this->manager->create($definition, $context->getContextValue()) : ($typed_data = $this->manager->create($definition));
     return $typed_data;
 }
예제 #2
0
 /**
  * @cover \Drupal\rules\Plugin\TypedDataFilter\FormatDateFilter
  */
 public function testFormatDateFilter()
 {
     $filter = $this->dataFilterManager->createInstance('format_date');
     $data = $this->typedDataManager->create(DataDefinition::create('timestamp'), 3700);
     $this->assertTrue($filter->canFilter($data->getDataDefinition()));
     $this->assertFalse($filter->canFilter(DataDefinition::create('any')));
     $this->assertEquals('string', $filter->filtersTo($data->getDataDefinition(), [])->getDataType());
     $fails = $filter->validateArguments($data->getDataDefinition(), []);
     $this->assertEquals(0, count($fails));
     $fails = $filter->validateArguments($data->getDataDefinition(), ['medium']);
     $this->assertEquals(0, count($fails));
     $fails = $filter->validateArguments($data->getDataDefinition(), ['invalid-format']);
     $this->assertEquals(1, count($fails));
     $fails = $filter->validateArguments($data->getDataDefinition(), ['custom']);
     $this->assertEquals(1, count($fails));
     $fails = $filter->validateArguments($data->getDataDefinition(), ['custom', 'Y']);
     $this->assertEquals(0, count($fails));
     /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
     $date_formatter = $this->container->get('date.formatter');
     $this->assertEquals($date_formatter->format(3700), $filter->filter($data->getDataDefinition(), $data->getValue(), []));
     $this->assertEquals($date_formatter->format(3700, 'short'), $filter->filter($data->getDataDefinition(), $data->getValue(), ['short']));
     $this->assertEquals('1970', $filter->filter($data->getDataDefinition(), $data->getValue(), ['custom', 'Y']));
     // Verify the filter works with non-timestamp data as well.
     $data = $this->typedDataManager->create(DataDefinition::create('datetime_iso8601'), "1970-01-01T10:10:10+00:00");
     $this->assertTrue($filter->canFilter($data->getDataDefinition()));
     $this->assertEquals('string', $filter->filtersTo($data->getDataDefinition(), [])->getDataType());
     $this->assertEquals('1970', $filter->filter($data->getDataDefinition(), $data->getValue(), ['custom', 'Y']));
     // Test cache dependencies of date format config entities are added in.
     $metadata = new BubbleableMetadata();
     $filter->filter($data->getDataDefinition(), $data->getValue(), ['short'], $metadata);
     $this->assertEquals(DateFormat::load('short')->getCacheTags(), $metadata->getCacheTags());
     $metadata = new BubbleableMetadata();
     $filter->filter($data->getDataDefinition(), $data->getValue(), ['custom', 'Y'], $metadata);
     $this->assertEquals([], $metadata->getCacheTags());
 }
 /**
  * Returns a typed data object.
  *
  * This helper for quick creation of typed data objects.
  *
  * @param string $data_type
  *   The data type to create an object for.
  * @param mixed[] $value
  *   The value to set.
  *
  * @return \Drupal\Core\TypedData\TypedDataInterface
  *   The created object.
  */
 protected function getTypedData($data_type, $value)
 {
     $definition = $this->typedDataManager->createDataDefinition($data_type);
     $data = $this->typedDataManager->create($definition);
     $data->setValue($value);
     return $data;
 }
 /**
  * @cover replacePlaceHolders
  */
 public function testApplyingFilters()
 {
     $this->node->field_integer = [1, 2, NULL];
     $this->node->title->value = NULL;
     $result = $this->placeholderResolver->replacePlaceHolders("test {{node.field_integer.2.value|default('0')}}", ['node' => $this->node->getTypedData()]);
     $this->assertEquals('test 0', $result);
     $result = $this->placeholderResolver->replacePlaceHolders("test {{node.title.value|default('tEsT')|lower}}", ['node' => $this->node->getTypedData()]);
     $this->assertEquals('test test', $result);
     // Test filter expressions with whitespaces.
     $result = $this->placeholderResolver->replacePlaceHolders("test {{ node.title.value | default('tEsT') | lower }}", ['node' => $this->node->getTypedData()]);
     $this->assertEquals('test test', $result);
     // Test a filter expression on data without accessing a property.
     $text = 'test {{string | lower}}';
     $result = $this->placeholderResolver->replacePlaceHolders($text, ['string' => $this->typedDataManager->create(DataDefinition::create('string'), 'Replacement')]);
     $this->assertEquals('test replacement', $result);
     $text = "The year is {{ date | format_date('custom', 'Y') }}.";
     $result = $this->placeholderResolver->replacePlaceHolders($text, ['date' => $this->typedDataManager->create(DataDefinition::create('timestamp'), '3600')]);
     $this->assertEquals('The year is 1970.', $result);
 }
예제 #5
0
 /**
  * @cover fetchDataByPropertyPath
  * @expectedException \Drupal\Core\TypedData\Exception\MissingDataException
  * @expectedExceptionMessageRegExp #Unable to apply data selector 'field_integer.0.value' at 'field_integer':.*#
  */
 public function testFetchingFromEmptyData()
 {
     $data_empty = $this->typedDataManager->create(EntityDataDefinition::create('node'));
     // This should trigger an exception.
     $this->dataFetcher->fetchDataByPropertyPath($data_empty, 'field_integer.0.value')->getValue();
 }