You are here

class WardenManagerTest in Warden 8.2

Same name and namespace in other branches
  1. 8 tests/src/Unit/Service/WardenManagerTest.php \Drupal\Tests\warden\Unit\Service\WardenManagerTest
  2. 3.x tests/src/Unit/Service/WardenManagerTest.php \Drupal\Tests\warden\Unit\Service\WardenManagerTest

@coversDefaultClass \Drupal\warden\Service\WardenManager @group warden

Hierarchy

Expanded class hierarchy of WardenManagerTest

File

tests/src/Unit/Service/WardenManagerTest.php, line 13

Namespace

Drupal\Tests\warden\Unit\Service
View source
class WardenManagerTest extends UnitTestCase {

  /**
   * @var WardenManager
   */
  protected $wardenManager;

  /**
   * @var string
   */
  protected $token;

  /**
   * @var \Drupal\Core\Extension\InfoParser|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $infoParser;

  /**
   * @var \Drupal\Core\Config\ConfigFactory|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $configFactory;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->token = hash('sha256', 42);
    $this->infoParser = $this
      ->getMock('Drupal\\Core\\Extension\\InfoParser', array(), array(), '', FALSE);
    $this->configFactory = $this
      ->getMock('Drupal\\Core\\Config\\ConfigFactory', array(), array(), '', FALSE);
    $this
      ->expectConfigFactoryAccess();
    $this->wardenManager = new WardenManager($this->configFactory);
    $this->wardenManager
      ->setBaseUrl('http://www.example.com')
      ->setTime(12345678)
      ->setInfoParser($this->infoParser)
      ->setThemes([])
      ->setModules([]);
  }

  /**
   * Expect access to the config factory via the WardenManager constructor.
   */
  protected function expectConfigFactoryAccess() {
    $wardenConfig = $this
      ->getMock('Drupal\\Core\\Config\\Config', array(), array(), '', FALSE);
    $systemConfig = $this
      ->getMock('Drupal\\Core\\Config\\Config', array(), array(), '', FALSE);
    $this->configFactory
      ->expects($this
      ->any())
      ->method('get')
      ->will($this
      ->returnCallback(function ($setting_name) use ($wardenConfig, $systemConfig) {
      $settings = [
        'warden.settings' => $wardenConfig,
        'system.site' => $systemConfig,
      ];
      return $settings[$setting_name];
    }));
    $systemConfig
      ->expects($this
      ->once())
      ->method('get')
      ->with('name')
      ->willReturn('My Website');
    $wardenConfig
      ->expects($this
      ->any())
      ->method('get')
      ->will($this
      ->returnCallback(function ($setting_name) {
      $settings = [
        'warden_preg_match_contrib' => '{^modules\\/contrib\\/*}',
        'warden_preg_match_custom' => '{^modules\\/custom\\/*}',
        'warden_match_custom' => TRUE,
        'warden_match_contrib' => TRUE,
        'warden_token' => $this->token,
        'warden_server_host_path' => 'http://warden.example.com',
        'warden_http_username' => '',
        'warden_http_password' => '',
        'warden_certificate_path' => '',
      ];
      return $settings[$setting_name];
    }));
  }

  /**
   * @param string $name
   * @param string $type
   * @return Extension|\PHPUnit_Framework_MockObject_MockObject
   */
  protected function getMockExtension($name, $type = 'contrib') {

    /** @var Extension|\PHPUnit_Framework_MockObject_MockObject $module_b */
    $extension = $this
      ->getMock('Drupal\\Core\\Extension\\Extension', array(), array(), '', FALSE);
    $extension
      ->expects($this
      ->once())
      ->method('getPathname')
      ->willReturn("modules/{$type}/{$name}/{$name}.info.yml");
    return $extension;
  }

