function setUp()
 {
     parent::setUp();
     $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
     // Create and login user.
     $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer languages', 'access administration pages', 'administer content types', 'administer comments', 'create article content', 'access comments', 'post comments', 'skip comment approval'));
     $this->drupalLogin($admin_user);
     // Add language.
     $edit = array('predefined_langcode' => 'fr');
     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
     // Set "Article" content type to use multilingual support.
     $edit = array('language_configuration[language_show]' => TRUE);
     $this->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type'));
     // Enable content language negotiation UI.
     \Drupal::state()->set('language_test.content_language_type', TRUE);
     // Set interface language detection to user and content language detection
     // to URL. Disable inheritance from interface language to ensure content
     // language will fall back to the default language if no URL language can be
     // detected.
     $edit = array('language_interface[enabled][language-user]' => TRUE, 'language_content[enabled][language-url]' => TRUE, 'language_content[enabled][language-interface]' => FALSE);
     $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
     // Change user language preference, this way interface language is always
     // French no matter what path prefix the URLs have.
     $edit = array('preferred_langcode' => 'fr');
     $this->drupalPostForm("user/" . $admin_user->id() . "/edit", $edit, t('Save'));
     // Create comment field on article.
     $this->container->get('comment.manager')->addDefaultField('node', 'article');
     // Make comment body translatable.
     $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
     $field_storage->translatable = TRUE;
     $field_storage->save();
     $this->assertTrue($field_storage->isTranslatable(), 'Comment body is translatable.');
 }
 function setUp()
 {
     parent::setUp();
     $this->drupalLogin($this->drupalCreateUser(['access comments']));
     // Add two new languages.
     ConfigurableLanguage::createFromLangcode('fr')->save();
     ConfigurableLanguage::createFromLangcode('es')->save();
     // Make the comment body field translatable. The title is already
     // translatable by definition.
     $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
     $field_storage->translatable = TRUE;
     $field_storage->save();
     // Set up comment titles.
     $this->comment_titles = array('en' => 'Food in Paris', 'es' => 'Comida en Paris', 'fr' => 'Nouriture en Paris');
     // Create a new comment. Using the one created earlier will not work,
     // as it predates the language set-up.
     $comment = array('uid' => $this->loggedInUser->id(), 'entity_id' => $this->node_user_commented->id(), 'entity_type' => 'node', 'field_name' => 'comment', 'cid' => '', 'pid' => '', 'node_type' => '');
     $this->comment = entity_create('comment', $comment);
     // Add field values and translate the comment.
     $this->comment->subject->value = $this->comment_titles['en'];
     $this->comment->comment_body->value = $this->comment_titles['en'];
     $this->comment->langcode = 'en';
     $this->comment->save();
     foreach (array('es', 'fr') as $langcode) {
         $translation = $this->comment->addTranslation($langcode, array());
         $translation->comment_body->value = $this->comment_titles[$langcode];
         $translation->subject->value = $this->comment_titles[$langcode];
     }
     $this->comment->save();
 }
  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->adminUser = $this->drupalCreateUser([
      'administer products',
      'administer stores',
      'access administration pages',
    ]);
    $this->drupalLogin($this->adminUser);

    $this->referenceField = FieldStorageConfig::loadByName('commerce_product', 'stores');
    $display = EntityFormDisplay::load('commerce_product.default.default');
    $display->setComponent('stores', [
      'type' => 'entity_select',
      'settings' => [
        'autocomplete_threshold' => 2,
      ],
    ])->save();

    $variation = $this->createEntity('commerce_product_variation', [
     'type' => 'default',
      'sku' => strtolower($this->randomMachineName()),
    ]);
    $this->product = $this->createEntity('commerce_product', [
      'type' => 'default',
      'title' => $this->randomMachineName(),
      'variations' => [$variation],
    ]);
  }
 /**
  * Tests that importing list_float fields works.
  */
 public function testImport()
 {
     $field_name = 'field_options_float';
     $type = 'options_install_test';
     // Test the results on installing options_config_install_test. All the
     // necessary configuration for this test is created by installing that
     // module.
     $field_storage = FieldStorageConfig::loadByName('node', $field_name);
     $this->assertIdentical($field_storage->getSetting('allowed_values'), $array = array('0' => 'Zero', '0.5' => 'Point five'));
     $admin_path = 'admin/structure/types/manage/' . $type . '/fields/node.' . $type . '.' . $field_name . '/storage';
     // Export active config to sync.
     $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
     // Set the active to not use dots in the allowed values key names.
     $edit = array('settings[allowed_values]' => "0|Zero\n1|One");
     $this->drupalPostForm($admin_path, $edit, t('Save field settings'));
     $field_storage = FieldStorageConfig::loadByName('node', $field_name);
     $this->assertIdentical($field_storage->getSetting('allowed_values'), $array = array('0' => 'Zero', '1' => 'One'));
     // Import configuration with dots in the allowed values key names. This
     // tests \Drupal\Core\Config\Entity\ConfigEntityStorage::importUpdate().
     $this->drupalGet('admin/config/development/configuration');
     $this->drupalPostForm(NULL, array(), t('Import all'));
     $field_storage = FieldStorageConfig::loadByName('node', $field_name);
     $this->assertIdentical($field_storage->getSetting('allowed_values'), $array = array('0' => 'Zero', '0.5' => 'Point five'));
     // Delete field to test creation. This tests
     // \Drupal\Core\Config\Entity\ConfigEntityStorage::importCreate().
     FieldConfig::loadByName('node', $type, $field_name)->delete();
     $this->drupalGet('admin/config/development/configuration');
     $this->drupalPostForm(NULL, array(), t('Import all'));
     $field_storage = FieldStorageConfig::loadByName('node', $field_name);
     $this->assertIdentical($field_storage->getSetting('allowed_values'), $array = array('0' => 'Zero', '0.5' => 'Point five'));
 }
 /**
  * Tests entity form language.
  */
 function testEntityFormLanguage()
 {
     $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
     $web_user = $this->drupalCreateUser(array('create page content', 'edit own page content', 'administer content types'));
     $this->drupalLogin($web_user);
     // Create a node with language LanguageInterface::LANGCODE_NOT_SPECIFIED.
     $edit = array();
     $edit['title[0][value]'] = $this->randomMachineName(8);
     $edit['body[0][value]'] = $this->randomMachineName(16);
     $this->drupalGet('node/add/page');
     $form_langcode = \Drupal::state()->get('entity_test.form_langcode');
     $this->drupalPostForm(NULL, $edit, t('Save'));
     $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
     $this->assertTrue($node->language()->getId() == $form_langcode, 'Form language is the same as the entity language.');
     // Edit the node and test the form language.
     $this->drupalGet($this->langcodes[0] . '/node/' . $node->id() . '/edit');
     $form_langcode = \Drupal::state()->get('entity_test.form_langcode');
     $this->assertTrue($node->language()->getId() == $form_langcode, 'Form language is the same as the entity language.');
     // Explicitly set form langcode.
     $langcode = $this->langcodes[0];
     $form_state_additions['langcode'] = $langcode;
     \Drupal::service('entity.form_builder')->getForm($node, 'default', $form_state_additions);
     $form_langcode = \Drupal::state()->get('entity_test.form_langcode');
     $this->assertTrue($langcode == $form_langcode, 'Form language is the same as the language parameter.');
     // Enable language selector.
     $this->drupalGet('admin/structure/types/manage/page');
     $edit = array('language_configuration[language_alterable]' => TRUE, 'language_configuration[langcode]' => LanguageInterface::LANGCODE_NOT_SPECIFIED);
     $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));
     $this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Basic page')), 'Basic page content type has been updated.');
     // Create a node with language.
     $edit = array();
     $langcode = $this->langcodes[0];
     $edit['title[0][value]'] = $this->randomMachineName(8);
     $edit['body[0][value]'] = $this->randomMachineName(16);
     $edit['langcode[0][value]'] = $langcode;
     $this->drupalPostForm('node/add/page', $edit, t('Save'));
     $this->assertText(t('Basic page @title has been created.', array('@title' => $edit['title[0][value]'])), 'Basic page created.');
     // Verify that the creation message contains a link to a node.
     $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', array(':href' => 'node/'));
     $this->assert(isset($view_link), 'The message area contains a link to a node');
     // Check to make sure the node was created.
     $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
     $this->assertTrue($node, 'Node found in database.');
     // Make body translatable.
     $field_storage = FieldStorageConfig::loadByName('node', 'body');
     $field_storage->setTranslatable(TRUE);
     $field_storage->save();
     $field_storage = FieldStorageConfig::loadByName('node', 'body');
     $this->assertTrue($field_storage->isTranslatable(), 'Field body is translatable.');
     // Create a body translation and check the form language.
     $langcode2 = $this->langcodes[1];
     $translation = $node->addTranslation($langcode2);
     $translation->title->value = $this->randomString();
     $translation->body->value = $this->randomMachineName(16);
     $translation->setOwnerId($web_user->id());
     $node->save();
     $this->drupalGet($langcode2 . '/node/' . $node->id() . '/edit');
     $form_langcode = \Drupal::state()->get('entity_test.form_langcode');
     $this->assertTrue($langcode2 == $form_langcode, "Node edit form language is {$langcode2}.");
 }
