View source
<?php
namespace Drupal\Tests\warden\Unit\Service;
use Drupal\Tests\UnitTestCase;
use Drupal\warden\Service\WardenManager;
use Drupal\Core\Extension\Extension;
class WardenManagerTest extends UnitTestCase {
protected $wardenManager;
protected $token;
protected $infoParser;
protected $configFactory;
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([]);
}
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];
}));
}
protected function getMockExtension($name, $type = 'contrib') {
$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;
}
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];
}));
}
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);
}
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);
}
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);
}
}