You are here

abstract class ConfigUpdateUnitTestBase in Configuration Update Manager 8

Base class for unit testing in Config Update Manager.

This class provides some mock classes for unit testing.

Hierarchy

Expanded class hierarchy of ConfigUpdateUnitTestBase

File

tests/src/Unit/ConfigUpdateUnitTestBase.php, line 14

Namespace

Drupal\Tests\config_update\Unit
View source
abstract class ConfigUpdateUnitTestBase extends UnitTestCase {

  /**
   * The mocked entity definition information.
   *
   * They are not sorted, to test that the methods sort them. Also there are a
   * couple with prefixes that are subsets of each other.
   *
   * @var string[]
   *
   * @see ConfigUpdateUnitTestBase::getEntityManagerMock().
   */
  protected $entityDefinitionInformation = [
    [
      'prefix' => 'foo.bar',
      'type' => 'foo',
    ],
    [
      'prefix' => 'foo.barbaz',
      'type' => 'bar',
    ],
    [
      'prefix' => 'baz.foo',
      'type' => 'baz',
    ],
  ];

  /**
   * Creates a mock entity manager for the test.
   *
   * @see ConfigUpdateUnitTestBase::entityDefinitionInformation
   */
  protected function getEntityManagerMock() {
    $definitions = [];
    $map = [];
    foreach ($this->entityDefinitionInformation as $info) {
      $def = $this
        ->getMockBuilder('Drupal\\Core\\Config\\Entity\\ConfigEntityTypeInterface')
        ->getMock();
      $def
        ->expects($this
        ->any())
        ->method('getConfigPrefix')
        ->willReturn($info['prefix']);
      $def
        ->expects($this
        ->any())
        ->method('entityClassImplements')
        ->willReturn(TRUE);
      $def
        ->method('getKey')
        ->willReturn('id');
      $def
        ->getConfigPrefix();
      $definitions[$info['type']] = $def;
      $map[] = [
        $info['type'],
        FALSE,
        $def,
      ];
      $map[] = [
        $info['type'],
        TRUE,
        $def,
      ];
    }

    // Add in a content entity definition, which shouldn't be recognized by the
    // config lister class.
    $def = $this
      ->getMockBuilder('Drupal\\Core\\Entity\\ContentEntityTypeInterface')
      ->getMock();
    $def
      ->expects($this
      ->any())
      ->method('entityClassImplements')
      ->willReturn(FALSE);
    $definitions['content_entity'] = $def;
    $manager = $this
      ->getMockBuilder('Drupal\\Core\\Entity\\EntityTypeManagerInterface')
      ->getMock();
    $manager
      ->method('getDefinitions')
      ->willReturn($definitions);
    $manager
      ->method('getDefinition')
      ->will($this
      ->returnValueMap($map));
    $manager
      ->method('getStorage')
      ->will($this
      ->returnCallback([
      $this,
      'mockGetStorage',
    ]));
    return $manager;
  }

  /**
   * Mocks the getStorage() method for the entity manager.
   */
  public function mockGetStorage($entity_type) {

    // Figure out the config prefix for this entity type.
    $prefix = '';
    foreach ($this->entityDefinitionInformation as $info) {
      if ($info['type'] == $entity_type) {
        $prefix = $info['prefix'];
      }
    }

    // This is used in ConfigReverter::import(). Although it is supposed to
    // be entity storage, we'll use our mock config object instead.
    return new MockConfig('', $prefix, $this);
  }

  /**
   * Array of active configuration information for mocking.
   *
   * Array structure: Each element is an array whose first element is a
   * provider name, and second is an array of config items it provides.
   *
   * @var array
   *
   * @see ConfigUpdateUnitTestBase::getConfigStorageMock()
   */
  protected $configStorageActiveInfo = [
    [
      'foo.bar',
      [
        'foo.bar.one',
        'foo.bar.two',
        'foo.bar.three',
      ],
    ],
    [
      'foo.barbaz',
      [
        'foo.barbaz.four',
        'foo.barbaz.five',
        'foo.barbaz.six',
      ],
    ],
    [
      'baz.foo',
      [],
    ],
    [
      '',
      [
        'foo.bar.one',
        'foo.bar.two',
        'foo.bar.three',
        'foo.barbaz.four',
        'foo.barbaz.five',
        'foo.barbaz.six',
        'something.else',
        'another.one',
      ],
    ],
  ];

  /**
   * Array of extension configuration information for mocking.
   *
   * Array structure: Each element is an array whose first element is a
   * provider name, and second is an array of config items it provides.
   *
   * @var array
   *
   * @see ConfigUpdateUnitTestBase::getConfigStorageMock()
   */
  protected $configStorageExtensionInfo = [
    [
      'foo.bar',
      [
        'foo.bar.one',
        'foo.bar.two',
        'foo.bar.seven',
      ],
    ],
    [
      'baz.foo',
      [],
    ],
    // This next item is assumed to be element 2 of the array. If not, you
    // will need to change ConfigUpdateUnitTestBase::getConfigStorageMock().
    [
      '',
      [
        'foo.bar.one',
        'foo.bar.two',
        'foo.bar.seven',
        'foo.barbaz.four',
        'foo.barnot.three',
        'something.else',
      ],
    ],
  ];

