StringData example

public function provideTestValidate() {
    $data = [];
    $data[] = [new BooleanData(DataDefinition::create('boolean')), NULL, TRUE];

    $data[] = [new BooleanData(DataDefinition::create('boolean')), 1, TRUE];
    $data[] = [new BooleanData(DataDefinition::create('boolean')), 'test', FALSE];
    $data[] = [new FloatData(DataDefinition::create('float')), 1.5, TRUE];
    $data[] = [new FloatData(DataDefinition::create('float')), 'test', FALSE];
    $data[] = [new IntegerData(DataDefinition::create('integer')), 1, TRUE];
    $data[] = [new IntegerData(DataDefinition::create('integer')), 1.5, FALSE];
    $data[] = [new IntegerData(DataDefinition::create('integer')), 'test', FALSE];
    $data[] = [new StringData(DataDefinition::create('string')), 'test', TRUE];
    $data[] = [new StringData(DataDefinition::create('string'))new TranslatableMarkup('test'), TRUE];
    // It is odd that 1 is a valid string.     // $data[] = [$this->createMock('Drupal\Core\TypedData\Type\StringInterface'), 1, FALSE];     $data[] = [new StringData(DataDefinition::create('string'))[], FALSE];
    $data[] = [new Uri(DataDefinition::create('uri')), 'http://www.example.com', TRUE];
    $data[] = [new Uri(DataDefinition::create('uri')), 'https://www.example.com', TRUE];
    $data[] = [new Uri(DataDefinition::create('uri')), 'Invalid', FALSE];
    $data[] = [new Uri(DataDefinition::create('uri')), 'entity:node/1', TRUE];
    $data[] = [new Uri(DataDefinition::create('uri')), 'base:', TRUE];
    $data[] = [new Uri(DataDefinition::create('uri')), 'base:node', TRUE];
    $data[] = [new Uri(DataDefinition::create('uri')), 'internal:', TRUE];
    

  public function testSetContextValue() {
    $typed_data_manager = $this->prophesize(TypedDataManagerInterface::class);
    $container = new ContainerBuilder();
    $container->set('typed_data_manager', $typed_data_manager->reveal());
    \Drupal::setContainer($container);

    $this->plugin->getPluginDefinition()->addContextDefinition('foo', new ContextDefinition('string'));

    $this->assertFalse($this->plugin->setContextCalled);
    $this->plugin->setContextValue('foo', new StringData(new DataDefinition(), 'bar'));
    $this->assertTrue($this->plugin->setContextCalled);
  }

}

class TestContextAwarePluginDefinition extends PluginDefinition implements ContextAwarePluginDefinitionInterface {

  use ContextAwarePluginDefinitionTrait;

}


class ContextTypedDataTest extends KernelTestBase {

  /** * Tests that contexts can be serialized. */
  public function testSerialize() {
    $definition = new ContextDefinition('any');
    $data_definition = DataDefinition::create('string');
    $typed_data = new StringData($data_definition);
    $typed_data->setValue('example string');
    $context = new Context($definition$typed_data);
    // getContextValue() will cause the context to reference the typed data     // manager service.     $value = $context->getContextValue();
    $context = serialize($context);
    $context = unserialize($context);
    $this->assertSame($value$context->getContextValue());
  }

  /** * Tests that getting a context value does not throw fatal errors. * * This test ensures that the typed data manager is set correctly on the * Context class. * * @covers ::getContextValue */
public function testNormalize($primitive_data$expected) {
    $this->assertSame($expected$this->normalizer->normalize($primitive_data));
  }

  /** * Data provider for testNormalize(). */
  public function dataProviderPrimitiveData() {
    $data = [];

    $definition = DataDefinition::createFromDataType('string');
    $string = new StringData($definition, 'string');
    $string->setValue('test');

    $data['string'] = [$string, 'test'];

    $definition = DataDefinition::createFromDataType('string');
    $string = new StringData($definition, 'string');
    $string->setValue(NULL);

    $data['string-null'] = [$string, NULL];

    $definition = DataDefinition::createFromDataType('integer');
    
Home | Imprint | This part of the site doesn't use cookies.