Esempio n. 6
0
 /**
  * Tests for the presence of nodes on the global tracker listing.
  */
 function testTrackerAll()
 {
     $this->drupalLogin($this->user);
     $unpublished = $this->drupalCreateNode(array('title' => $this->randomMachineName(8), 'status' => 0));
     $published = $this->drupalCreateNode(array('title' => $this->randomMachineName(8), 'status' => 1));
     $this->drupalGet('activity');
     $this->assertNoText($unpublished->label(), 'Unpublished node does not show up in the tracker listing.');
     $this->assertText($published->label(), 'Published node shows up in the tracker listing.');
     $this->assertLink(t('My recent content'), 0, 'User tab shows up on the global tracker page.');
     // Assert cache contexts, specifically the pager and node access contexts.
     $this->assertCacheContexts(['languages:language_interface', 'theme', 'url.query_args.pagers:0', 'user.node_grants:view', 'user.permissions']);
     // Assert cache tags for the visible node and node list cache tag.
     $this->assertCacheTags(Cache::mergeTags($published->getCacheTags(), $published->getOwner()->getCacheTags(), ['node_list', 'rendered']));
     // Delete a node and ensure it no longer appears on the tracker.
     $published->delete();
     $this->drupalGet('activity');
     $this->assertNoText($published->label(), 'Deleted node does not show up in the tracker listing.');
     // Test proper display of time on activity page when comments are disabled.
     // Disable comments.
     FieldStorageConfig::loadByName('node', 'comment')->delete();
     $node = $this->drupalCreateNode(['title' => 'testing_node_presave', 'status' => 1]);
     $this->drupalGet('activity');
     $this->assertText($node->label(), 'Published node shows up in the tracker listing.');
     $this->assertText(\Drupal::service('date.formatter')->formatTimeDiffSince($node->getChangedTime()), 'The changed time was displayed on the tracker listing.');
 }
 function setUp()
 {
     parent::setUp();
     // Create and login user.
     $test_user = $this->drupalCreateUser(array('access content', 'search content', 'use advanced search', 'administer nodes', 'administer languages', 'access administration pages', 'administer site configuration'));
     $this->drupalLogin($test_user);
     // Add a new language.
     $language = new Language(array('id' => 'es', 'name' => 'Spanish'));
     language_save($language);
     // Make the body field translatable. The title is already translatable by
     // definition. The parent class has already created the article and page
     // content types.
     $field_storage = FieldStorageConfig::loadByName('node', 'body');
     $field_storage->translatable = TRUE;
     $field_storage->save();
     // Create a few page nodes with multilingual body values.
     $default_format = filter_default_format();
     $nodes = array(array('title' => 'First node en', 'type' => 'page', 'body' => array(array('value' => $this->randomMachineName(32), 'format' => $default_format)), 'langcode' => 'en'), array('title' => 'Second node this is the Spanish title', 'type' => 'page', 'body' => array(array('value' => $this->randomMachineName(32), 'format' => $default_format)), 'langcode' => 'es'), array('title' => 'Third node en', 'type' => 'page', 'body' => array(array('value' => $this->randomMachineName(32), 'format' => $default_format)), 'langcode' => 'en'));
     $this->searchable_nodes = array();
     foreach ($nodes as $setting) {
         $this->searchable_nodes[] = $this->drupalCreateNode($setting);
     }
     // Add English translation to the second node.
     $translation = $this->searchable_nodes[1]->addTranslation('en', array('title' => 'Second node en'));
     $translation->body->value = $this->randomMachineName(32);
     $this->searchable_nodes[1]->save();
     // Add Spanish translation to the third node.
     $translation = $this->searchable_nodes[2]->addTranslation('es', array('title' => 'Third node es'));
     $translation->body->value = $this->randomMachineName(32);
     $this->searchable_nodes[2]->save();
     // Update the index and then run the shutdown method.
     $plugin = $this->container->get('plugin.manager.search')->createInstance('node_search');
     $plugin->updateIndex();
     search_update_totals();
 }
