You are here

protected function OgContextTest::setUp in Organic groups 8

Overrides UnitTestCase::setUp

File

tests/src/Unit/OgContextTest.php, line 74

Class

OgContextTest
Tests the OgContext context provider.

Namespace

Drupal\Tests\og\Unit

Code

protected function setUp() : void {
  parent::setUp();
  $this->pluginManager = $this
    ->prophesize(PluginManagerInterface::class);
  $this->configFactory = $this
    ->prophesize(ConfigFactoryInterface::class);
  $this->typedDataManager = $this
    ->prophesize(TypedDataManagerInterface::class);
  $this->dataDefinition = $this
    ->prophesize(EntityDataDefinition::class);
  $this->typedData = $this
    ->prophesize(TypedDataInterface::class);

  // When a ContextProvider creates a new Context object and sets the context
  // value on it, the Context object will use the typed data manager service
  // to get a DataDefinition object, which is an abstracted representation of
  // the data. Mock the method calls that are used during creation of this
  // DataDefinition object. In the case of OgContext this will return an
  // entity.
  $this->dataDefinition
    ->setLabel(Argument::any())
    ->willReturn($this->dataDefinition);
  $this->dataDefinition
    ->setDescription(Argument::any())
    ->willReturn($this->dataDefinition);
  $this->dataDefinition
    ->setRequired(Argument::any())
    ->willReturn($this->dataDefinition);
  $this->dataDefinition
    ->getConstraints()
    ->willReturn([]);
  $this->dataDefinition
    ->setConstraints(Argument::any())
    ->willReturn($this->dataDefinition);
  $this->typedDataManager
    ->createDataDefinition('entity')
    ->willReturn($this->dataDefinition
    ->reveal());

  // Mock the string translation service on the container, this will cover
  // calls to $this->t().
  $container = new Container();
  $container
    ->set('string_translation', $this
    ->getStringTranslationStub());

  // Put the mocked typed data manager service on the container. This is used
  // to set the context value.
  $container
    ->set('typed_data_manager', $this->typedDataManager
    ->reveal());
  \Drupal::setContainer($container);

  // Create 2 mock entities each for node, block_content and entity_test
  // entities.
  foreach ([
    'node',
    'block_content',
    'entity_test',
  ] as $type) {
    for ($i = 0; $i < 2; $i++) {
      $id = "{$type}-{$i}";
      $entity = $this
        ->prophesize(ContentEntityInterface::class);
      $entity
        ->id()
        ->willReturn($id);
      $entity
        ->getEntityTypeId()
        ->willReturn($type);
      $entity
        ->getCacheContexts()
        ->willReturn([]);
      $entity
        ->getCacheTags()
        ->willReturn([]);
      $entity
        ->getCacheMaxAge()
        ->willReturn(0);
      $this->entities[$id] = $entity
        ->reveal();
    }
  }
}