Verified Commit 5c503f4a authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #2825078 by Mile23, dawehner, Wim Leers, Gábor Hojtsy: Deprecate...

Issue #2825078 by Mile23, dawehner, Wim Leers, Gábor Hojtsy: Deprecate UnitTestCase dependency on block module
parent 63a95d58
Loading
Loading
Loading
Loading
+28 −0
Changes for core/modules/block/tests/src/Unit/BlockFormTest.php: 28 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -3,6 +3,8 @@
namespace Drupal\Tests\block\Unit;

use Drupal\block\BlockForm;
use Drupal\block\Entity\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\PluginFormFactoryInterface;
use Drupal\Tests\UnitTestCase;

@@ -82,6 +84,32 @@ protected function setUp() {
    $this->pluginFormFactory = $this->prophesize(PluginFormFactoryInterface::class);
  }

  /**
   * Mocks a block with a block plugin.
   *
   * @param string $machine_name
   *   The machine name of the block plugin.
   *
   * @return \Drupal\block\BlockInterface|\PHPUnit_Framework_MockObject_MockObject
   *   The mocked block.
   */
  protected function getBlockMockWithMachineName($machine_name) {
    $plugin = $this->getMockBuilder(BlockBase::class)
      ->disableOriginalConstructor()
      ->getMock();
    $plugin->expects($this->any())
      ->method('getMachineNameSuggestion')
      ->will($this->returnValue($machine_name));

    $block = $this->getMockBuilder(Block::class)
      ->disableOriginalConstructor()
      ->getMock();
    $block->expects($this->any())
      ->method('getPlugin')
      ->will($this->returnValue($plugin));
    return $block;
  }

  /**
   * Tests the unique machine name generator.
   *
+7 −0
Changes for core/tests/Drupal/Tests/UnitTestCase.php: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -180,6 +180,12 @@ public function getConfigStorageStub(array $configs) {
   *
   * @return \Drupal\block\BlockInterface|\PHPUnit_Framework_MockObject_MockObject
   *   The mocked block.
   *
   * @deprecated in Drupal 8.5.x, will be removed before Drupal 9.0.0. Unit test
   *   base classes should not have dependencies on extensions. Set up mocks in
   *   individual tests.
   *
   * @see https://www.drupal.org/node/2896072
   */
  protected function getBlockMockWithMachineName($machine_name) {
    $plugin = $this->getMockBuilder('Drupal\Core\Block\BlockBase')
@@ -195,6 +201,7 @@ protected function getBlockMockWithMachineName($machine_name) {
    $block->expects($this->any())
      ->method('getPlugin')
      ->will($this->returnValue($plugin));
    @trigger_error(__METHOD__ . ' is deprecated in Drupal 8.5.x, will be removed before Drupal 9.0.0. Unit test base classes should not have dependencies on extensions. Set up mocks in individual tests.', E_USER_DEPRECATED);
    return $block;
  }

+22 −0
Changes for core/tests/Drupal/Tests/UnitTestCaseDeprecationTest.php: 22 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests;

/**
 * @coversDefaultClass \Drupal\Tests\UnitTestCase
 *
 * @group Test
 * @group legacy
 */
class UnitTestCaseDeprecationTest extends UnitTestCase {

  /**
   * @covers ::getBlockMockWithMachineName
   * @expectedDeprecation Drupal\Tests\UnitTestCase::getBlockMockWithMachineName is deprecated in Drupal 8.5.x, will be removed before Drupal 9.0.0. Unit test base classes should not have dependencies on extensions. Set up mocks in individual tests.
   */
  public function testDeprecatedGetBlockMockWithMachineName() {
    $block_mock = $this->getBlockMockWithMachineName('test_name');
    $this->assertEquals('test_name', $block_mock->getPlugin()->getMachineNameSuggestion());
  }

}