BlockFormControllerTest.php 2.93 KB
Newer Older
1 2 3 4
<?php

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

8
namespace Drupal\block\Tests;
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

use Drupal\block\BlockFormController;
use Drupal\Tests\UnitTestCase;

/**
 * Tests the block form controller.
 *
 * @see \Drupal\block\BlockFormController
 */
class BlockFormControllerTest extends UnitTestCase {

  public static function getInfo() {
    return array(
      'name' => 'Block form controller',
      'description' => 'Tests the block form controller.',
      'group' => 'Block',
    );
  }

  /**
   * Tests the unique machine name generator.
   *
   * @see \Drupal\block\BlockFormController::getUniqueMachineName()
   */
  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');

41
    $query = $this->getMock('Drupal\Core\Entity\Query\QueryInterface');
42 43 44 45 46 47 48 49
    $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')));

50 51 52
    $block_storage = $this->getMock('Drupal\Core\Config\Entity\ConfigStorageControllerInterface');
    $block_storage->expects($this->exactly(5))
      ->method('getQuery')
53 54
      ->will($this->returnValue($query));

55
    $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
56 57 58 59 60

    $entity_manager->expects($this->any())
      ->method('getStorageController')
      ->will($this->returnValue($block_storage));

61
    $language_manager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
62

63
    $config_factory = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface');
64

65
    $block_form_controller = new BlockFormController($entity_manager, $language_manager, $config_factory);
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

    // 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));
  }

}