  /**
   * Array of optional configuration information for mocking.
   *
   * Array structure: Each element is an array whose first element is a
   * provider name, and second is an array of config items it provides.
   *
   * @var array
   *
   * @see ConfigUpdateUnitTestBase::getConfigStorageMock()
   */
  protected $configStorageOptionalInfo = [
    [
      'foo.bar',
      [],
    ],
    [
      'foo.barbaz',
      [
        'foo.barbaz.four',
      ],
    ],
    // This next item is assumed to be element 2 of the array. If not, you
    // will need to change ConfigUpdateUnitTestBase::getConfigStorageMock().
    [
      '',
      [
        'foo.barbaz.four',
      ],
    ],
  ];

  /**
   * Creates a mock config storage object for the test.
   *
   * @param string $type
   *   Type of storage object to return: 'active', 'extension', or 'optional'.
   *   In active storage, the read() method is mocked to assume you are reading
   *   core.extension to get the profile name, so it returns that information.
   *   For extension and optional storage, the getComponentNames() method is
   *   mocked, and for all storages, the listAll() method is mocked.
   *
   * @see ConfigUpdateUnitTestBase::configStorageActiveInfo
   * @see ConfigUpdateUnitTestBase::configStorageExtensionInfo
   * @see ConfigUpdateUnitTestBase::configStorageOptionalInfo
   */
  protected function getConfigStorageMock($type) {
    if ($type == 'active') {
      $storage = $this
        ->getMockBuilder('Drupal\\Core\\Config\\StorageInterface')
        ->getMock();

      // Various tests assume various values of configuration that need to be
      // read from active storage.
      $map = [
        [
          'core.extension',
          [
            'profile' => 'standard',
          ],
        ],
        [
          'foo.bar.one',
          [
            'foo.bar.one' => 'active',
            'id' => 'one',
          ],
        ],
        [
          'missing',
          FALSE,
        ],
        [
          'in.extension',
          [
            'in.extension' => 'active',
            '_core' => 'core_for_in.extension',
          ],
        ],
        [
          'in.both',
          [
            'in.both' => 'active',
          ],
        ],
        [
          'in.optional',
          [
            'in.optional' => 'active',
          ],
        ],
      ];
      $storage
        ->method('read')
        ->will($this
        ->returnValueMap($map));
      $storage
        ->method('listAll')
        ->will($this
        ->returnValueMap($this->configStorageActiveInfo));
    }
    elseif ($type == 'extension') {
      $storage = $this
        ->getMockBuilder('Drupal\\Core\\Config\\ExtensionInstallStorage')
        ->disableOriginalConstructor()
        ->getMock();
      $value = [];
      foreach ($this->configStorageExtensionInfo[2][1] as $item) {
        $value[$item] = 'ignored';
      }
      $storage
        ->method('getComponentNames')
        ->willReturn($value);
      $storage
        ->method('listAll')
        ->will($this
        ->returnValueMap($this->configStorageExtensionInfo));
      $map = [
        [
          'in.extension',
          [
            'in.extension' => 'extension',
          ],
        ],
        [
          'in.both',
          [
            'in.both' => 'extension',
          ],
        ],
        [
          'in.optional',
          FALSE,
        ],
        [
          'foo.bar.one',
          [
            'foo.bar.one' => 'extension',
            'id' => 'one',
          ],
        ],
        [
          'another',
          [
            'another' => 'extension',
            'id' => 'one',
          ],
        ],
        [
          'missing2',
          FALSE,
        ],
      ];
      $storage
        ->method('read')
        ->will($this
        ->returnValueMap($map));
    }
    else {
      $storage = $this
        ->getMockBuilder('Drupal\\Core\\Config\\ExtensionInstallStorage')
        ->disableOriginalConstructor()
        ->getMock();
      $value = [];
      foreach ($this->configStorageOptionalInfo[2][1] as $item) {
        $value[$item] = 'ignored';
      }
      $storage
        ->method('getComponentNames')
        ->willReturn($value);
      $storage
        ->method('listAll')
        ->will($this
        ->returnValueMap($this->configStorageOptionalInfo));
      $map = [
        [
          'in.optional',
          [
            'in.optional' => 'optional',
          ],
        ],
        [
          'in.both',
          [
            'in.both' => 'optional',
          ],
        ],
        [
          'missing2',
          FALSE,
        ],
      ];
      $storage
        ->method('read')
        ->will($this
        ->returnValueMap($map));
    }
    return $storage;
  }

  /**
   * Creates a mock module handler for the test.
   */
  protected function getModuleHandlerMock() {
    $manager = $this
      ->getMockBuilder('Drupal\\Core\\Extension\\ModuleHandlerInterface')
      ->getMock();
    $manager
      ->method('getModuleList')
      ->willReturn([
      'foo_module' => '',
      'standard' => '',
    ]);
    return $manager;
  }