Esempio n. 8
0
 /**
  * {@inheritdoc}
  */
 public function setUp()
 {
     parent::setUp();
     // Create content types.
     $this->drupalCreateContentType(['type' => 'page']);
     $this->drupalCreateContentType(['type' => 'article']);
     // Add a new language.
     ConfigurableLanguage::createFromLangcode('es')->save();
     // Make the body field translatable. The title is already translatable by
     // definition. The parent class has already created the article and page
     // content types.
     $field_storage = FieldStorageConfig::loadByName('node', 'body');
     $field_storage->setTranslatable(TRUE);
     $field_storage->save();
     // Add 9 nodes of the type page.
     for ($i = 1; $i <= 9; $i++) {
         // Adding a different created time per language to avoid to have exactly
         // the same value per nid and langcode.
         $created_time_en = new \DateTime('February ' . $i . ' 2016 ' . str_pad($i, 2, STR_PAD_LEFT, 0) . 'PM');
         $created_time_es = new \DateTime('March ' . $i . ' 2016 ' . str_pad($i, 2, STR_PAD_LEFT, 0) . 'PM');
         $node = $this->drupalCreateNode(array('title' => 'test page' . $i . ' EN', 'body' => 'test page' . $i, 'type' => 'page', 'created' => $created_time_en->format('U'), 'langcode' => 'en'));
         // Add Spanish translation to the node.
         $node->addTranslation('es', ['title' => 'test page' . $i . ' ES', 'created' => $created_time_es->format('U')]);
         $node->save();
     }
     $created_time = new \DateTime('March 9 2016 11PM');
     $this->drupalCreateNode(array('title' => 'test page 10 EN', 'body' => 'test page10', 'type' => 'page', 'created' => $created_time->format('U'), 'langcode' => 'en'));
     // Add 10 nodes of the type article.
     for ($i = 1; $i <= 10; $i++) {
         $created_time = new \DateTime('April ' . $i . ' 2016 ' . str_pad($i, 2, STR_PAD_LEFT, 0) . 'PM');
         $this->drupalCreateNode(array('title' => 'test article' . $i . ' EN', 'body' => 'test article' . $i, 'type' => 'article', 'created' => $created_time->format('U'), 'langcode' => 'en'));
     }
     // Create the users used for the tests.
     $this->adminUser = $this->drupalCreateUser(['administer search', 'use advanced search', 'administer facets', 'access administration pages', 'administer nodes', 'access content overview', 'administer content types', 'administer blocks', 'search content', 'administer languages', 'administer site configuration', 'access content']);
 }
 function setUp()
 {
     parent::setUp();
     // Create Page content type.
     if ($this->profile != 'standard') {
         $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
     }
     // Add two new languages.
     ConfigurableLanguage::createFromLangcode('fr')->save();
     ConfigurableLanguage::createFromLangcode('es')->save();
     // Make the body field translatable. The title is already translatable by
     // definition.
     $field_storage = FieldStorageConfig::loadByName('node', 'body');
     $field_storage->translatable = TRUE;
     $field_storage->save();
     // Set up node titles.
     $this->node_titles = array('en' => 'Food in Paris', 'es' => 'Comida en Paris', 'fr' => 'Nouriture en Paris');
     // Create node with translations.
     $node = $this->drupalCreateNode(array('title' => $this->node_titles['en'], 'langcode' => 'en', 'type' => 'page', 'body' => array(array('value' => $this->node_titles['en']))));
     foreach (array('es', 'fr') as $langcode) {
         $translation = $node->addTranslation($langcode, array('title' => $this->node_titles[$langcode]));
         $translation->body->value = $this->node_titles[$langcode];
     }
     $node->save();
 }
 /**
  * Tests the calculateDependencies method.
  */
 public function testCalculateDependencies()
 {
     $comment_type = entity_create('comment_type', array('id' => 'comment', 'label' => 'Comment settings', 'description' => 'Comment settings', 'target_entity_type_id' => 'node'));
     $comment_type->save();
     $content_type = entity_create('node_type', array('type' => $this->randomMachineName(), 'name' => $this->randomString()));
     $content_type->save();
     $field_storage = entity_create('field_storage_config', array('field_name' => Unicode::strtolower($this->randomMachineName()), 'entity_type' => 'node', 'type' => 'comment'));
     $field_storage->save();
     entity_create('field_config', array('field_storage' => $field_storage, 'bundle' => $content_type->id(), 'label' => $this->randomMachineName() . '_label', 'description' => $this->randomMachineName() . '_description', 'settings' => array('comment_type' => $comment_type->id())))->save();
     entity_create('field_config', array('field_storage' => FieldStorageConfig::loadByName('node', 'body'), 'bundle' => $content_type->id(), 'label' => $this->randomMachineName() . '_body', 'settings' => array('display_summary' => TRUE)))->save();
     $expected = [];
     $expected['test_field_get_entity'] = ['module' => ['comment', 'node', 'user']];
     // Tests dependencies of relationships.
     $expected['test_relationship_dependency'] = ['module' => ['comment', 'node', 'user']];
     $expected['test_plugin_dependencies'] = ['module' => ['comment', 'views_test_data'], 'content' => ['RowTest', 'StaticTest', 'StyleTest']];
     $expected['test_argument_dependency'] = ['config' => ['core.entity_view_mode.node.teaser', 'field.storage.node.body'], 'content' => ['ArgumentDefaultTest', 'ArgumentValidatorTest'], 'module' => ['node', 'search', 'text', 'user']];
     foreach ($this::$testViews as $view_id) {
         $view = Views::getView($view_id);
         $dependencies = $view->calculateDependencies();
         $this->assertEqual($expected[$view_id], $dependencies);
         $config = $this->config('views.view.' . $view_id);
         \Drupal::service('config.storage.staging')->write($view_id, $config->get());
     }
     // Ensure that dependencies are calculated on the display level.
     $expected_display['default'] = ['config' => ['core.entity_view_mode.node.teaser'], 'content' => ['ArgumentDefaultTest', 'ArgumentValidatorTest'], 'module' => ['core', 'node', 'search', 'user', 'views']];
     $expected_display['page'] = ['config' => ['field.storage.node.body'], 'module' => ['core', 'text', 'views']];
     $view = Views::getView('test_argument_dependency');
     $view->initDisplay();
     foreach ($view->displayHandlers as $display) {
         // Calculate the dependencies each display has.
         $this->assertEqual($expected_display[$display->getPluginId()], $display->calculateDependencies());
     }
 }
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     // Create Page content type.
     if ($this->profile != 'standard') {
         $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
     }
     // Add two new languages.
     ConfigurableLanguage::createFromLangcode('fr')->save();
     ConfigurableLanguage::createFromLangcode('es')->save();
     // Make the body field translatable. The title is already translatable by
     // definition.
     $field_storage = FieldStorageConfig::loadByName('node', 'body');
     $field_storage->translatable = TRUE;
     $field_storage->save();
     // Set up node titles. They should not include the words "French",
     // "English", or "Spanish", as there is a language field in the view
     // that prints out those words.
     $this->node_titles = array('es' => array('Primero nodo es', 'Segundo nodo es', 'Tercera nodo es'), 'en' => array('First node en', 'Second node en'), 'fr' => array('Premier nœud fr'));
     // Create nodes with translations.
     foreach ($this->node_titles['es'] as $index => $title) {
         $node = $this->drupalCreateNode(array('title' => $title, 'langcode' => 'es', 'type' => 'page', 'promote' => 1));
         foreach (array('en', 'fr') as $langcode) {
             if (isset($this->node_titles[$langcode][$index])) {
                 $translation = $node->addTranslation($langcode, array('title' => $this->node_titles[$langcode][$index]));
                 $translation->body->value = $this->randomMachineName(32);
             }
         }
         $node->save();
     }
     $user = $this->drupalCreateUser(array('access content overview', 'access content'));
     $this->drupalLogin($user);
 }
 /**
  * Displays links to all products that have not been categorized.
  *
  * @return
  *   Renderable form array.
  */
 public function orphans()
 {
     $build = array();
     if ($this->config('taxonomy.settings')->get('maintain_index_table')) {
         $vid = $this->config('uc_catalog.settings')->get('vocabulary');
         $product_types = uc_product_types();
         $field = FieldStorageConfig::loadByName('node', 'taxonomy_catalog');
         //@todo - figure this out
         // $field is a config object, not an array, so this doesn't work.
         //$types = array_intersect($product_types, $field['bundles']['node']);
         $types = $product_types;
         //temporary to get this to work at all
         $result = db_query('SELECT DISTINCT n.nid, n.title FROM {node_field_data} n LEFT JOIN (SELECT ti.nid, td.vid FROM {taxonomy_index} ti LEFT JOIN {taxonomy_term_data} td ON ti.tid = td.tid WHERE td.vid = :vid) txnome ON n.nid = txnome.nid WHERE n.type IN (:types[]) AND txnome.vid IS NULL', [':vid' => $vid, ':types[]' => $types]);
         $rows = array();
         while ($node = $result->fetchObject()) {
             $rows[] = $this->l($node->title, Url::fromRoute('entity.node.edit_form', ['node' => $node->nid], ['query' => ['destination' => 'admin/store/products/orphans']]));
         }
         if (count($rows) > 0) {
             $build['orphans'] = array('#theme' => 'item_list', '#items' => $rows);
         } else {
             $build['orphans'] = array('#markup' => $this->t('All products are currently listed in the catalog.'), '#prefix' => '<p>', '#suffix' => '</p>');
         }
     } else {
         $build['orphans'] = array('#markup' => $this->t('The node terms index is not being maintained, so Ubercart can not determine which products are not entered into the catalog.'), '#prefix' => '<p>', '#suffix' => '</p>');
     }
     return $build;
 }
 /**
  * Overrides \Drupal\content_translation\Tests\ContentTranslationUITest::setupTestFields().
  */
 function setupTestFields()
 {
     parent::setupTestFields();
     $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
     $field_storage->translatable = TRUE;
     $field_storage->save();
 }
