/** * @covers ::getDefinitions */ public function testGetDefinitions() { $definitions = array('foo' => array('label' => $this->randomMachineName())); $this->discovery->expects($this->once())->method('getDefinitions')->willReturn($definitions); $this->moduleHandler->expects($this->once())->method('alter')->with('payment_line_item'); $this->assertSame($definitions, $this->sut->getDefinitions()); }
/** * Tests getting the theme registry defined by a module. */ public function testGetRegistryForModule() { $this->setupTheme('test_theme'); $this->registry->setTheme(new ActiveTheme(['name' => 'test_theme', 'path' => 'core/modules/system/tests/themes/test_theme/test_theme.info.yml', 'engine' => 'twig', 'owner' => 'twig', 'stylesheets_remove' => [], 'stylesheets_override' => [], 'libraries' => [], 'extension' => '.twig', 'base_themes' => []])); // Include the module so that hook_theme can be called. include_once $this->root . '/core/modules/system/tests/modules/theme_test/theme_test.module'; $this->moduleHandler->expects($this->once())->method('getImplementations')->with('theme')->will($this->returnValue(array('theme_test'))); $registry = $this->registry->get(); // Ensure that the registry entries from the module are found. $this->assertArrayHasKey('theme_test', $registry); $this->assertArrayHasKey('theme_test_template_test', $registry); $this->assertArrayHasKey('theme_test_template_test_2', $registry); $this->assertArrayHasKey('theme_test_suggestion_provided', $registry); $this->assertArrayHasKey('theme_test_specific_suggestions', $registry); $this->assertArrayHasKey('theme_test_suggestions', $registry); $this->assertArrayHasKey('theme_test_function_suggestions', $registry); $this->assertArrayHasKey('theme_test_foo', $registry); $this->assertArrayHasKey('theme_test_render_element', $registry); $this->assertArrayHasKey('theme_test_render_element_children', $registry); $this->assertArrayHasKey('theme_test_function_template_override', $registry); $this->assertArrayNotHasKey('test_theme_not_existing_function', $registry); $info = $registry['theme_test_function_suggestions']; $this->assertEquals('module', $info['type']); $this->assertEquals('core/modules/system/tests/modules/theme_test', $info['theme path']); $this->assertEquals('theme_theme_test_function_suggestions', $info['function']); $this->assertEquals(array(), $info['variables']); }
/** * {@inheritdoc} */ protected function setUp() { // Create a config mock which does not mock the clear(), set() and get() methods. $methods = get_class_methods('Drupal\\Core\\Config\\Config'); unset($methods[array_search('set', $methods)]); unset($methods[array_search('get', $methods)]); unset($methods[array_search('clear', $methods)]); $config_mock = $this->getMockBuilder('Drupal\\Core\\Config\\Config')->disableOriginalConstructor()->setMethods($methods)->getMock(); // Create the config factory we use in the submitForm() function. $this->configFactory = $this->getMock('Drupal\\Core\\Config\\ConfigFactoryInterface'); $this->configFactory->expects($this->any())->method('getEditable')->will($this->returnValue($config_mock)); // Create a MailsystemManager mock. $this->mailManager = $this->getMock('\\Drupal\\mailsystem\\MailsystemManager', array(), array(), '', FALSE); $this->mailManager->expects($this->any())->method('getDefinition')->will($this->returnValueMap(array(array('mailsystem_test', TRUE, array('label' => 'Test Mail-Plugin')), array('mailsystem_demo', TRUE, array('label' => 'Demo Mail-Plugin'))))); $this->mailManager->expects($this->any())->method('getDefinitions')->will($this->returnValue(array(array('id' => 'mailsystem_test', 'label' => 'Test Mail-Plugin'), array('id' => 'mailsystem_demo', 'label' => 'Demo Mail-Plugin')))); // Create a module handler mock. $this->moduleHandler = $this->getMock('\\Drupal\\Core\\Extension\\ModuleHandlerInterface'); $this->moduleHandler->expects($this->any())->method('getImplementations')->with('mail')->will($this->returnValue(array('mailsystem_test', 'mailsystem_demo'))); $this->moduleHandler->expects($this->any())->method('moduleExists')->withAnyParameters()->will($this->returnValue(FALSE)); // Create a theme handler mock. $this->themeHandler = $this->getMock('\\Drupal\\Core\\Extension\\ThemeHandlerInterface'); $this->themeHandler->expects($this->any())->method('listInfo')->will($this->returnValue(array('test_theme' => (object) array('status' => 1, 'info' => array('name' => 'test theme name')), 'demo_theme' => (object) array('status' => 1, 'info' => array('name' => 'test theme name demo')), 'inactive_theme' => (object) array('status' => 0, 'info' => array('name' => 'inactive test theme'))))); // Inject a language-manager into \Drupal. $this->languageManager = $this->getMock('\\Drupal\\Core\\StringTranslation\\TranslationInterface'); $this->languageManager->expects($this->any())->method('translate')->withAnyParameters()->will($this->returnArgument(0)); $container = new ContainerBuilder(); $container->set('string_translation', $this->languageManager); \Drupal::setContainer($container); }
/** * @covers ::getOperations */ public function testGetOperations() { $operation_name = $this->randomMachineName(); $operations = array($operation_name => array('title' => $this->randomMachineName())); $this->moduleHandler->expects($this->once())->method('invokeAll')->with('entity_operation', array($this->role))->will($this->returnValue($operations)); $this->moduleHandler->expects($this->once())->method('alter')->with('entity_operation'); $this->container->set('module_handler', $this->moduleHandler); $this->role->expects($this->any())->method('access')->will($this->returnValue(AccessResult::allowed())); $this->role->expects($this->any())->method('hasLinkTemplate')->will($this->returnValue(TRUE)); $url = $this->getMockBuilder('\\Drupal\\Core\\Url')->disableOriginalConstructor()->getMock(); $url->expects($this->any())->method('toArray')->will($this->returnValue(array())); $this->role->expects($this->any())->method('urlInfo')->will($this->returnValue($url)); $list = new EntityListBuilder($this->entityType, $this->roleStorage, $this->moduleHandler); $list->setStringTranslation($this->translationManager); $operations = $list->getOperations($this->role); $this->assertInternalType('array', $operations); $this->assertArrayHasKey('edit', $operations); $this->assertInternalType('array', $operations['edit']); $this->assertArrayHasKey('title', $operations['edit']); $this->assertArrayHasKey('delete', $operations); $this->assertInternalType('array', $operations['delete']); $this->assertArrayHasKey('title', $operations['delete']); $this->assertArrayHasKey($operation_name, $operations); $this->assertInternalType('array', $operations[$operation_name]); $this->assertArrayHasKey('title', $operations[$operation_name]); }
/** * Tests the getInfo() method when render element plugins are used. * * @covers ::getInfo * @covers ::buildInfo * * @dataProvider providerTestGetInfoElementPlugin */ public function testGetInfoElementPlugin($plugin_class, $expected_info) { $this->moduleHandler->expects($this->once())->method('alter')->with('element_info', $this->anything())->will($this->returnArgument(0)); $plugin = $this->getMock($plugin_class); $plugin->expects($this->once())->method('getInfo')->willReturn(array('#theme' => 'page')); $element_info = $this->getMockBuilder('Drupal\\Core\\Render\\ElementInfoManager')->setConstructorArgs(array(new \ArrayObject(), $this->cache, $this->cacheTagsInvalidator, $this->moduleHandler, $this->themeManager))->setMethods(array('getDefinitions', 'createInstance'))->getMock(); $this->themeManager->expects($this->any())->method('getActiveTheme')->willReturn(new ActiveTheme(['name' => 'test'])); $element_info->expects($this->once())->method('createInstance')->with('page')->willReturn($plugin); $element_info->expects($this->once())->method('getDefinitions')->willReturn(array('page' => array('class' => 'TestElementPlugin'))); $this->assertEquals($expected_info, $element_info->getInfo('page')); }
/** * {@inheritdoc} */ public function setUp() { $cache_context_manager = $this->getMockBuilder(CacheContextsManager::class)->disableOriginalConstructor()->getMock(); $cache_context_manager->expects($this->any())->method('assertValidTokens')->willReturn(TRUE); $container = new Container(); $container->set('cache_contexts_manager', $cache_context_manager); \Drupal::setContainer($container); $entity_type = $this->getMock(EntityTypeInterface::class); $this->moduleHandler = $this->getMock(ModuleHandlerInterface::class); $this->moduleHandler->expects($this->any())->method('invokeAll')->willReturn([]); $this->sut = new PaymentMethodConfigurationAccessControlHandler($entity_type, $this->moduleHandler); }
/** * @covers ::getLibrariesByExtension */ public function testGetLibrariesByExtension() { $this->libraryDiscoveryCollector->expects($this->once())->method('get')->with('test')->willReturn($this->libraryData); $this->moduleHandler->expects($this->exactly(2))->method('alter')->with('library', $this->logicalOr($this->libraryData['test_1'], $this->libraryData['test_2']), $this->logicalOr('test/test_1', 'test/test_2')); $this->libraryDiscovery->getLibrariesbyExtension('test'); // Verify that subsequent calls don't trigger hook_library_info_alter() // and hook_js_settings_alter() invocations, nor do they talk to the // collector again. This ensures that the alterations made by // hook_library_info_alter() and hook_js_settings_alter() implementations // are statically cached, as desired. $this->libraryDiscovery->getLibraryByName('test', 'test_1'); $this->libraryDiscovery->getLibrariesbyExtension('test'); }
/** * Test the buildCommentedEntityLinks method. * * @param \Drupal\node\NodeInterface|\PHPUnit_Framework_MockObject_MockObject $node * Mock node. * @param array $context * Context for the links. * @param bool $has_access_comments * TRUE if the user has 'access comments' permission. * @param bool $history_exists * TRUE if the history module exists. * @param bool $has_post_comments * TRUE if the use has 'post comments' permission. * @param bool $is_anonymous * TRUE if the user is anonymous. * @param array $expected * Array of expected links keyed by link ID. Can be either string (link * title) or array of link properties. * * @dataProvider getLinkCombinations * * @covers ::buildCommentedEntityLinks */ public function testCommentLinkBuilder(NodeInterface $node, $context, $has_access_comments, $history_exists, $has_post_comments, $is_anonymous, $expected) { $this->moduleHandler->expects($this->any())->method('moduleExists')->with('history')->willReturn($history_exists); $this->currentUser->expects($this->any())->method('hasPermission')->willReturnMap(array(array('access comments', $has_access_comments), array('post comments', $has_post_comments))); $this->currentUser->expects($this->any())->method('isAuthenticated')->willReturn(!$is_anonymous); $this->currentUser->expects($this->any())->method('isAnonymous')->willReturn($is_anonymous); $links = $this->commentLinkBuilder->buildCommentedEntityLinks($node, $context); if (!empty($expected)) { if (!empty($links)) { foreach ($expected as $link => $detail) { if (is_array($detail)) { // Array of link attributes. foreach ($detail as $key => $value) { $this->assertEquals($value, $links['comment__comment']['#links'][$link][$key]); } } else { // Just the title. $this->assertEquals($detail, $links['comment__comment']['#links'][$link]['title']); } } } else { $this->fail('Expected links but found none.'); } } else { $this->assertSame($links, $expected); } }
/** * @covers ::getFieldMap */ public function testGetFieldMap() { // Set up a content entity type. $entity_type = $this->getMock('Drupal\\Core\\Entity\\ContentEntityTypeInterface'); $entity = $this->getMockBuilder('Drupal\\Tests\\Core\\Entity\\EntityManagerTestEntity')->disableOriginalConstructor()->getMockForAbstractClass(); $entity_class = get_class($entity); $entity_type->expects($this->any())->method('getClass')->will($this->returnValue($entity_class)); $entity_type->expects($this->any())->method('getKeys')->will($this->returnValue(array())); $entity_type->expects($this->any())->method('id')->will($this->returnValue('test_entity_type')); $entity_type->expects($this->any())->method('isSubclassOf')->with('\\Drupal\\Core\\Entity\\ContentEntityInterface')->will($this->returnValue(TRUE)); // Set up the module handler to return two bundles for the fieldable entity // type. $this->moduleHandler = $this->getMock('Drupal\\Core\\Extension\\ModuleHandlerInterface'); $this->moduleHandler->expects($this->any())->method('alter'); $this->moduleHandler->expects($this->any())->method('getImplementations')->will($this->returnValue(array())); $module_implements_value_map = array(array('entity_bundle_info', array(), array('test_entity_type' => array('first_bundle' => array(), 'second_bundle' => array())))); $this->moduleHandler->expects($this->any())->method('invokeAll')->will($this->returnValueMap($module_implements_value_map)); // Define an ID field definition as a base field. $id_definition = $this->getMockBuilder('Drupal\\Core\\Field\\FieldDefinition')->disableOriginalConstructor()->getMock(); $id_definition->expects($this->exactly(2))->method('getType')->will($this->returnValue('integer')); $base_field_definitions = array('id' => $id_definition); $entity_class::$baseFieldDefinitions = $base_field_definitions; // Set up a by bundle field definition that only exists on one bundle. $bundle_definition = $this->getMockBuilder('Drupal\\Core\\Field\\FieldDefinition')->disableOriginalConstructor()->getMock(); $bundle_definition->expects($this->once())->method('getType')->will($this->returnValue('string')); $entity_class::$bundleFieldDefinitions = array('test_entity_type' => array('first_bundle' => array(), 'second_bundle' => array('by_bundle' => $bundle_definition))); // Set up a non-content entity type. $non_content_entity_type = $this->getMock('Drupal\\Core\\Entity\\EntityTypeInterface'); $entity_type->expects($this->any())->method('isSubclassOf')->with('\\Drupal\\Core\\Entity\\ContentEntityInterface')->will($this->returnValue(FALSE)); $this->setUpEntityManager(array('test_entity_type' => $entity_type, 'non_fieldable' => $non_content_entity_type)); $expected = array('test_entity_type' => array('id' => array('type' => 'integer', 'bundles' => array('first_bundle', 'second_bundle')), 'by_bundle' => array('type' => 'string', 'bundles' => array('second_bundle')))); $this->assertEquals($expected, $this->entityManager->getFieldMap()); }
/** * Tests multiple breadcrumb builders of which one returns NULL. */ public function testBuildWithOneNotApplyingBuilders() { $builder1 = $this->getMock('Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface'); $builder1->expects($this->once()) ->method('applies') ->will($this->returnValue(FALSE)); $builder1->expects($this->never()) ->method('build'); $builder2 = $this->getMock('Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface'); $links2 = ['<a href="/example2">Test2</a>']; $this->breadcrumb->setLinks($links2); $this->breadcrumb->addCacheContexts(['baz'])->addCacheTags(['qux']); $builder2->expects($this->once()) ->method('applies') ->will($this->returnValue(TRUE)); $builder2->expects($this->once()) ->method('build') ->willReturn($this->breadcrumb); $route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface'); $this->moduleHandler->expects($this->once()) ->method('alter') ->with('system_breadcrumb', $this->breadcrumb, $route_match, array('builder' => $builder2)); $this->breadcrumbManager->addBuilder($builder1, 10); $this->breadcrumbManager->addBuilder($builder2, 0); $breadcrumb = $this->breadcrumbManager->build($route_match); $this->assertEquals($links2, $breadcrumb->getLinks()); $this->assertEquals(['baz'], $breadcrumb->getCacheContexts()); $this->assertEquals(['qux'], $breadcrumb->getCacheTags()); $this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge()); }
public function testInitPackageWithExistingPackage() { $bundle = new FeaturesBundle(['machine_name' => 'default'], 'features_bundle'); $features_manager = new TestFeaturesManager('vfs://drupal', $this->entityManager, $this->configFactory, $this->configStorage, $this->configManager, $this->moduleHandler); vfsStream::setup('drupal'); \Drupal::getContainer()->set('app.root', 'vfs://drupal'); vfsStream::create(['modules' => ['test_feature' => ['test_feature.info.yml' => <<<EOT name: Test feature 2 type: module core: 8.x description: test description 2 EOT , 'test_feature.features.yml' => <<<EOT true EOT ]]]); $extension = new Extension('vfs://drupal', 'module', 'modules/test_feature/test_feature.info.yml'); $features_manager->setAllModules(['test_feature' => $extension]); $this->moduleHandler->expects($this->any())->method('exists')->with('test_feature')->willReturn(TRUE); $info_parser = new InfoParser(); \Drupal::getContainer()->set('info_parser', $info_parser); $package = $features_manager->initPackage('test_feature', 'test name', 'test description', 'module', $bundle); $this->assertInstanceOf(Package::class, $package); $this->assertEquals(TRUE, $package->getFeaturesInfo()); }
/** * Tests the plugins alter hook. */ public function testPluginDefinitionAlter() { $definitions['test_plugin'] = array('id' => 'test_plugin', 'class' => '\\Drupal\\Core\\Menu\\ContextualLinkDefault', 'title' => 'Plugin', 'weight' => 2, 'group' => 'group1', 'route_name' => 'test_route', 'options' => array('key' => 'value')); $this->pluginDiscovery->expects($this->once())->method('getDefinitions')->will($this->returnValue($definitions)); $this->moduleHandler->expects($this->once())->method('alter')->with('contextual_links_plugins', $definitions); $this->contextualLinkManager->getDefinition('test_plugin'); }
/** * Tests rebuild the theme data with theme parents. */ public function testRebuildThemeDataWithThemeParents() { $this->extensionDiscovery->expects($this->at(0))->method('scan')->with('theme')->will($this->returnValue(array('test_subtheme' => new Extension($this->root, 'theme', $this->root . '/core/modules/system/tests/themes/test_subtheme/test_subtheme.info.yml', 'test_subtheme.info.yml'), 'test_basetheme' => new Extension($this->root, 'theme', $this->root . '/core/modules/system/tests/themes/test_basetheme/test_basetheme.info.yml', 'test_basetheme.info.yml')))); $this->extensionDiscovery->expects($this->at(1))->method('scan')->with('theme_engine')->will($this->returnValue(array('twig' => new Extension($this->root, 'theme_engine', $this->root . '/core/themes/engines/twig/twig.info.yml', 'twig.engine')))); $this->infoParser->expects($this->at(0))->method('parse')->with($this->root . '/core/modules/system/tests/themes/test_subtheme/test_subtheme.info.yml')->will($this->returnCallback(function ($file) { $info_parser = new InfoParser(); return $info_parser->parse($file); })); $this->infoParser->expects($this->at(1))->method('parse')->with($this->root . '/core/modules/system/tests/themes/test_basetheme/test_basetheme.info.yml')->will($this->returnCallback(function ($file) { $info_parser = new InfoParser(); return $info_parser->parse($file); })); $this->moduleHandler->expects($this->once())->method('buildModuleDependencies')->will($this->returnArgument(0)); $theme_data = $this->themeHandler->rebuildThemeData(); $this->assertCount(2, $theme_data); $info_basetheme = $theme_data['test_basetheme']; $info_subtheme = $theme_data['test_subtheme']; // Ensure some basic properties. $this->assertInstanceOf('Drupal\\Core\\Extension\\Extension', $info_basetheme); $this->assertEquals('test_basetheme', $info_basetheme->getName()); $this->assertInstanceOf('Drupal\\Core\\Extension\\Extension', $info_subtheme); $this->assertEquals('test_subtheme', $info_subtheme->getName()); // Test the parent/child-theme properties. $info_subtheme->info['base theme'] = 'test_basetheme'; $info_basetheme->sub_themes = array('test_subtheme'); $this->assertEquals($this->root . '/core/themes/engines/twig/twig.engine', $info_basetheme->owner); $this->assertEquals('twig', $info_basetheme->prefix); $this->assertEquals($this->root . '/core/themes/engines/twig/twig.engine', $info_subtheme->owner); $this->assertEquals('twig', $info_subtheme->prefix); }
/** * @covers ::buildRow * * @dataProvider providerTestBuildRow * * @depends testBuildOperations */ function testBuildRow($payment_currency_exists) { $payment_changed_time = time(); $payment_changed_time_formatted = $this->randomMachineName(); $payment_currency_code = $this->randomMachineName(); $payment_amount = mt_rand(); $payment_amount_formatted = $this->randomMachineName(); $payment_status_definition = array('label' => $this->randomMachineName()); $payment_status = $this->getMock(PaymentStatusInterface::class); $payment_status->expects($this->any())->method('getPluginDefinition')->willReturn($payment_status_definition); $owner = $this->getMock(UserInterface::class); $payment_method_label = $this->randomMachineName(); $payment_method_definition = ['label' => $payment_method_label]; $payment_method = $this->getMock(PaymentMethodInterface::class); $payment_method->expects($this->atLeastOnce())->method('getPluginDefinition')->willReturn($payment_method_definition); $payment = $this->getMock(PaymentInterface::class); $payment->expects($this->any())->method('getAmount')->willReturn($payment_amount); $payment->expects($this->any())->method('getChangedTime')->willReturn($payment_changed_time); $payment->expects($this->any())->method('getCurrencyCode')->willReturn($payment_currency_code); $payment->expects($this->any())->method('getOwner')->willReturn($owner); $payment->expects($this->any())->method('getPaymentMethod')->willReturn($payment_method); $payment->expects($this->any())->method('getPaymentStatus')->willReturn($payment_status); $currency = $this->getMock(CurrencyInterface::class); $currency->expects($this->once())->method('formatAmount')->with($payment_amount)->willReturn($payment_amount_formatted); $map = array(array($payment_currency_code, $payment_currency_exists ? $currency : NULL), array('XXX', $payment_currency_exists ? NULL : $currency)); $this->currencyStorage->expects($this->atLeastOnce())->method('load')->willReturnMap($map); $this->dateFormatter->expects($this->once())->method('format')->with($payment_changed_time)->willReturn($payment_changed_time_formatted); $this->moduleHandler->expects($this->any())->method('invokeAll')->willReturn([]); $build = $this->sut->buildRow($payment); unset($build['data']['operations']['data']['#attached']); $expected_build = array('data' => array('updated' => $payment_changed_time_formatted, 'status' => $payment_status_definition['label'], 'amount' => $payment_amount_formatted, 'payment_method' => $payment_method_label, 'owner' => array('data' => array('#theme' => 'username', '#account' => $owner)), 'operations' => array('data' => array('#type' => 'operations', '#links' => [])))); $this->assertSame($expected_build, $build); }
/** * @covers ::delete * @covers ::doDelete */ public function testDeleteNothing() { $this->moduleHandler->expects($this->never())->method($this->anything()); $this->configFactory->expects($this->never())->method('get'); $this->cacheTagsInvalidator->expects($this->never())->method('invalidateTags'); $this->entityStorage->delete(array()); }
/** * Tests a YAML file containing both static permissions and a callback. */ public function testPermissionsYamlStaticAndCallback() { vfsStreamWrapper::register(); $root = new vfsStreamDirectory('modules'); vfsStreamWrapper::setRoot($root); $this->moduleHandler = $this->getMock('Drupal\\Core\\Extension\\ModuleHandlerInterface'); $this->moduleHandler->expects($this->once())->method('getModuleDirectories')->willReturn(array('module_a' => vfsStream::url('modules/module_a'))); $url = vfsStream::url('modules'); mkdir($url . '/module_a'); file_put_contents($url . '/module_a/module_a.permissions.yml', "'access module a':\n title: 'Access A'\n description: 'bla bla'\npermission_callbacks:\n - 'Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescription'\n"); $modules = array('module_a'); $extensions = array('module_a' => $this->mockModuleExtension('module_a', 'Module a')); $this->moduleHandler->expects($this->any())->method('getImplementations')->with('permission')->willReturn(array()); $this->moduleHandler->expects($this->any())->method('getModuleList')->willReturn(array_flip($modules)); $this->controllerResolver->expects($this->once())->method('getControllerFromDefinition')->with('Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescription')->willReturn(array(new TestPermissionCallbacks(), 'titleDescription')); $this->permissionHandler = new TestPermissionHandler($this->moduleHandler, $this->stringTranslation, $this->controllerResolver); // Setup system_rebuild_module_data(). $this->permissionHandler->setSystemRebuildModuleData($extensions); $actual_permissions = $this->permissionHandler->getPermissions(); $this->assertCount(2, $actual_permissions); $this->assertEquals($actual_permissions['access module a']['title'], 'Access A'); $this->assertEquals($actual_permissions['access module a']['provider'], 'module_a'); $this->assertEquals($actual_permissions['access module a']['description'], 'bla bla'); $this->assertEquals($actual_permissions['access module b']['title'], 'Access B'); $this->assertEquals($actual_permissions['access module b']['provider'], 'module_a'); $this->assertEquals($actual_permissions['access module b']['description'], 'bla bla'); }
/** * @covers ::delete * @covers ::doDelete */ public function testDeleteNothing() { $this->setUpKeyValueEntityStorage(); $this->moduleHandler->expects($this->never())->method($this->anything()); $this->keyValueStore->expects($this->never())->method('delete'); $this->keyValueStore->expects($this->never())->method('deleteMultiple'); $this->entityStorage->delete(array()); }
/** * @covers ::getDefinitions * @covers ::processDefinition */ public function testGetDefinitions() { $discovery_definitions = array('foo' => array('id' => NULL, 'parent_id' => NULL, 'label' => $this->randomMachineName(), 'description' => NULL, 'operations_provider' => NULL, 'class' => DefaultPaymentStatus::class)); $manager_definitions = $discovery_definitions; $manager_definitions['foo']['label'] = new TranslatableMarkup($manager_definitions['foo']['label'], [], [], $this->stringTranslation); $this->discovery->expects($this->once())->method('getDefinitions')->willReturn($discovery_definitions); $this->moduleHandler->expects($this->once())->method('alter')->with('payment_status'); $this->assertEquals($manager_definitions, $this->sut->getDefinitions()); }
/** * @covers ::replace * @dataProvider providerTestReplaceEscaping */ public function testReplaceEscaping($string, array $tokens, $expected) { $this->moduleHandler->expects($this->any())->method('invokeAll')->willReturnCallback(function ($type, $args) { return $args[2]['tokens']; }); $result = $this->token->replace($string, ['tokens' => $tokens]); $this->assertInternalType('string', $result); $this->assertEquals($expected, $result); }
/** * @covers ::getInfo */ public function testGetInfo() { $token_info = array('types' => array('foo' => array('name' => $this->randomMachineName()))); $this->language->expects($this->atLeastOnce())->method('getId')->will($this->returnValue($this->randomMachineName())); $this->languageManager->expects($this->once())->method('getCurrentLanguage')->with(LanguageInterface::TYPE_CONTENT)->will($this->returnValue($this->language)); // The persistent cache must only be hit once, after which the info is // cached statically. $this->cache->expects($this->once())->method('get'); $this->cache->expects($this->once())->method('set')->with('token_info:' . $this->language->getId(), $token_info); $this->moduleHandler->expects($this->once())->method('invokeAll')->with('token_info')->will($this->returnValue($token_info)); $this->moduleHandler->expects($this->once())->method('alter')->with('token_info', $token_info); // Get the information for the first time. The cache should be checked, the // hooks invoked, and the info should be set to the cache should. $this->token->getInfo(); // Get the information for the second time. The data must be returned from // the static cache, so the persistent cache must not be accessed and the // hooks must not be invoked. $this->token->getInfo(); }
/** * @covers ::getPluginType * * @dataProvider providerGetPluginType */ public function testGetPluginType($expected_success, $plugin_type_id, $module_exists) { $this->moduleHandler->expects($this->atLeastOnce())->method('moduleExists')->willReturn(isset($this->pluginTypeDefinitions[$plugin_type_id]) && $module_exists); if ($expected_success) { $this->assertInstanceOf(PluginTypeInterface::class, $this->sut->getPluginType($plugin_type_id)); } else { $this->setExpectedException('\\InvalidArgumentException'); $this->sut->getPluginType($plugin_type_id); } }
/** * @covers ::buildRow */ function testBuildRow() { $entity_label = $this->randomMachineName(); $currency_locale = $this->getMock(CurrencyLocaleInterface::class); $currency_locale->expects($this->any())->method('label')->willReturn($entity_label); $this->moduleHandler->expects($this->any())->method('invokeAll')->willReturn([]); $row = $this->sut->buildRow($currency_locale); $expected = array('label' => $entity_label, 'operations' => array('data' => array('#type' => 'operations', '#links' => array()))); $this->assertSame($expected, $row); }
/** * @covers ::checkAccess * * @dataProvider providerTestCheckAccess */ function testCheckAccess($expected_value, $operation, $has_permission, $permission, $locale = NULL) { $account = $this->getMock(AccountInterface::class); $account->expects($this->any())->method('hasPermission')->with($permission)->willReturn((bool) $has_permission); $currency_locale = $this->getMock(CurrencyLocaleInterface::class); $currency_locale->expects($this->any())->method('getLocale')->willReturn($locale); $this->moduleHandler->expects($this->any())->method('invokeAll')->willReturn([]); $method = new \ReflectionMethod($this->sut, 'checkAccess'); $method->setAccessible(TRUE); $this->assertSame($expected_value, $method->invoke($this->sut, $currency_locale, $operation, $account)->isAllowed()); }
/** * @covers ::buildConfigurationForm * * @dataProvider providerTestBuildConfigurationForm */ public function testBuildConfigurationForm($filter_exists) { $this->moduleHandler->expects($this->atLeastOnce())->method('moduleExists')->with('filter')->willReturn($filter_exists); $form = []; $form_state = $this->getMock(FormStateInterface::class); $payment = $this->getMockPayment(); $elements = $this->sut->buildConfigurationForm($form, $form_state, $payment); $this->assertInternalType('array', $elements); $this->assertArrayHasKey('message', $elements); $this->assertInternalType('array', $elements['message']); }
/** * @covers ::getConfigStorage * @covers ::setConfigStorage */ public function testGetConfigStorage() { $method_get = new \ReflectionMethod($this->sut, 'getConfigStorage'); $method_get->setAccessible(TRUE); $extension = new Extension($this->randomMachineName(), $this->randomMachineName(), $this->randomMachineName()); $this->moduleHandler->expects($this->atLeastOnce())->method('getModule')->willReturn($extension); $this->assertInstanceof(StorageInterface::class, $method_get->invoke($this->sut)); $config_storage = $this->getMock(StorageInterface::class); $this->sut->setConfigStorage($config_storage); $this->assertSame($config_storage, $method_get->invoke($this->sut)); }
/** * {@inheritdoc} */ protected function setUp() { $this->editorId = $this->randomMachineName(); $this->entityTypeId = $this->randomMachineName(); $this->entityType = $this->getMock('\\Drupal\\Core\\Entity\\EntityTypeInterface'); $this->entityType->expects($this->any())->method('getProvider')->will($this->returnValue('editor')); $this->entityManager = $this->getMock('\\Drupal\\Core\\Entity\\EntityManagerInterface'); $this->entityManager->expects($this->any())->method('getDefinition')->with($this->entityTypeId)->will($this->returnValue($this->entityType)); $this->uuid = $this->getMock('\\Drupal\\Component\\Uuid\\UuidInterface'); $this->editorPluginManager = $this->getMockBuilder('Drupal\\editor\\Plugin\\EditorManager')->disableOriginalConstructor()->getMock(); $this->moduleHandler = $this->getMock('\\Drupal\\Core\\Extension\\ModuleHandlerInterface'); $this->moduleHandler->expects($this->once())->method('invokeAll')->with('editor_default_settings', array($this->editorId))->will($this->returnValue(array())); $this->moduleHandler->expects($this->once())->method('alter')->with('editor_default_settings', array(), $this->editorId)->will($this->returnValue(array())); $container = new ContainerBuilder(); $container->set('entity.manager', $this->entityManager); $container->set('uuid', $this->uuid); $container->set('plugin.manager.editor', $this->editorPluginManager); $container->set('module_handler', $this->moduleHandler); \Drupal::setContainer($container); }
/** * @covers ::loadCachedFormState */ public function testLoadCachedFormStateWithFiles() { $form_build_id = 'the_form_build_id'; $form_state = new FormState(); $cached_form = ['#cache_token' => NULL]; $this->formCacheStore->expects($this->once())->method('get')->with($form_build_id)->willReturn($cached_form); $this->account->expects($this->once())->method('isAnonymous')->willReturn(TRUE); $cached_form_state = ['build_info' => ['files' => [['module' => 'a_module', 'type' => 'the_type', 'name' => 'some_name'], ['module' => 'another_module']]]]; $this->moduleHandler->expects($this->at(0))->method('loadInclude')->with('a_module', 'the_type', 'some_name'); $this->moduleHandler->expects($this->at(1))->method('loadInclude')->with('another_module', 'inc', 'another_module'); $this->formStateCacheStore->expects($this->once())->method('get')->with($form_build_id)->willReturn($cached_form_state); $this->formCache->getCache($form_build_id, $form_state); }
/** * Tests the cache calls for all views data. * * Warm cache: * - all tables */ public function testCacheCallsWithWarmCacheAndGetAllTables() { $expected_views_data = $this->viewsDataWithProvider(); $this->moduleHandler->expects($this->never())->method('getImplementations'); // Setup a warm cache backend for a single table. $this->cacheBackend->expects($this->once())->method('get')->with("views_data:en")->will($this->returnValue((object) array('data' => $expected_views_data))); $this->cacheBackend->expects($this->never())->method('set'); // Initialize the views data cache and repeat with no specified table. This // should only load the cache entry for all tables. for ($i = 0; $i < 5; $i++) { $views_data = $this->viewsData->get(); $this->assertSame($expected_views_data, $views_data); } }
/** * @covers ::checkAccess * * @dataProvider providerTestCheckAccess */ function testCheckAccess($expected_value, $operation, $has_permission, $permission, $entity_status = FALSE, $currency_code = NULL) { $account = $this->getMock(AccountInterface::class); $account->expects($this->any())->method('hasPermission')->with($permission)->willReturn((bool) $has_permission); $language = $this->getMock(LanguageInterface::class); $currency = $this->getMock(CurrencyInterface::class); $currency->expects($this->any())->method('getCurrencyCode')->willReturn($currency_code); $currency->expects($this->any())->method('language')->willReturn($language); $currency->expects($this->any())->method('status')->willReturn($entity_status); $this->moduleHandler->expects($this->any())->method('invokeAll')->willReturn([]); $method = new \ReflectionMethod($this->sut, 'checkAccess'); $method->setAccessible(TRUE); $this->assertSame($expected_value, $method->invoke($this->sut, $currency, $operation, $account)->isAllowed()); }
/** * Tests getting the theme registry defined by a module. */ public function testGetRegistryForModule() { $test_theme = new ActiveTheme(['name' => 'test_theme', 'path' => 'core/modules/system/tests/themes/test_theme/test_theme.info.yml', 'engine' => 'twig', 'owner' => 'twig', 'stylesheets_remove' => [], 'libraries_override' => [], 'libraries_extend' => [], 'libraries' => [], 'extension' => '.twig', 'base_themes' => []]); $test_stable = new ActiveTheme(['name' => 'test_stable', 'path' => 'core/modules/system/tests/themes/test_stable/test_stable.info.yml', 'engine' => 'twig', 'owner' => 'twig', 'stylesheets_remove' => [], 'libraries_override' => [], 'libraries_extend' => [], 'libraries' => [], 'extension' => '.twig', 'base_themes' => []]); $this->themeManager->expects($this->exactly(2))->method('getActiveTheme')->willReturnOnConsecutiveCalls($test_theme, $test_stable); // Include the module and theme files so that hook_theme can be called. include_once $this->root . '/core/modules/system/tests/modules/theme_test/theme_test.module'; include_once $this->root . '/core/modules/system/tests/themes/test_stable/test_stable.theme'; $this->moduleHandler->expects($this->exactly(2))->method('getImplementations')->with('theme')->will($this->returnValue(array('theme_test'))); $this->moduleHandler->expects($this->atLeastOnce())->method('getModuleList')->willReturn([]); $registry = $this->registry->get(); // Ensure that the registry entries from the module are found. $this->assertArrayHasKey('theme_test', $registry); $this->assertArrayHasKey('theme_test_template_test', $registry); $this->assertArrayHasKey('theme_test_template_test_2', $registry); $this->assertArrayHasKey('theme_test_suggestion_provided', $registry); $this->assertArrayHasKey('theme_test_specific_suggestions', $registry); $this->assertArrayHasKey('theme_test_suggestions', $registry); $this->assertArrayHasKey('theme_test_function_suggestions', $registry); $this->assertArrayHasKey('theme_test_foo', $registry); $this->assertArrayHasKey('theme_test_render_element', $registry); $this->assertArrayHasKey('theme_test_render_element_children', $registry); $this->assertArrayHasKey('theme_test_function_template_override', $registry); $this->assertArrayNotHasKey('test_theme_not_existing_function', $registry); $this->assertFalse(in_array('test_stable_preprocess_theme_test_render_element', $registry['theme_test_render_element']['preprocess functions'])); $info = $registry['theme_test_function_suggestions']; $this->assertEquals('module', $info['type']); $this->assertEquals('core/modules/system/tests/modules/theme_test', $info['theme path']); $this->assertEquals('theme_theme_test_function_suggestions', $info['function']); $this->assertEquals(array(), $info['variables']); // The second call will initialize with the second theme. Ensure that this // returns a different object and the discovery for the second theme's // preprocess function worked. $other_registry = $this->registry->get(); $this->assertNotSame($registry, $other_registry); $this->assertTrue(in_array('test_stable_preprocess_theme_test_render_element', $other_registry['theme_test_render_element']['preprocess functions'])); }