本文整理汇总了PHP中Drupal\Core\TypedData\TypedDataManager类的典型用法代码示例。如果您正苦于以下问题:PHP TypedDataManager类的具体用法?PHP TypedDataManager怎么用?PHP TypedDataManager使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TypedDataManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setUp
/**
* {@inheritdoc}
*/
public function setUp()
{
parent::setUp();
$mock_data_definition = $this->getMock('Drupal\\Core\\TypedData\\DataDefinitionInterface');
$this->contextDefinition = $this->getMockBuilder('Drupal\\Core\\Plugin\\Context\\ContextDefinitionInterface')->setMethods(array('getDefaultValue', 'getDataDefinition'))->getMockForAbstractClass();
$this->contextDefinition->expects($this->once())->method('getDefaultValue')->willReturn('test');
$this->contextDefinition->expects($this->once())->method('getDataDefinition')->willReturn($mock_data_definition);
$this->typedData = $this->getMock('Drupal\\Core\\TypedData\\TypedDataInterface');
$this->typedDataManager = $this->getMockBuilder('Drupal\\Core\\TypedData\\TypedDataManager')->disableOriginalConstructor()->setMethods(array('create'))->getMock();
$this->typedDataManager->expects($this->once())->method('create')->with($mock_data_definition, 'test')->willReturn($this->typedData);
}
示例2: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->typedDataManager = $this->prophesize(TypedDataManager::class);
$container = new ContainerBuilder();
$container->set('string_translation', $this->getStringTranslationStub());
$container->set('typed_data_manager', $this->typedDataManager->reveal());
\Drupal::setContainer($container);
$this->page = $this->prophesize(PageInterface::class);
$this->event = new PageManagerContextEvent($this->page->reveal());
}
示例3: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->typedDataManager = $this->prophesize(TypedDataManager::class);
$container = new ContainerBuilder();
$container->set('string_translation', $this->getStringTranslationStub());
$container->set('typed_data_manager', $this->typedDataManager->reveal());
\Drupal::setContainer($container);
$this->executable = $this->getMockBuilder(PageExecutable::class)->disableOriginalConstructor()->setMethods(['getPage', 'addContext'])->getMock();
$this->event = new PageManagerContextEvent($this->executable);
}
示例4: filterPluginDefinitionsByContexts
/**
* {@inheritdoc}
*/
public function filterPluginDefinitionsByContexts(array $contexts, array $definitions)
{
return array_filter($definitions, function ($plugin_definition) use($contexts) {
// If this plugin doesn't need any context, it is available to use.
if (!isset($plugin_definition['context'])) {
return TRUE;
}
// Build an array of requirements out of the contexts specified by the
// plugin definition.
$requirements = array();
/** @var $plugin_context \Drupal\Core\Plugin\Context\ContextDefinitionInterface */
foreach ($plugin_definition['context'] as $context_id => $plugin_context) {
$definition = $this->typedDataManager->getDefinition($plugin_context->getDataType());
$definition['type'] = $plugin_context->getDataType();
// If the plugin specifies additional constraints, add them to the
// constraints defined by the plugin type.
if ($plugin_constraints = $plugin_context->getConstraints()) {
// Ensure the array exists before adding in constraints.
if (!isset($definition['constraints'])) {
$definition['constraints'] = array();
}
$definition['constraints'] += $plugin_constraints;
}
// Assume the requirement is required if unspecified.
if (!isset($definition['required'])) {
$definition['required'] = TRUE;
}
// @todo Use context definition objects after
// https://drupal.org/node/2281635.
$requirements[$context_id] = new DataDefinition($definition);
}
// Check the set of contexts against the requirements.
return $this->checkRequirements($contexts, $requirements);
});
}
示例5: testValidation
/**
* Tests the ComplexData validation constraint validator.
*
* For testing a map including a constraint on one of its keys is defined.
*/
public function testValidation()
{
// Create a definition that specifies some ComplexData constraint.
$definition = MapDataDefinition::create()->setPropertyDefinition('key', DataDefinition::create('integer'))->addConstraint('ComplexData', array('key' => array('AllowedValues' => array(1, 2, 3))));
// Test the validation.
$typed_data = $this->typedData->create($definition, array('key' => 1));
$violations = $typed_data->validate();
$this->assertEqual($violations->count(), 0, 'Validation passed for correct value.');
// Test the validation when an invalid value is passed.
$typed_data = $this->typedData->create($definition, array('key' => 4));
$violations = $typed_data->validate();
$this->assertEqual($violations->count(), 1, 'Validation failed for incorrect value.');
// Make sure the information provided by a violation is correct.
$violation = $violations[0];
$this->assertEqual($violation->getMessage(), t('The value you selected is not a valid choice.'), 'The message for invalid value is correct.');
$this->assertEqual($violation->getRoot(), $typed_data, 'Violation root is correct.');
$this->assertEqual($violation->getInvalidValue(), 4, 'The invalid value is set correctly in the violation.');
// Test using the constraint with a map without the specified key. This
// should be ignored as long as there is no NotNull or NotBlank constraint.
$typed_data = $this->typedData->create($definition, array('foo' => 'bar'));
$violations = $typed_data->validate();
$this->assertEqual($violations->count(), 0, 'Constraint on non-existing key is ignored.');
$definition = MapDataDefinition::create()->setPropertyDefinition('key', DataDefinition::create('integer'))->addConstraint('ComplexData', array('key' => array('NotNull' => array())));
$typed_data = $this->typedData->create($definition, array('foo' => 'bar'));
$violations = $typed_data->validate();
$this->assertEqual($violations->count(), 1, 'Key is required.');
}
示例6: testDataReferences
/**
* Tests deriving metadata from data references.
*/
public function testDataReferences()
{
$language_reference_definition = DataReferenceDefinition::create('language');
$this->assertTrue($language_reference_definition instanceof DataReferenceDefinitionInterface);
// Test retrieving metadata about the referenced data.
$this->assertEqual($language_reference_definition->getTargetDefinition()->getDataType(), 'language');
// Test using the definition factory.
$language_reference_definition2 = $this->typedDataManager->createDataDefinition('language_reference');
$this->assertTrue($language_reference_definition2 instanceof DataReferenceDefinitionInterface);
$this->assertEqual($language_reference_definition, $language_reference_definition2);
}
示例7: testGetContextValuesContext
/**
* @covers ::getContextValues
*/
public function testGetContextValuesContext()
{
$data_definition = DataDefinition::createFromDataType('integer');
$typed_data = IntegerData::createInstance($data_definition);
$this->typedDataManager->createDataDefinition('integer')->willReturn($data_definition);
$this->typedDataManager->getDefaultConstraints($data_definition)->willReturn([]);
$this->typedDataManager->create($data_definition, 5)->willReturn($typed_data);
$input = ['foo' => ['label' => 'Foo', 'type' => 'integer', 'value' => 5]];
$expected = new Context(new ContextDefinition('integer', 'Foo'), 5);
$actual = $this->staticContext->getContextValues($input)['foo'];
$this->assertEquals($expected, $actual);
}
示例8: buildForm
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state)
{
$cached_values = $form_state->getTemporaryValue('wizard');
$this->machine_name = $cached_values['id'];
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
$options = [];
foreach ($this->typedDataManager->getDefinitions() as $plugin_id => $definition) {
$options[$plugin_id] = (string) $definition['label'];
}
$form['items'] = array('#type' => 'markup', '#prefix' => '<div id="configured-contexts">', '#suffix' => '</div>', '#theme' => 'table', '#header' => array($this->t('Information'), $this->t('Description'), $this->t('Operations')), '#rows' => $this->renderContexts($cached_values), '#empty' => t('No required contexts have been configured.'));
$form['contexts'] = ['#type' => 'select', '#options' => $options];
$form['add'] = ['#type' => 'submit', '#name' => 'add', '#value' => t('Add required context'), '#ajax' => ['callback' => [$this, 'add'], 'event' => 'click'], '#submit' => ['callback' => [$this, 'submitform']]];
return $form;
}
示例9: testClearCachedFieldDefinitions
/**
* Tests the clearCachedFieldDefinitions() method.
*
* @covers ::clearCachedFieldDefinitions()
*/
public function testClearCachedFieldDefinitions()
{
$this->setUpEntityManager();
$this->cache->expects($this->once())->method('deleteTags')->with(array('entity_field_info' => TRUE));
$this->typedDataManager->expects($this->once())->method('clearCachedDefinitions');
$this->entityManager->clearCachedFieldDefinitions();
}
示例10: getTypedData
/**
* 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;
}
示例11: testRequiredValidation
/**
* Tests required validation.
*
* @covers ::validate
* @covers ::isValidationRequired
* @covers ::setValidationRequired
* @covers ::save
* @covers ::preSave
*
* @expectedException \LogicException
* @expectedExceptionMessage Entity validation was skipped.
*/
public function testRequiredValidation()
{
$validator = $this->getMock('\\Symfony\\Component\\Validator\\ValidatorInterface');
/** @var \Symfony\Component\Validator\ConstraintViolationList|\PHPUnit_Framework_MockObject_MockObject $empty_violation_list */
$empty_violation_list = $this->getMockBuilder('\\Symfony\\Component\\Validator\\ConstraintViolationList')->setMethods(NULL)->getMock();
$validator->expects($this->at(0))->method('validate')->with($this->entity->getTypedData())->will($this->returnValue($empty_violation_list));
$this->typedDataManager->expects($this->any())->method('getValidator')->will($this->returnValue($validator));
/** @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject $storage */
$storage = $this->getMock('\\Drupal\\Core\\Entity\\EntityStorageInterface');
$storage->expects($this->any())->method('save')->willReturnCallback(function (ContentEntityInterface $entity) use($storage) {
$entity->preSave($storage);
});
$this->entityManager->expects($this->any())->method('getStorage')->with($this->entityTypeId)->will($this->returnValue($storage));
// Check that entities can be saved normally when validation is not
// required.
$this->assertFalse($this->entity->isValidationRequired());
$this->entity->save();
// Make validation required and check that if the entity is validated, it
// can be saved normally.
$this->entity->setValidationRequired(TRUE);
$this->assertTrue($this->entity->isValidationRequired());
$this->entity->validate();
$this->entity->save();
// Check that the "validated" status is reset after saving the entity and
// that trying to save a non-validated entity when validation is required
// results in an exception.
$this->assertTrue($this->entity->isValidationRequired());
$this->entity->save();
}
示例12: testValidation
/**
* Tests the AllowedValues validation constraint validator.
*
* For testing we define an integer with a set of allowed values.
*/
public function testValidation()
{
// Create a definition that specifies some AllowedValues.
$definition = DataDefinition::create('integer')->addConstraint('AllowedValues', array(1, 2, 3));
// Test the validation.
$typed_data = $this->typedData->create($definition, 1);
$violations = $typed_data->validate();
$this->assertEqual($violations->count(), 0, 'Validation passed for correct value.');
// Test the validation when an invalid value is passed.
$typed_data = $this->typedData->create($definition, 4);
$violations = $typed_data->validate();
$this->assertEqual($violations->count(), 1, 'Validation failed for incorrect value.');
// Make sure the information provided by a violation is correct.
$violation = $violations[0];
$this->assertEqual($violation->getMessage(), t('The value you selected is not a valid choice.'), 'The message for invalid value is correct.');
$this->assertEqual($violation->getRoot(), $typed_data, 'Violation root is correct.');
$this->assertEqual($violation->getInvalidValue(), 4, 'The invalid value is set correctly in the violation.');
}
示例13: setUpDefaultValue
/**
* Set up mocks for the getDefaultValue() method call.
*
* @param mixed $default_value
* The default value to assign to the mock context definition.
*/
protected function setUpDefaultValue($default_value = NULL)
{
$mock_data_definition = $this->getMock('Drupal\\Core\\TypedData\\DataDefinitionInterface');
$this->contextDefinition = $this->getMockBuilder('Drupal\\Core\\Plugin\\Context\\ContextDefinitionInterface')->setMethods(array('getDefaultValue', 'getDataDefinition'))->getMockForAbstractClass();
$this->contextDefinition->expects($this->once())->method('getDefaultValue')->willReturn($default_value);
$this->contextDefinition->expects($this->once())->method('getDataDefinition')->willReturn($mock_data_definition);
$this->typedData = $this->getMock('Drupal\\Core\\TypedData\\TypedDataInterface');
$this->typedDataManager->expects($this->once())->method('create')->with($mock_data_definition, $default_value)->willReturn($this->typedData);
}
示例14: assertValidation
/**
* Executes the BundleConstraintValidator test for a given bundle.
*
* @param string|array $bundle
* Bundle/bundles to use as constraint option.
*/
protected function assertValidation($bundle)
{
// Create a typed data definition with a Bundle constraint.
$definition = DataDefinition::create('entity_reference')->addConstraint('Bundle', $bundle);
// Test the validation.
$node = $this->container->get('entity.manager')->getStorage('node')->create(array('type' => 'foo'));
$typed_data = $this->typedData->create($definition, $node);
$violations = $typed_data->validate();
$this->assertEqual($violations->count(), 0, 'Validation passed for correct value.');
// Test the validation when an invalid value is passed.
$page_node = $this->container->get('entity.manager')->getStorage('node')->create(array('type' => 'baz'));
$typed_data = $this->typedData->create($definition, $page_node);
$violations = $typed_data->validate();
$this->assertEqual($violations->count(), 1, 'Validation failed for incorrect value.');
// Make sure the information provided by a violation is correct.
$violation = $violations[0];
$this->assertEqual($violation->getMessage(), t('The entity must be of bundle %bundle.', array('%bundle' => implode(', ', (array) $bundle))), 'The message for invalid value is correct.');
$this->assertEqual($violation->getRoot(), $typed_data, 'Violation root is correct.');
$this->assertEqual($violation->getInvalidValue(), $page_node, 'The invalid value is set correctly in the violation.');
}
示例15: testEntityReferences
/**
* Tests deriving metadata from entity references.
*/
public function testEntityReferences()
{
$reference_definition = DataReferenceDefinition::create('entity');
$this->assertTrue($reference_definition instanceof DataReferenceDefinitionInterface);
// Test retrieving metadata about the referenced data.
$this->assertEqual($reference_definition->getTargetDefinition()->getDataType(), 'entity');
$this->assertTrue($reference_definition->getTargetDefinition() instanceof EntityDataDefinitionInterface);
// Test that the definition factory creates the right definition object.
$reference_definition2 = $this->typedDataManager->createDataDefinition('entity_reference');
$this->assertTrue($reference_definition2 instanceof DataReferenceDefinitionInterface);
$this->assertEqual($reference_definition2, $reference_definition);
}