You are here

public function OgContextTest::testGetRuntimeContexts in Organic groups 8

Tests retrieving group context during runtime.

@covers ::getRuntimeContexts

@dataProvider getRuntimeContextsProvider

Parameters

array $unqualified_context_ids: The requested context IDs that are passed to ::getRuntimeContexts(). The context provider must only return contexts for those IDs.

array $group_resolvers: An array of group resolver plugins that are used in the test case, ordered by priority. Each element is an array of plugin behaviors, with the following keys:

  • candidates: an array of group context candidates that the plugin adds to the collection of resolved groups.
  • stop_propagation: whether or not the plugin declares that the search for further groups is over. Defaults to FALSE.

string|false $expected_context: The ID of the entity that is expected to be provided as group context, or FALSE if no context should be returned.

string[] $expected_cache_contexts: An array of cache context IDs which are expected to be returned as cacheability metadata.

File

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

Class

OgContextTest
Tests the OgContext context provider.

Namespace

Drupal\Tests\og\Unit

Code

public function testGetRuntimeContexts(array $unqualified_context_ids, array $group_resolvers, $expected_context, array $expected_cache_contexts) {

  // Make the test entities available in the local scope so we can use it in
  // anonymous functions.
  $entities = $this->entities;

  // Translate the ID of the expected context to the actual test entity.
  $expected_context_entity = !empty($expected_context) ? $entities[$expected_context] : NULL;

  // Return the list of OgGroupResolver plugins that are supplied in the test
  // case. These are expected to be retrieved from config.
  $group_resolvers_config = $this
    ->prophesize(ImmutableConfig::class);
  $group_resolvers_config
    ->get('group_resolvers')
    ->willReturn(array_keys($group_resolvers));
  $this->configFactory
    ->get('og.settings')
    ->willReturn($group_resolvers_config);

  // Mock the OgGroupResolver plugins.
  foreach ($group_resolvers as $id => $group_resolver) {
    $plugin = $this
      ->prophesize(OgGroupResolverInterface::class);
    $plugin
      ->isPropagationStopped()
      ->willReturn(!empty($group_resolver['stop_propagation']));
    $plugin
      ->resolve(Argument::type(OgResolvedGroupCollectionInterface::class))
      ->will(function ($args) use ($entities, $group_resolver) {

      /** @var \Drupal\og\OgResolvedGroupCollectionInterface $collection */
      $collection = $args[0];
      foreach ($group_resolver['candidates'] as $candidate) {
        $entity = $entities[$candidate['entity']];
        $cache_contexts = $candidate['cache_contexts'];
        $collection
          ->addGroup($entity, $cache_contexts);
      }
    });
    $this->pluginManager
      ->createInstance($id)
      ->willReturn($plugin);
  }

  // It is expected that the correct resolved group will be set on the Context
  // object.
  if ($expected_context !== FALSE) {
    $this->typedDataManager
      ->create($this->dataDefinition, $entities[$expected_context])
      ->shouldBeCalled()
      ->willReturn($this->typedData
      ->reveal());

    // If the group is correctly set as on the Context object, then it is
    // reasonable to expect that it will be returned as a typed data object
    // that will give back the group when it is asked for it.
    $this->typedData
      ->getValue()
      ->willReturn($expected_context_entity);
  }
  $og_context = new OgContext($this->pluginManager
    ->reveal(), $this->configFactory
    ->reveal());
  $result = $og_context
    ->getRuntimeContexts($unqualified_context_ids);

  // If no group context is expected to be returned, the result should be an
  // empty array.
  if ($expected_context === FALSE) {
    $this
      ->assertEquals([], $result);
  }
  else {

    // Check that the 'og' context is populated.
    $this
      ->assertNotEmpty($result['og']);

    // Check that the correct group is set as the context value.
    $this
      ->assertEquals($expected_context_entity, $result['og']
      ->getContextData()
      ->getValue());

    // Check that the correct cache context IDs are set as cacheability
    // metadata.
    $this
      ->assertEquals($expected_cache_contexts, array_values($result['og']
      ->getCacheContexts()));
  }
}