Esempio n. 14
0
 function testUserTokens()
 {
     // Add a user picture to the account.
     $image = current($this->drupalGetTestFiles('image'));
     $edit = array('files[user_picture_0]' => drupal_realpath($image->uri));
     $this->drupalPostForm('user/' . $this->account->id() . '/edit', $edit, t('Save'));
     $storage = \Drupal::entityManager()->getStorage('user');
     // Load actual user data from database.
     $storage->resetCache();
     $this->account = $storage->load($this->account->id());
     $this->assertTrue(!empty($this->account->user_picture->target_id), 'User picture uploaded.');
     $picture = ['#theme' => 'user_picture', '#account' => $this->account];
     /** @var \Drupal\Core\Render\RendererInterface $renderer */
     $renderer = \Drupal::service('renderer');
     $user_tokens = array('picture' => $renderer->render($picture), 'picture:fid' => $this->account->user_picture->target_id, 'picture:size-raw' => 125, 'ip-address' => NULL, 'roles' => implode(', ', $this->account->getRoles()));
     $this->assertTokens('user', array('user' => $this->account), $user_tokens);
     // Remove the simpletest-created user role.
     $roles = $this->account->getRoles();
     $this->account->removeRole(end($roles));
     $this->account->save();
     // Remove the user picture field and reload the user.
     FieldStorageConfig::loadByName('user', 'user_picture')->delete();
     $storage->resetCache();
     $this->account = $storage->load($this->account->id());
     $user_tokens = array('picture' => NULL, 'picture:fid' => NULL, 'ip-address' => NULL, 'roles' => 'authenticated', 'roles:keys' => (string) DRUPAL_AUTHENTICATED_RID);
     $this->assertTokens('user', array('user' => $this->account), $user_tokens);
     // The ip address token should work for the current user token type.
     $tokens = array('ip-address' => \Drupal::request()->getClientIp());
     $this->assertTokens('current-user', array(), $tokens);
     $anonymous = new AnonymousUserSession();
     $tokens = array('roles' => 'anonymous', 'roles:keys' => (string) DRUPAL_ANONYMOUS_RID);
     $this->assertTokens('user', array('user' => $anonymous), $tokens);
 }
