本文整理汇总了PHP中Drupal::setContainer方法的典型用法代码示例。如果您正苦于以下问题:PHP Drupal::setContainer方法的具体用法?PHP Drupal::setContainer怎么用?PHP Drupal::setContainer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal
的用法示例。
在下文中一共展示了Drupal::setContainer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setUp
protected function setUp() {
// Mock the TypedDataManager to return a TypedDataInterface mock.
$this->typedData = $this->getMock('Drupal\Core\TypedData\TypedDataInterface');
$typed_data_manager = $this->getMock(TypedDataManagerInterface::class);
$typed_data_manager->expects($this->any())
->method('getPropertyInstance')
->will($this->returnValue($this->typedData));
// Set up a mock container as ItemList() will call for the 'typed_data_manager'
// service.
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')
->setMethods(array('get'))
->getMock();
$container->expects($this->any())
->method('get')
->with($this->equalTo('typed_data_manager'))
->will($this->returnValue($typed_data_manager));
\Drupal::setContainer($container);
$this->normalizer = new ListNormalizer();
$this->list = new ItemList(new DataDefinition());
$this->list->setValue($this->expectedListValues);
}
示例2: testInlineTemplate
/**
* Tests inline templates.
*/
public function testInlineTemplate()
{
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = $this->container->get('renderer');
/** @var \Drupal\Core\Template\TwigEnvironment $environment */
$environment = \Drupal::service('twig');
$this->assertEqual($environment->renderInline('test-no-context'), 'test-no-context');
$this->assertEqual($environment->renderInline('test-with-context {{ llama }}', array('llama' => 'muuh')), 'test-with-context muuh');
$element = array();
$unsafe_string = '<script>alert(\'Danger! High voltage!\');</script>';
$element['test'] = array('#type' => 'inline_template', '#template' => 'test-with-context <label>{{ unsafe_content }}</label>', '#context' => array('unsafe_content' => $unsafe_string));
$this->assertEqual($renderer->renderRoot($element), 'test-with-context <label>' . SafeMarkup::checkPlain($unsafe_string) . '</label>');
// Enable twig_auto_reload and twig_debug.
$settings = Settings::getAll();
$settings['twig_debug'] = TRUE;
$settings['twig_auto_reload'] = TRUE;
new Settings($settings);
$this->container = $this->kernel->rebuildContainer();
\Drupal::setContainer($this->container);
$element = array();
$element['test'] = array('#type' => 'inline_template', '#template' => 'test-with-context {{ llama }}', '#context' => array('llama' => 'muuh'));
$element_copy = $element;
// Render it twice so that twig caching is triggered.
$this->assertEqual($renderer->renderRoot($element), 'test-with-context muuh');
$this->assertEqual($renderer->renderRoot($element_copy), 'test-with-context muuh');
}
示例3: setUp
public function setUp()
{
$this->request = Request::create('/');
$this->account = $this->getMock('\\Drupal\\Core\\Session\\AccountInterface');
$this->container = $this->getMock('\\Symfony\\Component\\DependencyInjection\\ContainerInterface');
$this->manager = $this->getMock('\\Drupal\\Core\\Entity\\EntityManagerInterface');
$this->entityInfo = $this->getMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
$this->manager->expects($this->any())->method('getDefinition')->will($this->returnValue($this->entityInfo));
$this->linkGenerator = $this->getMock('\\Drupal\\Core\\Utility\\LinkGeneratorInterface');
$this->map = array(array('current_user', Container::EXCEPTION_ON_INVALID_REFERENCE, $this->account), array('entity.manager', Container::EXCEPTION_ON_INVALID_REFERENCE, $this->manager), array('link_generator', Container::EXCEPTION_ON_INVALID_REFERENCE, $this->linkGenerator));
$this->container->expects($this->any())->method('get')->will($this->returnValueMap($this->map));
$this->date = $this->getMockBuilder('\\Drupal\\Core\\Datetime\\Date')->disableOriginalConstructor()->getMock();
\Drupal::setContainer($this->container);
$this->entityManager = $this->getMock('\\Drupal\\Core\\Entity\\EntityManagerInterface');
$this->config = $this->getMockBuilder('\\Drupal\\Core\\Config\\Config')->disableOriginalConstructor()->getMock();
$this->translation = $this->getStringTranslationStub();
$this->controller = new FeedFormController($this->entityManager, $this->config, $this->date);
$this->controller->setTranslationManager($this->translation);
$this->controller->setRequest($this->request);
$this->fetcher = new MockFetcher();
$this->parser = new MockParser();
$this->processor = new MockProcessor();
$this->importer = $this->getMock('\\Drupal\\feeds\\ImporterInterface');
$this->importer->expects($this->any())->method('getPlugins')->will($this->returnValue(array('fetcher' => $this->fetcher, 'parser' => $this->parser, 'processor' => $this->processor)));
$this->entityLanguage = $this->getMock('\\Drupal\\Core\\Language\\Language');
$this->feed = $this->getMock('\\Drupal\\feeds\\FeedInterface');
$this->feed->expects($this->any())->method('language')->will($this->returnValue($this->entityLanguage));
$this->feed->expects($this->any())->method('getImporter')->will($this->returnValue($this->importer));
$this->feed->expects($this->any())->method('getAuthor')->will($this->returnValue($this->account));
$this->feed->expects($this->any())->method('entityInfo')->will($this->returnValue($this->entityInfo));
$this->controller->setEntity($this->feed);
}
示例4: testConstructor
/**
* Tests the constructor assignment of actions.
*/
public function testConstructor()
{
$actions = array();
for ($i = 1; $i <= 2; $i++) {
$action = $this->getMock('\\Drupal\\system\\ActionConfigEntityInterface');
$action->expects($this->any())->method('getType')->will($this->returnValue('user'));
$actions[$i] = $action;
}
$action = $this->getMock('\\Drupal\\system\\ActionConfigEntityInterface');
$action->expects($this->any())->method('getType')->will($this->returnValue('node'));
$actions[] = $action;
$entity_storage = $this->getMock('Drupal\\Core\\Entity\\EntityStorageInterface');
$entity_storage->expects($this->any())->method('loadMultiple')->will($this->returnValue($actions));
$views_data = $this->getMockBuilder('Drupal\\views\\ViewsData')->disableOriginalConstructor()->getMock();
$views_data->expects($this->any())->method('get')->with('users')->will($this->returnValue(array('table' => array('entity type' => 'user'))));
$container = new ContainerBuilder();
$container->set('views.views_data', $views_data);
\Drupal::setContainer($container);
$storage = $this->getMock('Drupal\\views\\ViewStorageInterface');
$storage->expects($this->any())->method('get')->with('base_table')->will($this->returnValue('users'));
$executable = $this->getMockBuilder('Drupal\\views\\ViewExecutable')->disableOriginalConstructor()->getMock();
$executable->storage = $storage;
$display = $this->getMockBuilder('Drupal\\views\\Plugin\\views\\display\\DisplayPluginBase')->disableOriginalConstructor()->getMock();
$definition['title'] = '';
$options = array();
$user_bulk_form = new UserBulkForm(array(), 'user_bulk_form', $definition, $entity_storage);
$user_bulk_form->init($executable, $display, $options);
$this->assertAttributeEquals(array_slice($actions, 0, -1, TRUE), 'actions', $user_bulk_form);
}
示例5: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
$cache_contexts_manager = $this->prophesize(CacheContextsManager::class)->reveal();
$container = new Container();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
}
示例6: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
// TODO: Change the autogenerated stub
$condition_plugin_manager = $this->getMock('Drupal\\Core\\Executable\\ExecutableManagerInterface');
$condition_plugin_manager->expects($this->any())->method('getDefinitions')->will($this->returnValue(array()));
$container = new ContainerBuilder();
$container->set('plugin.manager.condition', $condition_plugin_manager);
\Drupal::setContainer($container);
$this->executable = $this->getMockBuilder('Drupal\\views\\ViewExecutable')->disableOriginalConstructor()->setMethods(['buildRenderable', 'setDisplay', 'setItemsPerPage'])->getMock();
$this->executable->expects($this->any())->method('setDisplay')->with('block_1')->will($this->returnValue(TRUE));
$this->executable->expects($this->any())->method('getShowAdminLinks')->willReturn(FALSE);
$this->executable->display_handler = $this->getMockBuilder('Drupal\\views\\Plugin\\views\\display\\Block')->disableOriginalConstructor()->setMethods(NULL)->getMock();
$this->view = $this->getMockBuilder('Drupal\\views\\Entity\\View')->disableOriginalConstructor()->getMock();
$this->view->expects($this->any())->method('id')->willReturn('test_view');
$this->executable->storage = $this->view;
$this->executableFactory = $this->getMockBuilder('Drupal\\views\\ViewExecutableFactory')->disableOriginalConstructor()->getMock();
$this->executableFactory->expects($this->any())->method('get')->with($this->view)->will($this->returnValue($this->executable));
$this->displayHandler = $this->getMockBuilder('Drupal\\views\\Plugin\\views\\display\\Block')->disableOriginalConstructor()->getMock();
$this->displayHandler->expects($this->any())->method('blockSettings')->willReturn([]);
$this->displayHandler->expects($this->any())->method('getPluginId')->willReturn('block');
$this->executable->display_handler = $this->displayHandler;
$this->storage = $this->getMockBuilder('Drupal\\Core\\Config\\Entity\\ConfigEntityStorage')->disableOriginalConstructor()->getMock();
$this->storage->expects($this->any())->method('load')->with('test_view')->will($this->returnValue($this->view));
$this->account = $this->getMock('Drupal\\Core\\Session\\AccountInterface');
}
示例7: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
$this->viewStorage = $this->getMock('Drupal\\Core\\Entity\\EntityStorageInterface');
$this->executableFactory = $this->getMockBuilder('Drupal\\views\\ViewExecutableFactory')->disableOriginalConstructor()->getMock();
$this->renderer = $this->getMock('\\Drupal\\Core\\Render\\RendererInterface');
$this->renderer->expects($this->any())->method('render')->will($this->returnCallback(function (array &$elements) {
$elements['#attached'] = [];
return isset($elements['#markup']) ? $elements['#markup'] : '';
}));
$this->renderer->expects($this->any())->method('executeInRenderContext')->willReturnCallback(function (RenderContext $context, callable $callable) {
return $callable();
});
$this->currentPath = $this->getMockBuilder('Drupal\\Core\\Path\\CurrentPathStack')->disableOriginalConstructor()->getMock();
$this->redirectDestination = $this->getMock('\\Drupal\\Core\\Routing\\RedirectDestinationInterface');
$this->viewAjaxController = new ViewAjaxController($this->viewStorage, $this->executableFactory, $this->renderer, $this->currentPath, $this->redirectDestination);
$element_info_manager = $this->getMock('\\Drupal\\Core\\Render\\ElementInfoManagerInterface');
$element_info_manager->expects($this->any())->method('getInfo')->with('markup')->willReturn(['#pre_render' => [[Markup::class, 'ensureMarkupIsSafe']], '#defaults_loaded' => TRUE]);
$request_stack = new RequestStack();
$request_stack->push(new Request());
$args = [$this->getMock('\\Drupal\\Core\\Controller\\ControllerResolverInterface'), $this->getMock('\\Drupal\\Core\\Theme\\ThemeManagerInterface'), $element_info_manager, $this->getMock('\\Drupal\\Core\\Render\\RenderCacheInterface'), $request_stack, ['required_cache_contexts' => ['languages:language_interface', 'theme']]];
$this->renderer = $this->getMockBuilder('Drupal\\Core\\Render\\Renderer')->setConstructorArgs($args)->setMethods(NULL)->getMock();
$container = new ContainerBuilder();
$container->set('renderer', $this->renderer);
\Drupal::setContainer($container);
}
示例8: testAnonymousUserSessionWithNoRequest
/**
* Tests creating an AnonymousUserSession when the request is not available.
*
* @covers ::__construct()
*/
public function testAnonymousUserSessionWithNoRequest()
{
$container = new ContainerBuilder();
\Drupal::setContainer($container);
$anonymous_user = new AnonymousUserSession();
$this->assertSame('', $anonymous_user->getHostname());
}
示例9: handle
/**
* {@inheritdoc}
*/
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE)
{
if (\Drupal::state()->get('error_service_test.break_bare_html_renderer')) {
// Let the bedlam begin.
// 1) Force a container rebuild.
/** @var \Drupal\Core\DrupalKernelInterface $kernel */
$kernel = \Drupal::service('kernel');
$kernel->rebuildContainer();
// 2) Fetch the in-situ container builder.
$container = ErrorServiceTestServiceProvider::$containerBuilder;
// Ensure the compiler pass worked.
if (!$container) {
throw new \Exception('Oh oh, monkeys stole the ServiceProvider.');
}
// Stop the theme manager from being found - and triggering error
// maintenance mode.
$container->removeDefinition('theme.manager');
// Mash. Mash. Mash.
\Drupal::setContainer($container);
throw new \Exception('Oh oh, bananas in the instruments.');
}
if (\Drupal::state()->get('error_service_test.break_logger')) {
throw new \Exception('Deforestation');
}
return $this->app->handle($request, $type, $catch);
}
示例10: setUp
/**
* {@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);
}
示例11: setUp
/**
* {@inheritdoc}
*/
public function setUp()
{
parent::setUp();
$cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
$cache_contexts_manager->assertValidTokens()->willReturn(TRUE);
$cache_contexts_manager->reveal();
$container = new Container();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
$this->viewer = $this->getMock('\\Drupal\\Core\\Session\\AccountInterface');
$this->viewer->expects($this->any())->method('hasPermission')->will($this->returnValue(FALSE));
$this->viewer->expects($this->any())->method('id')->will($this->returnValue(1));
$this->owner = $this->getMock('\\Drupal\\Core\\Session\\AccountInterface');
$this->owner->expects($this->any())->method('hasPermission')->will($this->returnValueMap(array(array('administer users', FALSE), array('change own username', TRUE))));
$this->owner->expects($this->any())->method('id')->will($this->returnValue(2));
$this->admin = $this->getMock('\\Drupal\\Core\\Session\\AccountInterface');
$this->admin->expects($this->any())->method('hasPermission')->will($this->returnValue(TRUE));
$entity_type = $this->getMock('Drupal\\Core\\Entity\\EntityTypeInterface');
$this->accessControlHandler = new UserAccessControlHandler($entity_type);
$module_handler = $this->getMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
$module_handler->expects($this->any())->method('getImplementations')->will($this->returnValue(array()));
$this->accessControlHandler->setModuleHandler($module_handler);
$this->items = $this->getMockBuilder('Drupal\\Core\\Field\\FieldItemList')->disableOriginalConstructor()->getMock();
$this->items->expects($this->any())->method('defaultAccess')->will($this->returnValue(AccessResult::allowed()));
}
示例12: tearDown
/**
* {@inheritdoc}
*/
protected function tearDown()
{
parent::tearDown();
// Restore the previous container.
$this->container = $this->previousContainer;
\Drupal::setContainer($this->previousContainer);
}
示例13: testTitleQuery
/**
* Tests the title_query method.
*
* @see \Drupal\user\Plugin\views\argument\RolesRid::title_query()
*/
public function testTitleQuery()
{
$role1 = new Role(array('id' => 'test_rid_1', 'label' => 'test rid 1'), 'user_role');
$role2 = new Role(array('id' => 'test_rid_2', 'label' => 'test <strong>rid 2</strong>'), 'user_role');
// Creates a stub entity storage;
$role_storage = $this->getMockForAbstractClass('Drupal\\Core\\Entity\\EntityStorageInterface');
$role_storage->expects($this->any())->method('loadMultiple')->will($this->returnValueMap(array(array(array(), array()), array(array('test_rid_1'), array('test_rid_1' => $role1)), array(array('test_rid_1', 'test_rid_2'), array('test_rid_1' => $role1, 'test_rid_2' => $role2)))));
$entity_type = $this->getMock('Drupal\\Core\\Entity\\EntityTypeInterface');
$entity_type->expects($this->any())->method('getKey')->with('label')->will($this->returnValue('label'));
$entity_manager = $this->getMock('Drupal\\Core\\Entity\\EntityManagerInterface');
$entity_manager->expects($this->any())->method('getDefinition')->with($this->equalTo('user_role'))->will($this->returnValue($entity_type));
$entity_manager->expects($this->once())->method('getStorage')->with($this->equalTo('user_role'))->will($this->returnValue($role_storage));
// @todo \Drupal\Core\Entity\Entity::entityType() uses a global call to
// entity_get_info(), which in turn wraps \Drupal::entityManager(). Set
// the entity manager until this is fixed.
$container = new ContainerBuilder();
$container->set('entity.manager', $entity_manager);
\Drupal::setContainer($container);
$roles_rid_argument = new RolesRid(array(), 'user__roles_rid', array(), $entity_manager);
$roles_rid_argument->value = array();
$titles = $roles_rid_argument->title_query();
$this->assertEquals(array(), $titles);
$roles_rid_argument->value = array('test_rid_1');
$titles = $roles_rid_argument->title_query();
$this->assertEquals(array('test rid 1'), $titles);
$roles_rid_argument->value = array('test_rid_1', 'test_rid_2');
$titles = $roles_rid_argument->title_query();
$this->assertEquals(array('test rid 1', String::checkPlain('test <strong>rid 2</strong>')), $titles);
}
示例14: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->pathValidator = $this->getMock('Drupal\\Core\\Path\\PathValidatorInterface');
$container = new ContainerBuilder();
$container->set('path.validator', $this->pathValidator);
\Drupal::setContainer($container);
}
示例15: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
$this->editAccessCheck = new EditEntityFieldAccessCheck();
$cache_contexts_manager = $this->prophesize(CacheContextsManager::class)->reveal();
$container = new Container();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
}