  /**
   * Creates a mock theme handler for the test.
   */
  protected function getThemeHandlerMock() {
    $manager = $this
      ->getMockBuilder('Drupal\\Core\\Extension\\ThemeHandlerInterface')
      ->getMock();
    $manager
      ->method('listInfo')
      ->willReturn([
      'foo_theme' => '',
    ]);
    return $manager;
  }

  /**
   * Creates a mock string translation class for the test.
   */
  protected function getTranslationMock() {
    $translation = $this
      ->getMockBuilder('Drupal\\Core\\StringTranslation\\TranslationInterface')
      ->getMock();
    $translation
      ->method('translateString')
      ->will($this
      ->returnCallback([
      $this,
      'mockTranslate',
    ]));
    return $translation;
  }

  /**
   * Mocks the translateString() method for the string translation mock object.
   *
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup $input
   *   Object to translate.
   *
   * @return string
   *   The untranslated string from $input.
   */
  public function mockTranslate(TranslatableMarkup $input) {
    return $input
      ->getUntranslatedString();
  }

  /**
   * List of mock-dispatched events.
   *
   * Each element of the array is the call parameters to dispatchEvent() in
   * the mocked dispatch class: name and event instance.
   *
   * @var array
   *
   * @see ConfigUpdateUnitTestBase::getEventDispatcherMock()
   */
  protected $dispatchedEvents = [];

  /**
   * Mocks the event dispatcher service.
   *
   * Stores dispatched events in ConfigUpdateUnitTestBase::dispatchedEvents.
   */
  protected function getEventDispatcherMock() {
    $event = $this
      ->getMockBuilder('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface')
      ->getMock();
    $event
      ->method('dispatch')
      ->will($this
      ->returnCallback([
      $this,
      'mockDispatch',
    ]));
    return $event;
  }

  /**
   * Mocks event dispatch.
   *
   * For \Symfony\Component\EventDispatcher\EventDispatchInterface::dispatch().
   */
  public function mockDispatch($name, Event $event = NULL) {
    $this->dispatchedEvents[] = [
      $name,
      $event,
    ];
  }

  /**
   * Mock config storage for the mock config factory.
   *
   * This is actually managed by the MockConfig class in this file.
   *
   * @var array
   */
  protected $configStorage = [];

  /**
   * Gets the value of the mocked config storage.
   */
  public function getConfigStorage() {
    return $this->configStorage;
  }

  /**
   * Sets the value of the mocked config storage.
   */
  public function setConfigStorage($values) {
    $this->configStorage = $values;
  }

  /**
   * Creates a mock config factory class for the test.
   */
  protected function getConfigFactoryMock() {
    $config = $this
      ->getMockBuilder('Drupal\\Core\\Config\\ConfigFactoryInterface')
      ->getMock();
    $config
      ->method('getEditable')
      ->will($this
      ->returnCallback([
      $this,
      'mockGetEditable',
    ]));
    return $config;
  }

  /**
   * Mocks the getEditable() method for the mock config factory.
   *
   * @param string $name
   *   Name of the config object to get an editable object for.
   *
   * @return MockConfig
   *   Editable mock config object.
   */
  public function mockGetEditable($name) {
    return new MockConfig($name, '', $this);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigUpdateUnitTestBase::$configStorage protected property Mock config storage for the mock config factory.
ConfigUpdateUnitTestBase::$configStorageActiveInfo protected property Array of active configuration information for mocking.
ConfigUpdateUnitTestBase::$configStorageExtensionInfo protected property Array of extension configuration information for mocking.
ConfigUpdateUnitTestBase::$configStorageOptionalInfo protected property Array of optional configuration information for mocking.
ConfigUpdateUnitTestBase::$dispatchedEvents protected property List of mock-dispatched events.
ConfigUpdateUnitTestBase::$entityDefinitionInformation protected property The mocked entity definition information.
ConfigUpdateUnitTestBase::getConfigFactoryMock protected function Creates a mock config factory class for the test.
ConfigUpdateUnitTestBase::getConfigStorage public function Gets the value of the mocked config storage.
ConfigUpdateUnitTestBase::getConfigStorageMock protected function Creates a mock config storage object for the test.
ConfigUpdateUnitTestBase::getEntityManagerMock protected function Creates a mock entity manager for the test.
ConfigUpdateUnitTestBase::getEventDispatcherMock protected function Mocks the event dispatcher service.
ConfigUpdateUnitTestBase::getModuleHandlerMock protected function Creates a mock module handler for the test.
ConfigUpdateUnitTestBase::getThemeHandlerMock protected function Creates a mock theme handler for the test.
ConfigUpdateUnitTestBase::getTranslationMock protected function Creates a mock string translation class for the test.
ConfigUpdateUnitTestBase::mockDispatch public function Mocks event dispatch.
ConfigUpdateUnitTestBase::mockGetEditable public function Mocks the getEditable() method for the mock config factory.
ConfigUpdateUnitTestBase::mockGetStorage public function Mocks the getStorage() method for the entity manager.
ConfigUpdateUnitTestBase::mockTranslate public function Mocks the translateString() method for the string translation mock object.
ConfigUpdateUnitTestBase::setConfigStorage public function Sets the value of the mocked config storage.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340