/** * Tests if forum module uninstallation properly deletes the field. */ function testForumUninstallWithField() { // Ensure that the field exists before uninstallation. $field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums'); $this->assertNotNull($field_storage, 'The taxonomy_forums field storage exists.'); // Create a taxonomy term. $term = entity_create('taxonomy_term', array('name' => t('A term'), 'langcode' => \Drupal::languageManager()->getDefaultLanguage()->id, 'description' => '', 'parent' => array(0), 'vid' => 'forums', 'forum_container' => 0)); $term->save(); // Create a forum node. $node = $this->drupalCreateNode(array('title' => 'A forum post', 'type' => 'forum', 'taxonomy_forums' => array(array('target_id' => $term->id())))); // Create at least one comment against the forum node. $comment = entity_create('comment', array('entity_id' => $node->nid->value, 'entity_type' => 'node', 'field_name' => 'comment_forum', 'pid' => 0, 'uid' => 0, 'status' => CommentInterface::PUBLISHED, 'subject' => $this->randomName(), 'hostname' => '127.0.0.1')); $comment->save(); // Uninstall the forum module which should trigger field deletion. $this->container->get('module_handler')->uninstall(array('forum')); // We want to test the handling of removing the forum comment field, so we // ensure there is at least one other comment field attached to a node type // so that comment_entity_load() runs for nodes. \Drupal::service('comment.manager')->addDefaultField('node', 'forum', 'another_comment_field', CommentItemInterface::OPEN, 'another_comment_field'); $this->drupalGet('node/' . $node->nid->value); $this->assertResponse(200); // Check that the field is now deleted. $field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums'); $this->assertNull($field_storage, 'The taxonomy_forums field storage has been deleted.'); }
/** * Tests that configuration has been updated. */ public function testUpdatedSite() { $key_to_be_removed = 'display.default.display_options.fields.nid'; /** @var \Drupal\Core\Config\Config $config_override */ $language_config_override = \Drupal::service('language.config_factory_override'); $config_override = $language_config_override->getOverride('es', 'views.view.content'); $this->assertEqual('Spanish ID', $config_override->get($key_to_be_removed)['label'], 'The spanish override for the missing field exists before updating.'); // Since the above view will be fixed by other updates that fix views // configuration for example, // views_post_update_update_cacheability_metadata(), also test configuration // that has yet to be modified in an update path. $config_override = $language_config_override->getOverride('es', 'system.cron'); $this->assertEqual('Should be cleaned by system_update_8200', $config_override->get('bogus_key'), 'The spanish override in system.cron exists before updating.'); $this->runUpdates(); /** @var \Drupal\Core\Config\Config $config_override */ $config_override = \Drupal::service('language.config_factory_override')->getOverride('es', 'views.view.content'); $this->assertNull($config_override->get($key_to_be_removed), 'The spanish override for the missing field has been removed.'); $config_override = $language_config_override->getOverride('es', 'system.cron'); $this->assertTrue($config_override->isNew(), 'After updating the system.cron spanish override does not exist.'); $this->assertTrue(empty($config_override->get()), 'After updating the system.cron spanish override has no data.'); // Test that the spanish overrides still work. $this->drupalLogin($this->createUser(['access content overview'])); $this->drupalGet('admin/content', ['language' => \Drupal::languageManager()->getLanguage('es')]); $this->assertText('Spanish Title'); $this->assertText('Spanish Author'); }
/** * Helper method for ::getActiveTrailIds(). */ protected function doGetActiveTrailIds($menu_name) { // Parent ids; used both as key and value to ensure uniqueness. // We always want all the top-level links with parent == ''. $active_trail = array('' => ''); // If a link in the given menu indeed matches the route, then use it to // complete the active trail. if ($active_link = $this->getActiveLink($menu_name)) { if ($parents = $this->menuLinkManager->getParentIds($active_link->getPluginId())) { $active_trail = $parents + $active_trail; } } else { // No matching link, so check paths against current link. $path = $this->getCurrentPathAlias(); $menu_parameters = new MenuTreeParameters(); $tree = \Drupal::menuTree()->load($menu_name, $menu_parameters); foreach ($tree as $menu_link_route => $menu_link) { $menu_url = $menu_link->link->getUrlObject(); $menu_path = $menu_url->toString(); // Check if this item's path exists in the current path. // Also check if there is a langcode prefix. $lang_prefix = '/' . \Drupal::languageManager()->getCurrentLanguage()->getId(); if (strpos($path, $menu_path) === 0 || strpos($lang_prefix . $path, $menu_path) === 0) { if ($this->pathIsMoreSimilar($path, $menu_path)) { $parents = array($menu_link_route => $menu_link_route); $active_trail = $parents + $active_trail; } } } } return $active_trail; }
function testDefaultTermLanguage() { // Configure the vocabulary to not hide the language selector, and make the // default language of the terms fixed. $edit = array('default_language[langcode]' => 'bb', 'default_language[language_show]' => TRUE); $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save')); $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add'); $this->assertOptionSelected('edit-langcode', 'bb', 'The expected langcode was selected.'); // Make the default language of the terms to be the current interface. $edit = array('default_language[langcode]' => 'current_interface', 'default_language[language_show]' => TRUE); $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save')); $this->drupalGet('aa/admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add'); $this->assertOptionSelected('edit-langcode', 'aa', "The expected langcode, 'aa', was selected."); $this->drupalGet('bb/admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add'); $this->assertOptionSelected('edit-langcode', 'bb', "The expected langcode, 'bb', was selected."); // Change the default language of the site and check if the default terms // language is still correctly selected. $old_default = \Drupal::languageManager()->getDefaultLanguage(); $old_default->default = FALSE; language_save($old_default); $new_default = \Drupal::languageManager()->getLanguage('cc'); $new_default->default = TRUE; language_save($new_default); $edit = array('default_language[langcode]' => 'site_default', 'default_language[language_show]' => TRUE); $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save')); $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add'); $this->assertOptionSelected('edit-langcode', 'cc', "The expected langcode, 'cc', was selected."); }
/** * Checks the From: and Reply-to: headers. */ public function testFromAndReplyToHeader() { $language = \Drupal::languageManager()->getCurrentLanguage(); // Use the state system collector mail backend. $this->config('system.mail')->set('interface.default', 'test_mail_collector')->save(); // Reset the state variable that holds sent messages. \Drupal::state()->set('system.test_mail_collector', array()); // Send an email with a reply-to address specified. $from_email = 'Drupal <*****@*****.**>'; $reply_email = '*****@*****.**'; \Drupal::service('plugin.manager.mail')->mail('simpletest', 'from_test', '*****@*****.**', $language, array(), $reply_email); // Test that the reply-to email is just the email and not the site name // and default sender email. $captured_emails = \Drupal::state()->get('system.test_mail_collector'); $sent_message = end($captured_emails); $this->assertEqual($from_email, $sent_message['headers']['From'], 'Message is sent from the site email account.'); $this->assertEqual($reply_email, $sent_message['headers']['Reply-to'], 'Message reply-to headers are set.'); $this->assertFalse(isset($sent_message['headers']['Errors-To']), 'Errors-to header must not be set, it is deprecated.'); // Send an email and check that the From-header contains the site name. \Drupal::service('plugin.manager.mail')->mail('simpletest', 'from_test', '*****@*****.**', $language); $captured_emails = \Drupal::state()->get('system.test_mail_collector'); $sent_message = end($captured_emails); $this->assertEqual($from_email, $sent_message['headers']['From'], 'Message is sent from the site email account.'); $this->assertFalse(isset($sent_message['headers']['Reply-to']), 'Message reply-to is not set if not specified.'); $this->assertFalse(isset($sent_message['headers']['Errors-To']), 'Errors-to header must not be set, it is deprecated.'); }
/** * Test translated breadcrumbs. */ public function testTranslatedBreadcrumbs() { // Ensure non-translated breadcrumb is correct. $breadcrumb = array(Url::fromRoute('<front>')->toString() => 'Home'); foreach ($this->terms as $term) { $breadcrumb[$term->url()] = $term->label(); } // The last item will not be in the breadcrumb. array_pop($breadcrumb); // Check the breadcrumb on the leaf term page. $term = $this->getLeafTerm(); $this->assertBreadcrumb($term->urlInfo(), $breadcrumb, $term->label()); $languages = \Drupal::languageManager()->getLanguages(); // Construct the expected translated breadcrumb. $breadcrumb = array(Url::fromRoute('<front>', [], ['language' => $languages[$this->translateToLangcode]])->toString() => 'Home'); foreach ($this->terms as $term) { $translated = $term->getTranslation($this->translateToLangcode); $url = $translated->url('canonical', ['language' => $languages[$this->translateToLangcode]]); $breadcrumb[$url] = $translated->label(); } array_pop($breadcrumb); // Check for the translated breadcrumb on the translated leaf term page. $term = $this->getLeafTerm(); $translated = $term->getTranslation($this->translateToLangcode); $this->assertBreadcrumb($translated->urlInfo('canonical', ['language' => $languages[$this->translateToLangcode]]), $breadcrumb, $translated->label()); }
/** * Tests translating the site name. */ function testSiteNameTranslation() { $adminUser = $this->drupalCreateUser(array('administer site configuration', 'administer languages')); $this->drupalLogin($adminUser); // Add a custom language. $langcode = 'xx'; $name = $this->randomMachineName(16); $edit = array('predefined_langcode' => 'custom', 'langcode' => $langcode, 'label' => $name, 'direction' => LanguageInterface::DIRECTION_LTR); $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language')); \Drupal::languageManager()->getLanguageConfigOverride($langcode, 'system.site')->set('name', 'XX site name')->save(); $this->drupalLogout(); // The home page in English should not have the override. $this->drupalGet(''); $this->assertNoText('XX site name'); // During path resolution the system.site configuration object is used to // determine the front page. This occurs before language negotiation causing // the configuration factory to cache an object without the correct // overrides. We are testing that the configuration factory is // re-initialised after language negotiation. Ensure that it applies when // we access the XX front page. // @see \Drupal\Core\PathProcessor::processInbound() $this->drupalGet('xx'); $this->assertText('XX site name'); // Set the xx language to be the default language and delete the English // language so the site is no longer multilingual and confirm configuration // overrides still work. $language_manager = \Drupal::languageManager()->reset(); $this->assertTrue($language_manager->isMultilingual(), 'The test site is multilingual.'); $this->config('system.site')->set('default_langcode', 'xx')->save(); ConfigurableLanguage::load('en')->delete(); $this->assertFalse($language_manager->isMultilingual(), 'The test site is monolingual.'); $this->drupalGet('xx'); $this->assertText('XX site name'); }
/** * {@inheritdoc} */ public function render(ResultRow $values) { $value = $this->getValue($values); $language = \Drupal::languageManager()->getLanguage($value); $value = $language ? $language->getName() : ''; return $this->renderLink($this->sanitizeValue($value), $values); }
/** * Tests that language can be enabled and overrides are created during a sync. */ public function testConfigOverrideImport() { ConfigurableLanguage::createFromLangcode('fr')->save(); /* @var \Drupal\Core\Config\StorageInterface $sync */ $sync = \Drupal::service('config.storage.sync'); $this->copyConfig(\Drupal::service('config.storage'), $sync); // Uninstall the language module and its dependencies so we can test // enabling the language module and creating overrides at the same time // during a configuration synchronization. \Drupal::service('module_installer')->uninstall(array('language')); // Ensure that the current site has no overrides registered to the // ConfigFactory. $this->rebuildContainer(); /* @var \Drupal\Core\Config\StorageInterface $override_sync */ $override_sync = $sync->createCollection('language.fr'); // Create some overrides in sync. $override_sync->write('system.site', array('name' => 'FR default site name')); $override_sync->write('system.maintenance', array('message' => 'FR message: @site is currently under maintenance. We should be back shortly. Thank you for your patience')); $this->configImporter()->import(); $this->rebuildContainer(); \Drupal::service('router.builder')->rebuild(); $override = \Drupal::languageManager()->getLanguageConfigOverride('fr', 'system.site'); $this->assertEqual('FR default site name', $override->get('name')); $this->drupalGet('fr'); $this->assertText('FR default site name'); $this->drupalLogin($this->rootUser); $this->drupalGet('admin/config/development/maintenance/translate/fr/edit'); $this->assertText('FR message: @site is currently under maintenance. We should be back shortly. Thank you for your patience'); }
/** * Tests creating a comment type programmatically and via a form. */ public function testCommentTypeCreation() { // Create a comment type programmatically. $type = $this->createCommentType('other'); $comment_type = CommentType::load('other'); $this->assertTrue($comment_type, 'The new comment type has been created.'); // Login a test user. $this->drupalLogin($this->adminUser); $this->drupalGet('admin/structure/comment/manage/' . $type->id()); $this->assertResponse(200, 'The new comment type can be accessed at the edit form.'); // Create a comment type via the user interface. $edit = array('id' => 'foo', 'label' => 'title for foo', 'description' => '', 'target_entity_type_id' => 'node'); $this->drupalPostForm('admin/structure/comment/types/add', $edit, t('Save')); $comment_type = CommentType::load('foo'); $this->assertTrue($comment_type, 'The new comment type has been created.'); // Check that the comment type was created in site default language. $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId(); $this->assertEqual($comment_type->language()->getId(), $default_langcode); // Edit the comment-type and ensure that we cannot change the entity-type. $this->drupalGet('admin/structure/comment/manage/foo'); $this->assertNoField('target_entity_type_id', 'Entity type file not present'); $this->assertText(t('Target entity type')); // Save the form and ensure the entity-type value is preserved even though // the field isn't present. $this->drupalPostForm(NULL, array(), t('Save')); \Drupal::entityManager()->getStorage('comment_type')->resetCache(array('foo')); $comment_type = CommentType::load('foo'); $this->assertEqual($comment_type->getTargetEntityTypeId(), 'node'); }
/** * Adds a language. * * @param string $langcode * The language code of the language to add. */ protected function addLanguage($langcode) { $edit = array('predefined_langcode' => $langcode); $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language')); $this->container->get('language_manager')->reset(); $this->assertTrue(\Drupal::languageManager()->getLanguage($langcode), SafeMarkup::format('Language %langcode added.', array('%langcode' => $langcode))); }
protected function setUp() { parent::setUp(); // Create and login user. $web_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'administer site configuration')); $this->drupalLogin($web_user); // Enable French language. $edit = array(); $edit['predefined_langcode'] = 'fr'; $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language')); // Make French the default language. $edit = array('site_default_language' => 'fr'); $this->drupalPostForm('admin/config/regional/language', $edit, t('Save configuration')); // Delete English. $this->drupalPostForm('admin/config/regional/language/delete/en', array(), t('Delete')); // Changing the default language causes a container rebuild. Therefore need // to rebuild the container in the test environment. $this->rebuildContainer(); // Verify that French is the only language. $this->container->get('language_manager')->reset(); $this->assertFalse(\Drupal::languageManager()->isMultilingual(), 'Site is mono-lingual'); $this->assertEqual(\Drupal::languageManager()->getDefaultLanguage()->getId(), 'fr', 'French is the default language'); // Set language detection to URL. $edit = array('language_interface[enabled][language-url]' => TRUE); $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings')); }
/** * {@inheritdoc} */ public function applyDefaultValue($notify = TRUE) { // Default to the site's default language. When language module is enabled, // this behavior is configurable, see language_field_info_alter(). $this->setValue(array('value' => \Drupal::languageManager()->getDefaultLanguage()->getId()), $notify); return $this; }
/** * Test our new email form. * * Tests for the following: * * - A link to the email_example in the Tools menu. * - That you can successfully access the email_example page. */ public function testEmailExampleBasic() { // Test for a link to the email_example in the Tools menu. $this->drupalGet(''); $this->assertResponse(200, 'The Home page is available.'); $this->assertLinkByHref('examples/email_example'); // Verify if we can successfully access the email_example page. $this->drupalGet('examples/email_example'); $this->assertResponse(200, 'The Email Example description page is available.'); // Verifiy email form has email & message fields. $this->assertFieldById('edit-email', NULL, 'The email field appears.'); $this->assertFieldById('edit-message', NULL, 'The message field appears.'); // Verifiy email form is submitted. $edit = array('email' => '*****@*****.**', 'message' => 'test'); $this->drupalPostForm('examples/email_example', $edit, t('Submit')); $this->assertResponse(200); // Verifiy comfirmation page. $this->assertText(t('Your message has been sent.'), 'The text "Your message has been sent." appears on the email example page.', 'Form response with the right message.'); $this->assertMailString('to', $edit['email'], 1); // Verifiy correct email recieved. $from = \Drupal::config('system.site')->get('mail'); $t_options = array('langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId()); $this->assertMailString('subject', t('E-mail sent from @site-name', array('@site-name' => $from), $t_options), 1); $this->assertMailString('body', $edit['message'], 1); $this->assertMailString('body', t("\n--\nMail altered by email_example module.", array(), $t_options), 1); }
/** * Creates a node, then tests the statistics tokens generated from it. */ function testStatisticsTokenReplacement() { $language_interface = \Drupal::languageManager()->getCurrentLanguage(); // Create user and node. $user = $this->drupalCreateUser(array('create page content')); $this->drupalLogin($user); $node = $this->drupalCreateNode(array('type' => 'page', 'uid' => $user->id())); // Hit the node. $this->drupalGet('node/' . $node->id()); // Manually calling statistics.php, simulating ajax behavior. $nid = $node->id(); $post = http_build_query(array('nid' => $nid)); $headers = array('Content-Type' => 'application/x-www-form-urlencoded'); global $base_url; $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php'; $client = \Drupal::service('http_client_factory')->fromOptions(['config/curl' => [CURLOPT_TIMEOUT => 10]]); $client->post($stats_path, array('headers' => $headers, 'body' => $post)); $statistics = statistics_get($node->id()); // Generate and test tokens. $tests = array(); $tests['[node:total-count]'] = 1; $tests['[node:day-count]'] = 1; $tests['[node:last-view]'] = format_date($statistics['timestamp']); $tests['[node:last-view:short]'] = format_date($statistics['timestamp'], 'short'); // Test to make sure that we generated something for each token. $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.'); foreach ($tests as $input => $expected) { $output = \Drupal::token()->replace($input, array('node' => $node), array('langcode' => $language_interface->getId())); $this->assertEqual($output, $expected, format_string('Statistics token %token replaced.', array('%token' => $input))); } }
/** * Test update changes configuration translations if enabled after language. */ public function testConfigTranslationImport() { // Add a language. The Afrikaans translation file of locale_test_translate // (test.af.po) has been prepared with a configuration translation. ConfigurableLanguage::createFromLangcode('af')->save(); // Enable locale module. $this->container->get('module_installer')->install(array('locale')); $this->resetAll(); // Enable import of translations. By default this is disabled for automated // tests. $this->config('locale.settings')->set('translation.import_enabled', TRUE)->save(); // Add translation permissions now that the locale module has been enabled. $edit = array('authenticated[translate interface]' => 'translate interface'); $this->drupalPostForm('admin/people/permissions', $edit, t('Save permissions')); // Check and update the translation status. This will import the Afrikaans // translations of locale_test_translate module. $this->drupalGet('admin/reports/translations/check'); // Override the Drupal core translation status to be up to date. // Drupal core should not be a subject in this test. $status = locale_translation_get_status(); $status['drupal']['af']->type = 'current'; \Drupal::state()->set('locale.translation_status', $status); $this->drupalPostForm('admin/reports/translations', array(), t('Update translations')); // Check if configuration translations have been imported. $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'system.maintenance'); $this->assertEqual($override->get('message'), 'Ons is tans besig met onderhoud op @site. Wees asseblief geduldig, ons sal binnekort weer terug wees.'); }
/** * Tests that translations for each language were loaded. */ public function testTranslationsLoaded() { // Verify German and Spanish were configured. $this->drupalGet('admin/config/regional/language'); $this->assertText('German'); $this->assertText('Spanish'); // If the installer was English, we expect that configured also. if ($this->langcode == 'en') { $this->assertText('English'); } // Verify the strings from the translation files were imported. $test_samples = array('Save and continue', 'Anonymous'); $languages = array('de' => 'German', 'es' => 'Spanish'); foreach ($test_samples as $sample) { foreach ($languages as $langcode => $name) { $edit = array(); $edit['langcode'] = $langcode; $edit['translation'] = 'translated'; $edit['string'] = $sample; $this->drupalPostForm('admin/config/regional/translate', $edit, t('Filter')); $this->assertText($sample . ' ' . $name); } } $config = \Drupal::languageManager()->getLanguageConfigOverride('de', 'user.settings'); $this->assertEqual($config->get('anonymous'), 'Anonymous German'); $config = \Drupal::languageManager()->getLanguageConfigOverride('es', 'user.settings'); $this->assertEqual($config->get('anonymous'), 'Anonymous Spanish'); }
/** * Overrides Drupal\Core\Entity\EntityForm::form(). */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); $this->getSubscriptionWidget($form_state)->setAvailableNewsletterIds(array_keys(simplenews_newsletter_get_visible())); /* @var \Drupal\simplenews\SubscriberInterface $subscriber */ $subscriber = $this->entity; $form['#title'] = $this->t('Edit subscriber @mail', array('@mail' => $subscriber->getMail())); $form['activated'] = array('#title' => t('Status'), '#type' => 'fieldset', '#description' => t('Active or inactive account.'), '#weight' => 15); $form['activated']['status'] = array('#type' => 'checkbox', '#title' => t('Active'), '#default_value' => $subscriber->getStatus()); $language_manager = \Drupal::languageManager(); if ($language_manager->isMultilingual()) { $languages = $language_manager->getLanguages(); foreach ($languages as $langcode => $language) { $language_options[$langcode] = $language->getName(); } $form['language'] = array('#type' => 'fieldset', '#title' => t('Preferred language'), '#description' => t('The e-mails will be localized in language chosen. Real users have their preference in account settings.'), '#disabled' => FALSE); if ($subscriber->getUserId()) { // Fallback if user has not defined a language. $form['language']['langcode'] = array('#type' => 'item', '#title' => t('User language'), '#markup' => $subscriber->language()->getName()); } else { $form['language']['langcode'] = array('#type' => 'select', '#default_value' => $subscriber->language()->getId(), '#options' => $language_options, '#required' => TRUE); } } return $form; }
/** * Overrides TypedData::getValue(). * * @return \Drupal\Core\Language\LanguageInterface|null */ public function getValue() { if (!isset($this->language) && $this->id) { $this->language = \Drupal::languageManager()->getLanguage($this->id); } return $this->language; }
/** * Tests creating a block type programmatically and via a form. */ public function testBlockContentTypeCreation() { // Login a test user. $this->drupalLogin($this->adminUser); // Test the page with no block-types. $this->drupalGet('block/add'); $this->assertResponse(200); $this->assertRaw(t('You have not created any block types yet. Go to the <a href="!url">block type creation page</a> to add a new block type.', ['!url' => Url::fromRoute('block_content.type_add')->toString()])); // Now create an initial block-type. $this->createBlockContentType('basic', TRUE); // Create a block type programmatically. $type = $this->createBlockContentType('other'); $block_type = BlockContentType::load('other'); $this->assertTrue($block_type, 'The new block type has been created.'); $this->drupalGet('block/add/' . $type->id()); $this->assertResponse(200, 'The new block type can be accessed at block/add.'); // Create a block type via the user interface. $edit = array('id' => 'foo', 'label' => 'title for foo'); $this->drupalPostForm('admin/structure/block/block-content/types/add', $edit, t('Save')); $block_type = BlockContentType::load('foo'); $this->assertTrue($block_type, 'The new block type has been created.'); $field_definitions = \Drupal::entityManager()->getFieldDefinitions('block_content', 'foo'); $this->assertTrue(isset($field_definitions['body']), 'Body field was created when using the UI to create block content types.'); // Check that the block type was created in site default language. $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId(); $this->assertEqual($block_type->language()->getId(), $default_langcode); }
/** * Tests creating a block type programmatically and via a form. */ public function testBlockContentTypeCreation() { // Login a test user. $this->drupalLogin($this->adminUser); // Test the page with no block-types. $this->drupalGet('block/add'); $this->assertResponse(200); $this->assertText('You have not created any block types yet'); $this->clickLink('block type creation page'); // Create a block type via the user interface. $edit = array('id' => 'foo', 'label' => 'title for foo'); $this->drupalPostForm(NULL, $edit, t('Save')); $block_type = BlockContentType::load('foo'); $this->assertTrue($block_type, 'The new block type has been created.'); $field_definitions = \Drupal::entityManager()->getFieldDefinitions('block_content', 'foo'); $this->assertTrue(isset($field_definitions['body']), 'Body field created when using the UI to create block content types.'); // Check that the block type was created in site default language. $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId(); $this->assertEqual($block_type->language()->getId(), $default_langcode); // Create block types programmatically. $this->createBlockContentType('basic', TRUE); $field_definitions = \Drupal::entityManager()->getFieldDefinitions('block_content', 'basic'); $this->assertTrue(isset($field_definitions['body']), "Body field for 'basic' block type created when using the testing API to create block content types."); $this->createBlockContentType('other'); $field_definitions = \Drupal::entityManager()->getFieldDefinitions('block_content', 'other'); $this->assertFalse(isset($field_definitions['body']), "Body field for 'other' block type not created when using the testing API to create block content types."); $block_type = BlockContentType::load('other'); $this->assertTrue($block_type, 'The new block type has been created.'); $this->drupalGet('block/add/' . $block_type->id()); $this->assertResponse(200); }
protected function setUp() { parent::setUp(); // Set up an additional language. $this->langcodes = array(\Drupal::languageManager()->getDefaultLanguage()->getId(), 'es'); ConfigurableLanguage::createFromLangcode('es')->save(); // Create a content type. $this->bundle = $this->randomMachineName(); $this->contentType = $this->drupalCreateContentType(array('type' => $this->bundle)); // Enable translation for the current entity type and ensure the change is // picked up. \Drupal::service('content_translation.manager')->setEnabled('node', $this->bundle, TRUE); drupal_static_reset(); \Drupal::entityManager()->clearCachedBundles(); \Drupal::service('router.builder')->rebuild(); // Add a translatable field to the content type. entity_create('field_storage_config', array('field_name' => 'field_test_text', 'entity_type' => 'node', 'type' => 'text', 'cardinality' => 1, 'translatable' => TRUE))->save(); entity_create('field_config', array('entity_type' => 'node', 'field_name' => 'field_test_text', 'bundle' => $this->bundle, 'label' => 'Test text-field'))->save(); entity_get_form_display('node', $this->bundle, 'default')->setComponent('field_test_text', array('type' => 'text_textfield', 'weight' => 0))->save(); // Enable content translation. ContentLanguageSettings::loadByEntityTypeBundle('node', $this->bundle)->setLanguageAlterable(TRUE)->setDefaultLangcode(\Drupal::languageManager()->getDefaultLanguage()->getId())->save(); // Create a translator user. $permissions = array('access contextual links', 'administer nodes', "edit any {$this->bundle} content", 'translate any entity'); $this->translator = $this->drupalCreateUser($permissions); }
/** * Tests if forum module uninstallation properly deletes the field. */ public function testForumUninstallWithField() { $this->drupalLogin($this->drupalCreateUser(['administer taxonomy', 'administer nodes', 'administer modules', 'delete any forum content', 'administer content types'])); // Ensure that the field exists before uninstallation. $field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums'); $this->assertNotNull($field_storage, 'The taxonomy_forums field storage exists.'); // Create a taxonomy term. $term = entity_create('taxonomy_term', array('name' => t('A term'), 'langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId(), 'description' => '', 'parent' => array(0), 'vid' => 'forums', 'forum_container' => 0)); $term->save(); // Create a forum node. $node = $this->drupalCreateNode(array('title' => 'A forum post', 'type' => 'forum', 'taxonomy_forums' => array(array('target_id' => $term->id())))); // Create at least one comment against the forum node. $comment = entity_create('comment', array('entity_id' => $node->nid->value, 'entity_type' => 'node', 'field_name' => 'comment_forum', 'pid' => 0, 'uid' => 0, 'status' => CommentInterface::PUBLISHED, 'subject' => $this->randomMachineName(), 'hostname' => '127.0.0.1')); $comment->save(); // Attempt to uninstall forum. $this->drupalGet('admin/modules/uninstall'); // Assert forum is required. $this->assertNoFieldByName('uninstall[forum]'); $this->drupalGet('admin/modules'); $this->assertText('To uninstall Forum first delete all Forum content'); // Delete the node. $this->drupalPostForm('node/' . $node->id() . '/delete', array(), t('Delete')); // Attempt to uninstall forum. $this->drupalGet('admin/modules/uninstall'); // Assert forum is still required. $this->assertNoFieldByName('uninstall[forum]'); $this->drupalGet('admin/modules'); $this->assertText('To uninstall Forum first delete all Forums terms'); // Delete any forum terms. $vid = $this->config('forum.settings')->get('vocabulary'); $terms = entity_load_multiple_by_properties('taxonomy_term', ['vid' => $vid]); foreach ($terms as $term) { $term->delete(); } // Ensure that the forum node type can not be deleted. $this->drupalGet('admin/structure/types/manage/forum'); $this->assertNoLink(t('Delete')); // Now attempt to uninstall forum. $this->drupalGet('admin/modules/uninstall'); // Assert forum is no longer required. $this->assertFieldByName('uninstall[forum]'); $this->drupalGet('admin/modules'); $this->assertNoText('To uninstall Forum first delete all Forum content'); $this->drupalPostForm('admin/modules/uninstall', array('uninstall[forum]' => 1), t('Uninstall')); $this->drupalPostForm(NULL, [], t('Uninstall')); // Check that the field is now deleted. $field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums'); $this->assertNull($field_storage, 'The taxonomy_forums field storage has been deleted.'); // Check that a node type with a machine name of forum can be created after // uninstalling the forum module and the node type is not locked. $edit = array('name' => 'Forum', 'title_label' => 'title for forum', 'type' => 'forum'); $this->drupalPostForm('admin/structure/types/add', $edit, t('Save content type')); $this->assertTrue((bool) NodeType::load('forum'), 'Node type with machine forum created.'); $this->drupalGet('admin/structure/types/manage/forum'); $this->clickLink(t('Delete')); $this->drupalPostForm(NULL, array(), t('Delete')); $this->assertResponse(200); $this->assertFalse((bool) NodeType::load('forum'), 'Node type with machine forum deleted.'); }
public function __construct() { $this->initializeValuesFromConfig(); $this->is_https = \Drupal::request()->isSecure(); $this->request_method = \Drupal::request()->getMethod(); $this->path = \Drupal::service('path.current')->getPath() ? \Drupal::service('path.current')->getPath() : ''; $this->langcode = \Drupal::languageManager()->getCurrentLanguage()->getId(); }
/** * Constructs a date object. * * @param string $time * A DateTime object, a date/input_time_adjusted string, a unix timestamp. * Defaults to 'now'. * @param mixed $timezone * PHP DateTimeZone object, string or NULL allowed. * Defaults to NULL. * @param array $settings * - validate_format: (optional) Boolean choice to validate the * created date using the input format. The format used in * createFromFormat() allows slightly different values than format(). * Using an input format that works in both functions makes it * possible to a validation step to confirm that the date created * from a format string exactly matches the input. This option * indicates the format can be used for validation. Defaults to TRUE. * - langcode: (optional) Used to control the result of the format() method. * Defaults to NULL. * - debug: (optional) Boolean choice to leave debug values in the * date object for debugging purposes. Defaults to FALSE. */ public function __construct($time = 'now', $timezone = NULL, $settings = array()) { if (!isset($settings['langcode'])) { $settings['langcode'] = \Drupal::languageManager()->getCurrentLanguage()->getId(); } // Instantiate the parent class. parent::__construct($time, $timezone, $settings); }
public function setUp() { parent::setUp(); // Install default system configuration. $this->installConfig(array('system')); $this->interfaceLanguage = \Drupal::languageManager()->getCurrentLanguage(); $this->tokenService = \Drupal::token(); }
/** * {@inheritdoc} */ public function view(FieldItemListInterface $items, $langcode = NULL) { // Default the language to the current content language. if (empty($langcode)) { $langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId(); } return $this->viewElements($items, $langcode); }
/** * Perform alterations on language switcher links. * * A language switcher link may need to point to a different path or use a * translated link text before going through l(), which will just handle the * path aliases. * * @param $links * Nested array of links keyed by language code. * @param $type * The language type the links will switch. * @param $path * The current path. */ function hook_language_switch_links_alter(array &$links, $type, $path) { $language_interface = \Drupal::languageManager()->getCurrentLanguage(); if ($type == LanguageInterface::TYPE_CONTENT && isset($links[$language_interface->id])) { foreach ($links[$language_interface->id] as $link) { $link['attributes']['class'][] = 'active-language'; } } }
/** * @inheritDoc */ public function buildHeader() { // Enable language column and filter if multiple languages are added. $header = array('fullname' => $this->t('Full name'), 'shortname' => array('data' => $this->t('Short name'), 'class' => array(RESPONSIVE_PRIORITY_LOW)), 'category' => array('data' => $this->t('Category'), 'class' => array(RESPONSIVE_PRIORITY_LOW)), 'timemodified' => array('data' => $this->t('Updated'), 'class' => array(RESPONSIVE_PRIORITY_LOW))); if (\Drupal::languageManager()->isMultilingual()) { $header['lang'] = array('data' => $this->t('Language'), 'class' => array(RESPONSIVE_PRIORITY_LOW)); } return $header; }
protected function setUp() { parent::setUp(); // Install default system configuration. $this->installConfig(array('system')); \Drupal::service('router.builder')->rebuild(); $this->interfaceLanguage = \Drupal::languageManager()->getCurrentLanguage(); $this->tokenService = \Drupal::token(); }