BlockConfigEntityUnitTest.php 3.16 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Tests\block\Unit\BlockConfigEntityUnitTest.
6 7
 */

8
namespace Drupal\Tests\block\Unit;
9 10

use Drupal\Core\DependencyInjection\ContainerBuilder;
11
use Drupal\Tests\Core\Plugin\Fixtures\TestConfigurablePlugin;
12 13 14 15
use Drupal\Tests\UnitTestCase;

/**
 * @coversDefaultClass \Drupal\block\Entity\Block
16
 * @group block
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
 */
class BlockConfigEntityUnitTest extends UnitTestCase {

  /**
   * The entity type used for testing.
   *
   * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityType;

  /**
   * The entity manager used for testing.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityManager;

  /**
   * The ID of the type of the entity under test.
   *
   * @var string
   */
  protected $entityTypeId;

  /**
   * The UUID generator used for testing.
   *
   * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $uuid;

  /**
   * {@inheritdoc}
   */
51
  protected function setUp() {
52
    $this->entityTypeId = $this->randomMachineName();
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

    $this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
    $this->entityType->expects($this->any())
      ->method('getProvider')
      ->will($this->returnValue('block'));

    $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
    $this->entityManager->expects($this->any())
      ->method('getDefinition')
      ->with($this->entityTypeId)
      ->will($this->returnValue($this->entityType));

    $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');

    $container = new ContainerBuilder();
    $container->set('entity.manager', $this->entityManager);
    $container->set('uuid', $this->uuid);
    \Drupal::setContainer($container);
  }

  /**
   * @covers ::calculateDependencies
   */
  public function testCalculateDependencies() {
    $values = array('theme' => 'stark');
78
    // Mock the entity under test so that we can mock getPluginBags().
79 80
    $entity = $this->getMockBuilder('\Drupal\block\Entity\Block')
      ->setConstructorArgs(array($values, $this->entityTypeId))
81
      ->setMethods(array('getPluginBags'))
82 83
      ->getMock();
    // Create a configurable plugin that would add a dependency.
84
    $instance_id = $this->randomMachineName();
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    $instance = new TestConfigurablePlugin(array(), $instance_id, array('provider' => 'test'));

    // Create a plugin bag to contain the instance.
    $plugin_bag = $this->getMockBuilder('\Drupal\Core\Plugin\DefaultPluginBag')
      ->disableOriginalConstructor()
      ->setMethods(array('get'))
      ->getMock();
    $plugin_bag->expects($this->atLeastOnce())
      ->method('get')
      ->with($instance_id)
      ->will($this->returnValue($instance));
    $plugin_bag->addInstanceId($instance_id);

    // Return the mocked plugin bag.
    $entity->expects($this->once())
100 101
      ->method('getPluginBags')
      ->will($this->returnValue(array($plugin_bag)));
102 103 104 105 106 107 108

    $dependencies = $entity->calculateDependencies();
    $this->assertContains('test', $dependencies['module']);
    $this->assertContains('stark', $dependencies['theme']);
  }

}