BlockFormTest.php 3.76 KB
Newer Older
1 2 3 4
<?php

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

8
namespace Drupal\Tests\block\Unit;
9

10
use Drupal\block\BlockForm;
11 12 13
use Drupal\Tests\UnitTestCase;

/**
14 15
 * @coversDefaultClass \Drupal\block\BlockForm
 * @group block
16
 */
17
class BlockFormTest extends UnitTestCase {
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  /**
   * The condition plugin manager.
   *
   * @var \Drupal\Core\Executable\ExecutableManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $conditionManager;

  /**
   * The block storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $storage;

  /**
   * The event dispatcher service.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $dispatcher;

  /**
   * The language manager service.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $language;

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

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->conditionManager = $this->getMock('Drupal\Core\Executable\ExecutableManagerInterface');
    $this->language = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
    $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');

    $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
    $this->storage = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityStorageInterface');
    $this->entityManager->expects($this->any())
      ->method('getStorage')
      ->will($this->returnValue($this->storage));

  }

72 73 74
  /**
   * Tests the unique machine name generator.
   *
75
   * @see \Drupal\block\BlockForm::getUniqueMachineName()
76 77 78 79 80 81 82 83 84
   */
  public function testGetUniqueMachineName() {
    $blocks = array();

    $blocks['test'] = $this->getBlockMockWithMachineName('test');
    $blocks['other_test'] = $this->getBlockMockWithMachineName('other_test');
    $blocks['other_test_1'] = $this->getBlockMockWithMachineName('other_test');
    $blocks['other_test_2'] = $this->getBlockMockWithMachineName('other_test');

85
    $query = $this->getMock('Drupal\Core\Entity\Query\QueryInterface');
86 87 88 89 90 91 92 93
    $query->expects($this->exactly(5))
      ->method('condition')
      ->will($this->returnValue($query));

    $query->expects($this->exactly(5))
      ->method('execute')
      ->will($this->returnValue(array('test', 'other_test', 'other_test_1', 'other_test_2')));

94
    $this->storage->expects($this->exactly(5))
95
      ->method('getQuery')
96 97
      ->will($this->returnValue($query));

98
    $block_form_controller = new BlockForm($this->entityManager, $this->conditionManager, $this->dispatcher, $this->language);
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

    // Ensure that the block with just one other instance gets the next available
    // name suggestion.
    $this->assertEquals('test_2', $block_form_controller->getUniqueMachineName($blocks['test']));

    // Ensure that the block with already three instances (_0, _1, _2) gets the
    // 4th available name.
    $this->assertEquals('other_test_3', $block_form_controller->getUniqueMachineName($blocks['other_test']));
    $this->assertEquals('other_test_3', $block_form_controller->getUniqueMachineName($blocks['other_test_1']));
    $this->assertEquals('other_test_3', $block_form_controller->getUniqueMachineName($blocks['other_test_2']));

    // Ensure that a block without an instance yet gets the suggestion as
    // unique machine name.
    $last_block = $this->getBlockMockWithMachineName('last_test');
    $this->assertEquals('last_test', $block_form_controller->getUniqueMachineName($last_block));
  }

}