Esempio n. 15
0
 /**
  * {@inheritdoc}
  */
 function setUp()
 {
     parent::setUp();
     // Create Page content type.
     if ($this->profile != 'standard') {
         $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
     }
     // Add two new languages.
     $language = new Language(array('id' => 'fr', 'name' => 'French'));
     language_save($language);
     $language = new Language(array('id' => 'es', 'name' => 'Spanish'));
     language_save($language);
     // Make the body field translatable. The title is already translatable by
     // definition.
     $field = FieldStorageConfig::loadByName('node', 'body');
     $field->translatable = TRUE;
     $field->save();
     // Set up node titles. They should not include the words "French",
     // "English", or "Spanish", as there is a language field in the view
     // that prints out those words.
     $this->node_titles = array('en' => array('First node en', 'Second node en'), 'es' => array('Primero nodo es', 'Segundo nodo es'), 'fr' => array('Premier nodule fr'));
     // Create nodes with translations.
     foreach ($this->node_titles['en'] as $index => $title) {
         $node = $this->drupalCreateNode(array('title' => $title, 'langcode' => 'en', 'type' => 'page'));
         foreach (array('es', 'fr') as $langcode) {
             if (isset($this->node_titles[$langcode][$index])) {
                 $translation = $node->addTranslation($langcode, array('title' => $this->node_titles[$langcode][$index]));
                 $translation->body->value = $this->randomName(32);
             }
         }
         $node->save();
     }
 }
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     $this->referenceField = FieldStorageConfig::loadByName('commerce_product', 'stores');
     $display = EntityFormDisplay::load('commerce_product.default.default');
     $display->setComponent('stores', ['type' => 'commerce_entity_select', 'settings' => ['autocomplete_threshold' => 2]])->save();
     $variation = $this->createEntity('commerce_product_variation', ['type' => 'default', 'sku' => strtolower($this->randomMachineName())]);
     $this->product = $this->createEntity('commerce_product', ['type' => 'default', 'title' => $this->randomMachineName(), 'variations' => [$variation]]);
 }