  /**
   * @param array $moduleNames
   *   A list of module names (e.g. ['module_a', 'module_b']
   *   The info parser will expect there to be 2 calls to parse
   *   and it will return an array each time of the form
   *   ['version' => '8.x-1.0']
   */
  protected function expectContribModules(array $moduleNames) {
    $results = [];
    foreach ($moduleNames as $moduleName) {
      $results["modules/contrib/{$moduleName}/{$moduleName}.info.yml"] = [
        'version' => '8.x-1.0',
      ];
    }
    $this->infoParser
      ->expects($this
      ->exactly(count($moduleNames)))
      ->method('parse')
      ->will($this
      ->returnCallback(function ($info_filename) use ($results) {
      return $results[$info_filename];
    }));
  }

  /**
   * Tests generating module data with no modules.
   */
  public function testGenerateDataNoModules() {
    $expected_data = [
      'core' => [
        'drupal' => [
          'version' => \Drupal::VERSION,
        ],
      ],
      'contrib' => [],
      'custom' => [],
      'url' => 'http://www.example.com',
      'site_name' => 'My Website',
      'key' => $this->token,
      'time' => 12345678,
    ];
    $actual_data = $this->wardenManager
      ->generateSiteData();
    $this
      ->assertEquals($expected_data, $actual_data);
  }

  /**
   * Tests generating module data with one module.
   */
  public function testGenerateDataOneModule() {
    $expected_data = [
      'core' => [
        'drupal' => [
          'version' => \Drupal::VERSION,
        ],
      ],
      'contrib' => [
        'Module A' => [
          'version' => '8.x-1.0',
        ],
      ],
      'custom' => [],
      'url' => 'http://www.example.com',
      'site_name' => 'My Website',
      'key' => $this->token,
      'time' => 12345678,
    ];
    $this->wardenManager
      ->setModules([
      'module_a' => $this
        ->getMockExtension('module_a'),
    ]);
    $this->infoParser
      ->expects($this
      ->once())
      ->method('parse')
      ->with('modules/contrib/module_a/module_a.info.yml')
      ->willReturn([
      'project' => 'Module A',
      'version' => '8.x-1.0',
    ]);
    $actual_data = $this->wardenManager
      ->generateSiteData();
    $this
      ->assertEquals($expected_data, $actual_data);
  }

  /**
   * Tests generating module data with two modules.
   */
  public function testGenerateDataTwoModules() {
    $expected_data = [
      'core' => [
        'drupal' => [
          'version' => \Drupal::VERSION,
        ],
      ],
      'contrib' => [
        'module_a' => [
          'version' => '8.x-1.0',
        ],
        'module_b' => [
          'version' => '8.x-1.0',
        ],
      ],
      'custom' => [],
      'url' => 'http://www.example.com',
      'site_name' => 'My Website',
      'key' => $this->token,
      'time' => 12345678,
    ];
    $this->wardenManager
      ->setModules([
      'module_a' => $this
        ->getMockExtension('module_a'),
      'module_b' => $this
        ->getMockExtension('module_b'),
    ]);
    $this
      ->expectContribModules([
      'module_a',
      'module_b',
    ]);
    $actual_data = $this->wardenManager
      ->generateSiteData();
    $this
      ->assertEquals($expected_data, $actual_data);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
WardenManagerTest::$configFactory protected property
WardenManagerTest::$infoParser protected property
WardenManagerTest::$token protected property
WardenManagerTest::$wardenManager protected property
WardenManagerTest::expectConfigFactoryAccess protected function Expect access to the config factory via the WardenManager constructor.
WardenManagerTest::expectContribModules protected function
WardenManagerTest::getMockExtension protected function
WardenManagerTest::setUp protected function Overrides UnitTestCase::setUp
WardenManagerTest::testGenerateDataNoModules public function Tests generating module data with no modules.
WardenManagerTest::testGenerateDataOneModule public function Tests generating module data with one module.
WardenManagerTest::testGenerateDataTwoModules public function Tests generating module data with two modules.