View source  
  <?php
namespace Drupal\Tests\Core\Plugin\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\TypedData\TypedDataManagerInterface;
use Drupal\Tests\UnitTestCase;
class ContextDefinitionTest extends UnitTestCase {
  
  public function providerGetDataDefinition() {
    return [
      [
        TRUE,
      ],
      [
        FALSE,
      ],
    ];
  }
  
  public function testGetDataDefinition($is_multiple) {
    $data_type = 'valid';
    $mock_data_definition = $this
      ->getMockBuilder('\\Drupal\\Core\\TypedData\\ListDataDefinitionInterface')
      ->onlyMethods([
      'getConstraints',
    ])
      ->addMethods([
      'setLabel',
      'setDescription',
      'setRequired',
      'setConstraints',
    ])
      ->getMockForAbstractClass();
    $mock_data_definition
      ->expects($this
      ->once())
      ->method('setLabel')
      ->willReturnSelf();
    $mock_data_definition
      ->expects($this
      ->once())
      ->method('setDescription')
      ->willReturnSelf();
    $mock_data_definition
      ->expects($this
      ->once())
      ->method('setRequired')
      ->willReturnSelf();
    $mock_data_definition
      ->expects($this
      ->once())
      ->method('getConstraints')
      ->willReturn([]);
    $mock_data_definition
      ->expects($this
      ->once())
      ->method('setConstraints')
      ->willReturn(NULL);
    
    $create_definition_method = 'createDataDefinition';
    if ($is_multiple) {
      $create_definition_method = 'createListDataDefinition';
    }
    $mock_data_manager = $this
      ->createMock(TypedDataManagerInterface::class);
    
    $mock_data_manager
      ->expects($this
      ->once())
      ->method($create_definition_method)
      ->willReturnMap([
      [
        'not_valid',
        NULL,
      ],
      [
        'valid',
        $mock_data_definition,
      ],
    ]);
    
    $mock_context_definition = $this
      ->getMockBuilder('Drupal\\Core\\Plugin\\Context\\ContextDefinition')
      ->disableOriginalConstructor()
      ->onlyMethods([
      'isMultiple',
      'getTypedDataManager',
      'getDataType',
      'getLabel',
      'getDescription',
      'isRequired',
      'getConstraints',
      'setConstraints',
    ])
      ->getMock();
    $mock_context_definition
      ->expects($this
      ->once())
      ->method('isMultiple')
      ->willReturn($is_multiple);
    $mock_context_definition
      ->expects($this
      ->once())
      ->method('getTypedDataManager')
      ->willReturn($mock_data_manager);
    $mock_context_definition
      ->expects($this
      ->once())
      ->method('getDataType')
      ->willReturn($data_type);
    $mock_context_definition
      ->expects($this
      ->once())
      ->method('getConstraints')
      ->willReturn([]);
    $this
      ->assertSame($mock_data_definition, $mock_context_definition
      ->getDataDefinition());
  }
  
  public function testGetDataDefinitionInvalidType($is_multiple) {
    
    $data_type = 'not_valid';
    $mock_data_definition = $this
      ->getMockBuilder('\\Drupal\\Core\\TypedData\\ListDataDefinitionInterface')
      ->getMockForAbstractClass();
    
    $create_definition_method = 'createDataDefinition';
    if ($is_multiple) {
      $create_definition_method = 'createListDataDefinition';
    }
    $mock_data_manager = $this
      ->createMock(TypedDataManagerInterface::class);
    
    $mock_data_manager
      ->expects($this
      ->once())
      ->method($create_definition_method)
      ->willReturnMap([
      [
        'not_valid',
        NULL,
      ],
      [
        'valid',
        $mock_data_definition,
      ],
    ]);
    
    $mock_context_definition = $this
      ->getMockBuilder('Drupal\\Core\\Plugin\\Context\\ContextDefinition')
      ->disableOriginalConstructor()
      ->onlyMethods([
      'isMultiple',
      'getTypedDataManager',
      'getDataType',
    ])
      ->getMock();
    $mock_context_definition
      ->expects($this
      ->once())
      ->method('isMultiple')
      ->willReturn($is_multiple);
    $mock_context_definition
      ->expects($this
      ->once())
      ->method('getTypedDataManager')
      ->willReturn($mock_data_manager);
    $mock_context_definition
      ->method('getDataType')
      ->willReturn($data_type);
    $this
      ->expectException(\Exception::class);
    $mock_context_definition
      ->getDataDefinition();
  }
  
  public function providerGetConstraint() {
    return [
      [
        NULL,
        [],
        'nonexistent_constraint_name',
      ],
      [
        'not_null',
        [
          'constraint_name' => 'not_null',
        ],
        'constraint_name',
      ],
    ];
  }
  
  public function testGetConstraint($expected, $constraint_array, $constraint) {
    $mock_context_definition = $this
      ->getMockBuilder('Drupal\\Core\\Plugin\\Context\\ContextDefinition')
      ->disableOriginalConstructor()
      ->onlyMethods([
      'getConstraints',
    ])
      ->getMock();
    $mock_context_definition
      ->expects($this
      ->once())
      ->method('getConstraints')
      ->willReturn($constraint_array);
    $this
      ->assertEquals($expected, $mock_context_definition
      ->getConstraint($constraint));
  }
  
  public function testDefaultValue() {
    $context_definition = new ContextDefinition();
    $this
      ->assertNull($context_definition
      ->getDefaultValue());
    $context_definition
      ->setDefaultValue('test');
    $this
      ->assertSame('test', $context_definition
      ->getDefaultValue());
  }
}