Esempio n. 17
0
 /**
  * Creates a field of an entity reference field storage on the specified bundle.
  *
  * @param string $entity_type
  *   The type of entity the field will be attached to.
  * @param string $bundle
  *   The bundle name of the entity the field will be attached to.
  * @param string $field_name
  *   The name of the field; if it already exists, a new instance of the existing
  *   field will be created.
  * @param string $field_label
  *   The label of the field.
  * @param string $target_entity_type
  *   The type of the referenced entity.
  * @param string $selection_handler
  *   The selection handler used by this field.
  * @param array $selection_handler_settings
  *   An array of settings supported by the selection handler specified above.
  *   (e.g. 'target_bundles', 'sort', 'auto_create', etc).
  * @param int $cardinality
  *   The cardinality of the field.
  *
  * @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase::buildConfigurationForm()
  */
 protected function createEntityReferenceField($entity_type, $bundle, $field_name, $field_label, $target_entity_type, $selection_handler = 'default', $selection_handler_settings = array(), $cardinality = 1)
 {
     // Look for or add the specified field to the requested entity bundle.
     if (!FieldStorageConfig::loadByName($entity_type, $field_name)) {
         FieldStorageConfig::create(array('field_name' => $field_name, 'type' => 'entity_reference', 'entity_type' => $entity_type, 'cardinality' => $cardinality, 'settings' => array('target_type' => $target_entity_type)))->save();
     }
     if (!FieldConfig::loadByName($entity_type, $bundle, $field_name)) {
         FieldConfig::create(array('field_name' => $field_name, 'entity_type' => $entity_type, 'bundle' => $bundle, 'label' => $field_label, 'settings' => array('handler' => $selection_handler, 'handler_settings' => $selection_handler_settings)))->save();
     }
 }
 /**
  * Adds term reference field for the article content type.
  *
  * @param bool $translatable
  *   (optional) If TRUE, create a translatable term reference field. Defaults
  *   to FALSE.
  */
 protected function setUpTermReferenceField()
 {
     $handler_settings = array('target_bundles' => array($this->vocabulary->id() => $this->vocabulary->id()), 'auto_create' => TRUE);
     $this->createEntityReferenceField('node', 'article', $this->termFieldName, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
     $field_storage = FieldStorageConfig::loadByName('node', $this->termFieldName);
     $field_storage->setTranslatable(FALSE);
     $field_storage->save();
     entity_get_form_display('node', 'article', 'default')->setComponent($this->termFieldName, array('type' => 'entity_reference_autocomplete_tags'))->save();
     entity_get_display('node', 'article', 'default')->setComponent($this->termFieldName, array('type' => 'entity_reference_label'))->save();
 }
Esempio n. 19
0
 /**
  * Tests uninstallation if the field storage has been deleted beforehand.
  */
 function testForumUninstallWithoutFieldStorage()
 {
     // Manually delete the taxonomy_forums field before module uninstallation.
     $field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums');
     $this->assertNotNull($field_storage, 'The taxonomy_forums field storage exists.');
     $field_storage->delete();
     // 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.');
     // Ensure that uninstallation succeeds even if the field has already been
     // deleted manually beforehand.
     $this->container->get('module_handler')->uninstall(array('forum'));
 }
 /**
  * {@inheritdoc}
  */
 public function ensureMyTable()
 {
     $field = FieldStorageConfig::loadByName($this->definition['target entity type'], $this->definition['field name']);
     $cardinality = $field->getCardinality();
     if (!isset($this->tableAlias)) {
         $join = $this->getJoin();
         if ($this->options['delta'] != -1 && $cardinality) {
             $join->extra[] = array('field' => 'delta', 'value' => $this->options['delta'], 'numeric' => TRUE);
         }
         $this->tableAlias = $this->query->ensureTable($this->table, $this->relationship, $join);
         return $this->tableAlias;
     }
 }
 /**
  * Tests if uninstallation succeeds if the field has been deleted beforehand.
  */
 function testCommentUninstallWithoutField()
 {
     // Manually delete the comment_body field before module uninstallation.
     $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
     $this->assertNotNull($field_storage, 'The comment_body field exists.');
     $field_storage->delete();
     // Check that the field is now deleted.
     $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
     $this->assertNull($field_storage, 'The comment_body field has been deleted.');
     // Ensure that uninstallation succeeds even if the field has already been
     // deleted manually beforehand.
     $this->container->get('module_handler')->uninstall(array('comment'));
 }
 /**
  * #post_render_cache callback; replaces placeholder with comment form.
  *
  * @param array $element
  *   The renderable array that contains the to be replaced placeholder.
  * @param array $context
  *   An array with the following keys:
  *   - entity_type: an entity type
  *   - entity_id: an entity ID
  *   - field_name: a comment field name
  *
  * @return array
  *   A renderable array containing the comment form.
  */
 public function renderForm(array $element, array $context)
 {
     $field_name = $context['field_name'];
     $entity = $this->entityManager->getStorage($context['entity_type'])->load($context['entity_id']);
     $field_storage = FieldStorageConfig::loadByName($entity->getEntityTypeId(), $field_name);
     $values = array('entity_type' => $entity->getEntityTypeId(), 'entity_id' => $entity->id(), 'field_name' => $field_name, 'comment_type' => $field_storage->getSetting('bundle'), 'pid' => NULL);
     $comment = $this->entityManager->getStorage('comment')->create($values);
     $form = $this->entityFormBuilder->getForm($comment);
     $markup = drupal_render($form);
     $callback = 'comment.post_render_cache:renderForm';
     $placeholder = drupal_render_cache_generate_placeholder($callback, $context);
     $element['#markup'] = str_replace($placeholder, $markup, $element['#markup']);
     $element['#attached'] = drupal_merge_attached($element['#attached'], $form['#attached']);
     return $element;
 }
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp(FALSE);
     // Install the necessary dependencies for node type creation to work.
     $this->installEntitySchema('node');
     $this->installConfig(array('field', 'node'));
     $comment_type = entity_create('comment_type', array('id' => 'comment', 'label' => 'Comment settings', 'description' => 'Comment settings', 'target_entity_type_id' => 'node'));
     $comment_type->save();
     $content_type = entity_create('node_type', array('type' => $this->randomMachineName(), 'name' => $this->randomString()));
     $content_type->save();
     $field_storage = entity_create('field_storage_config', array('field_name' => Unicode::strtolower($this->randomMachineName()), 'entity_type' => 'node', 'type' => 'comment'));
     $field_storage->save();
     entity_create('field_config', array('field_storage' => $field_storage, 'bundle' => $content_type->id(), 'label' => $this->randomMachineName() . '_label', 'description' => $this->randomMachineName() . '_description', 'settings' => array('comment_type' => $comment_type->id())))->save();
     entity_create('field_config', array('field_storage' => FieldStorageConfig::loadByName('node', 'body'), 'bundle' => $content_type->id(), 'label' => $this->randomMachineName() . '_body', 'settings' => array('display_summary' => TRUE)))->save();
     ViewTestData::createTestViews(get_class($this), array('views_test_config'));
 }
 /**
  * #post_render_cache callback; replaces placeholder with comment form.
  *
  * @param array $element
  *   The renderable array that contains the to be replaced placeholder.
  * @param array $context
  *   An array with the following keys:
  *   - entity_type: an entity type
  *   - entity_id: an entity ID
  *   - field_name: a comment field name
  *
  * @return array
  *   A renderable array containing the comment form.
  */
 public function renderForm(array $element, array $context)
 {
     $field_name = $context['field_name'];
     $entity = $this->entityManager->getStorage($context['entity_type'])->load($context['entity_id']);
     $field_storage = FieldStorageConfig::loadByName($entity->getEntityTypeId(), $field_name);
     $values = array('entity_type' => $entity->getEntityTypeId(), 'entity_id' => $entity->id(), 'field_name' => $field_name, 'comment_type' => $field_storage->getSetting('bundle'), 'pid' => NULL);
     $comment = $this->entityManager->getStorage('comment')->create($values);
     $form = $this->entityFormBuilder->getForm($comment);
     // @todo: This only works as long as assets are still tracked in a global
     //   static variable, see https://drupal.org/node/2238835
     $markup = drupal_render($form, TRUE);
     $callback = 'comment.post_render_cache:renderForm';
     $placeholder = drupal_render_cache_generate_placeholder($callback, $context);
     $element['#markup'] = str_replace($placeholder, $markup, $element['#markup']);
     return $element;
 }
 /**
  * Tests node body field storage persistence even if there are no instances.
  */
 public function testFieldOverrides()
 {
     $field_storage = FieldStorageConfig::loadByName('node', 'body');
     $this->assertTrue($field_storage, 'Node body field storage exists.');
     $type = NodeType::create(['name' => 'Ponies', 'type' => 'ponies']);
     $type->save();
     node_add_body_field($type);
     $field_storage = FieldStorageConfig::loadByName('node', 'body');
     $this->assertTrue(count($field_storage->getBundles()) == 1, 'Node body field storage is being used on the new node type.');
     $field = FieldConfig::loadByName('node', 'ponies', 'body');
     $field->delete();
     $field_storage = FieldStorageConfig::loadByName('node', 'body');
     $this->assertTrue(count($field_storage->getBundles()) == 0, 'Node body field storage exists after deleting the only instance of a field.');
     \Drupal::service('module_installer')->uninstall(array('node'));
     $field_storage = FieldStorageConfig::loadByName('node', 'body');
     $this->assertFalse($field_storage, 'Node body field storage does not exist after uninstalling the Node module.');
 }
 protected function setUp()
 {
     parent::setUp();
     // Create a user who can administer search, do searches, see the status
     // report, and administer cron. Log in.
     $user = $this->drupalCreateUser(array('administer search', 'search content', 'use advanced search', 'access content', 'access site reports', 'administer site configuration'));
     $this->drupalLogin($user);
     // Make sure that auto-cron is disabled.
     $this->config('system.cron')->set('threshold.autorun', 0)->save();
     // Set up the search plugin.
     $this->plugin = $this->container->get('plugin.manager.search')->createInstance('node_search');
     // Check indexing counts before adding any nodes.
     $this->assertIndexCounts(0, 0, 'before adding nodes');
     $this->assertDatabaseCounts(0, 0, 'before adding nodes');
     // Add two new languages.
     ConfigurableLanguage::createFromLangcode('hu')->save();
     ConfigurableLanguage::createFromLangcode('sv')->save();
     // Make the body field translatable. The title is already translatable by
     // definition. The parent class has already created the article and page
     // content types.
     $field_storage = FieldStorageConfig::loadByName('node', 'body');
     $field_storage->setTranslatable(TRUE);
     $field_storage->save();
     // Create a few page nodes with multilingual body values.
     $default_format = filter_default_format();
     $nodes = array(array('title' => 'First node en', 'type' => 'page', 'body' => array(array('value' => $this->randomMachineName(32), 'format' => $default_format)), 'langcode' => 'en'), array('title' => 'Second node this is the English title', 'type' => 'page', 'body' => array(array('value' => $this->randomMachineName(32), 'format' => $default_format)), 'langcode' => 'en'), array('title' => 'Third node en', 'type' => 'page', 'body' => array(array('value' => $this->randomMachineName(32), 'format' => $default_format)), 'langcode' => 'en'), array(), array(), array(), array(), array());
     $this->searchableNodes = array();
     foreach ($nodes as $setting) {
         $this->searchableNodes[] = $this->drupalCreateNode($setting);
     }
     // Add a single translation to the second node.
     $translation = $this->searchableNodes[1]->addTranslation('hu', array('title' => 'Second node hu'));
     $translation->body->value = $this->randomMachineName(32);
     $this->searchableNodes[1]->save();
     // Add two translations to the third node.
     $translation = $this->searchableNodes[2]->addTranslation('hu', array('title' => 'Third node this is the Hungarian title'));
     $translation->body->value = $this->randomMachineName(32);
     $translation = $this->searchableNodes[2]->addTranslation('sv', array('title' => 'Third node sv'));
     $translation->body->value = $this->randomMachineName(32);
     $this->searchableNodes[2]->save();
     // Verify that we have 8 nodes left to do.
     $this->assertIndexCounts(8, 8, 'before updating the search index');
     $this->assertDatabaseCounts(0, 0, 'before updating the search index');
 }
 /**
  * Tests term reference field and widget with multiple vocabularies.
  */
 function testTaxonomyTermFieldMultipleVocabularies()
 {
     // Create a term in each vocabulary.
     $term1 = $this->createTerm($this->vocabulary1);
     $term2 = $this->createTerm($this->vocabulary2);
     // Submit an entity with both terms.
     $this->drupalGet('entity_test/add');
     // Just check if the widget for the select is displayed, the NULL value is
     // used to ignore the value check.
     $this->assertFieldByName("{$this->field_name}[]", NULL, 'Widget is displayed.');
     $edit = array('user_id' => mt_rand(0, 10), 'name' => $this->randomName(), "{$this->field_name}[]" => array($term1->id(), $term2->id()));
     $this->drupalPostForm(NULL, $edit, t('Save'));
     preg_match('|entity_test/manage/(\\d+)|', $this->url, $match);
     $id = $match[1];
     $this->assertText(t('entity_test @id has been created.', array('@id' => $id)), 'Entity was created.');
     // Render the entity.
     $entity = entity_load('entity_test', $id);
     $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full');
     $content = $display->build($entity);
     $this->drupalSetContent(drupal_render($content));
     $this->assertText($term1->getName(), 'Term 1 name is displayed.');
     $this->assertText($term2->getName(), 'Term 2 name is displayed.');
     // Delete vocabulary 2.
     $this->vocabulary2->delete();
     // Re-render the content.
     $entity = entity_load('entity_test', $id);
     $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full');
     $content = $display->build($entity);
     $this->drupalSetContent(drupal_render($content));
     // Term 1 should still be displayed; term 2 should not be.
     $this->assertText($term1->getName(), 'Term 1 name is displayed.');
     $this->assertNoText($term2->getName(), 'Term 2 name is not displayed.');
     // Verify that field storage and instance settings are correct.
     $field_storage = FieldStorageConfig::loadByName('entity_test', $this->field_name);
     $this->assertEqual(count($field_storage->getSetting('allowed_values')), 1, 'Only one vocabulary is allowed for the field.');
     // The widget should still be displayed.
     $this->drupalGet('entity_test/add');
     // Just check if the widget for the select is displayed, the NULL value is
     // used to ignore the value check.
     $this->assertFieldByName("{$this->field_name}[]", NULL, 'Widget is still displayed.');
     // Term 1 should still pass validation.
     $edit = array('user_id' => mt_rand(0, 10), 'name' => $this->randomName(), "{$this->field_name}[]" => array($term1->id()));
     $this->drupalPostForm(NULL, $edit, t('Save'));
 }
Esempio n. 28
0
 protected function setUp()
 {
     parent::setUp();
     $this->installEntitySchema('file');
     $this->installSchema('node', array('node_access'));
     $this->installSchema('file', array('file_usage'));
     $this->installConfig(['node']);
     // Add text formats.
     $filtered_html_format = FilterFormat::create(array('format' => 'filtered_html', 'name' => 'Filtered HTML', 'weight' => 0, 'filters' => array()));
     $filtered_html_format->save();
     // Set cardinality for body field.
     FieldStorageConfig::loadByName('node', 'body')->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)->save();
     // Set up text editor.
     $editor = Editor::create(['format' => 'filtered_html', 'editor' => 'unicorn']);
     $editor->save();
     // Create a node type for testing.
     $type = NodeType::create(['type' => 'page', 'name' => 'page']);
     $type->save();
     node_add_body_field($type);
 }
 /**
  * Checks whether field languages are correctly stored for the given entity.
  *
  * @param \Drupal\Core\Entity\ContentEntityInterface $entity
  *   The entity fields are attached to.
  * @param string $message
  *   (optional) A message to display with the assertion.
  */
 protected function assertFieldStorageLangcode(ContentEntityInterface $entity, $message = '')
 {
     $status = TRUE;
     $entity_type = $entity->getEntityTypeId();
     $id = $entity->id();
     $langcode = $entity->getUntranslated()->language()->id;
     $fields = array($this->field_name, $this->untranslatable_field_name);
     foreach ($fields as $field_name) {
         $field_storage = FieldStorageConfig::loadByName($entity_type, $field_name);
         $tables = array(ContentEntityDatabaseStorage::_fieldTableName($field_storage), ContentEntityDatabaseStorage::_fieldRevisionTableName($field_storage));
         foreach ($tables as $table) {
             $record = \Drupal::database()->select($table, 'f')->fields('f')->condition('f.entity_id', $id)->condition('f.revision_id', $id)->execute()->fetchObject();
             if ($record->langcode != $langcode) {
                 $status = FALSE;
                 break;
             }
         }
     }
     return $this->assertTrue($status, $message);
 }
 /**
  * Checks whether field languages are correctly stored for the given entity.
  *
  * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
  *   The entity fields are attached to.
  * @param string $message
  *   (optional) A message to display with the assertion.
  */
 protected function assertFieldStorageLangcode(FieldableEntityInterface $entity, $message = '')
 {
     $status = TRUE;
     $entity_type = $entity->getEntityTypeId();
     $id = $entity->id();
     $langcode = $entity->getUntranslated()->language()->getId();
     $fields = array($this->field_name, $this->untranslatable_field_name);
     /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
     $table_mapping = \Drupal::entityManager()->getStorage($entity_type)->getTableMapping();
     foreach ($fields as $field_name) {
         $field_storage = FieldStorageConfig::loadByName($entity_type, $field_name);
         $table = $table_mapping->getDedicatedDataTableName($field_storage);
         $record = \Drupal::database()->select($table, 'f')->fields('f')->condition('f.entity_id', $id)->condition('f.revision_id', $id)->execute()->fetchObject();
         if ($record->langcode != $langcode) {
             $status = FALSE;
             break;
         }
     }
     return $this->assertTrue($status, $message);
 }