/** * Checks that the Revision tab is displayed correctly. */ function testDisplayRevisionTab() { $this->drupalPlaceBlock('local_tasks_block'); $this->drupalLogin($this->editor); $node_storage = $this->container->get('entity.manager')->getStorage('node'); // Set page revision setting 'create new revision'. This will mean new // revisions are created by default when the node is edited. $type = NodeType::load('page'); $type->setNewRevision(TRUE); $type->save(); // Create the node. $node = $this->drupalCreateNode(); // Verify the checkbox is checked on the node edit form. $this->drupalGet('node/' . $node->id() . '/edit'); $this->assertFieldChecked('edit-revision', "'Create new revision' checkbox is checked"); // Uncheck the create new revision checkbox and save the node. $edit = array('revision' => FALSE); $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, 'Save and keep published'); $this->assertUrl($node->toUrl()); $this->assertNoLink(t('Revisions')); // Verify the checkbox is checked on the node edit form. $this->drupalGet('node/' . $node->id() . '/edit'); $this->assertFieldChecked('edit-revision', "'Create new revision' checkbox is checked"); // Submit the form without changing the checkbox. $edit = array(); $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, 'Save and keep published'); $this->assertUrl($node->toUrl()); $this->assertLink(t('Revisions')); }
/** * Performs setup tasks before each individual test method is run. */ public function setUp() { parent::setUp('content_access'); // The parent method already installs most needed node and comment schemas, // but here we also need the comment statistics. $this->installSchema('comment', array('comment_entity_statistics')); // Create a node type for testing. $type = NodeType::create(array('type' => 'page', 'name' => 'page')); $type->save(); // Create anonymous user role. $role = Role::create(array('id' => 'anonymous', 'label' => 'anonymous')); $role->save(); // Insert the anonymous user into the database, as the user table is inner // joined by \Drupal\comment\CommentStorage. User::create(array('uid' => 0, 'name' => ''))->save(); // Create a node with attached comment. $this->nodes[0] = Node::create(array('status' => NODE_PUBLISHED, 'type' => 'page', 'title' => 'test title')); $this->nodes[0]->save(); $comment_type = CommentType::create(array('id' => 'comment', 'target_entity_type_id' => 'node')); $comment_type->save(); $this->installConfig(array('comment')); $this->addDefaultCommentField('node', 'page'); $comment = Comment::create(array('entity_type' => 'node', 'entity_id' => $this->nodes[0]->id(), 'field_name' => 'comment', 'body' => 'test body', 'comment_type' => $comment_type->id())); $comment->save(); $this->comments[] = $comment; $this->nodes[1] = Node::create(array('status' => NODE_PUBLISHED, 'type' => 'page', 'title' => 'some title')); $this->nodes[1]->save(); $this->nodes[2] = Node::create(array('status' => NODE_NOT_PUBLISHED, 'type' => 'page', 'title' => 'other title')); $this->nodes[2]->save(); // Also index users, to verify that they are unaffected by the processor. $this->index->set('datasources', array('entity:comment', 'entity:node', 'entity:user')); $this->index->save(); $this->index = entity_load('search_api_index', $this->index->id(), TRUE); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->installEntitySchema('node'); // Need at least one node type present. NodeType::create(['type' => 'testnodetype', 'name' => 'Test node type'])->save(); }
/** * Adds a content type with a Paragraphs field. * * @param string $content_type_name * Content type name to be used. * @param string $paragraphs_field_name * Paragraphs field name to be used. */ protected function addParagraphedContentType($content_type_name, $paragraphs_field_name) { // Create the content type. $node_type = NodeType::create(['type' => $content_type_name, 'name' => $content_type_name]); $node_type->save(); $this->addParagraphsField($content_type_name, $paragraphs_field_name, 'node'); }
/** * Tests configuration renaming validation. */ public function testRenameValidation() { // Create a test entity. $test_entity_id = $this->randomMachineName(); $test_entity = entity_create('config_test', array('id' => $test_entity_id, 'label' => $this->randomMachineName())); $test_entity->save(); $uuid = $test_entity->uuid(); // Stage the test entity and then delete it from the active storage. $active = $this->container->get('config.storage'); $sync = $this->container->get('config.storage.sync'); $this->copyConfig($active, $sync); $test_entity->delete(); // Create a content type with a matching UUID in the active storage. $content_type = NodeType::create(['type' => Unicode::strtolower($this->randomMachineName(16)), 'name' => $this->randomMachineName(), 'uuid' => $uuid]); $content_type->save(); // Confirm that the staged configuration is detected as a rename since the // UUIDs match. $this->configImporter->reset(); $expected = array('node.type.' . $content_type->id() . '::config_test.dynamic.' . $test_entity_id); $renames = $this->configImporter->getUnprocessedConfiguration('rename'); $this->assertIdentical($expected, $renames); // Try to import the configuration. We expect an exception to be thrown // because the staged entity is of a different type. try { $this->configImporter->import(); $this->fail('Expected ConfigImporterException thrown when a renamed configuration entity does not match the existing entity type.'); } catch (ConfigImporterException $e) { $this->pass('Expected ConfigImporterException thrown when a renamed configuration entity does not match the existing entity type.'); $expected = array(SafeMarkup::format('Entity type mismatch on rename. @old_type not equal to @new_type for existing configuration @old_name and staged configuration @new_name.', array('@old_type' => 'node_type', '@new_type' => 'config_test', '@old_name' => 'node.type.' . $content_type->id(), '@new_name' => 'config_test.dynamic.' . $test_entity_id))); $this->assertEqual($expected, $this->configImporter->getErrors()); } }
/** * Tests the deletion of a scheduled node. * * This tests if it is possible to delete a node that does not have a * publication date set, when scheduled publishing is required. * * @see https://drupal.org/node/1614880 */ public function testScheduledNodeDelete() { // Log in. $this->drupalLogin($this->adminUser); // Create a published and an unpublished node, both without scheduling. $published_node = $this->drupalCreateNode(['type' => 'page', 'status' => 1]); $unpublished_node = $this->drupalCreateNode(['type' => 'page', 'status' => 0]); // Make scheduled publishing and unpublishing required. $node_type = NodeType::load('page'); $node_type->setThirdPartySetting('scheduler', 'publish_required', TRUE); $node_type->setThirdPartySetting('scheduler', 'unpublish_required', TRUE); $node_type->save(); // Check that deleting the nodes does not throw form validation errors. ### @TODO Delete was a button in 7.x but a separate link node/<nid>/delete in 8.x ### Is the previous validation (that we had to avoid on delete) still done now in D8, given that there is no form? ### Maybe this test is not actually checking anything useful? Can it be altered to do something testable? $this->drupalGet('node/' . $published_node->id() . '/delete'); // Note that the text 'error message' is used in a header h2 html tag which // is normally made hidden from browsers but will be in the page source. // It is also good when testing for the absense of somthing to also test // for the presence of text, hence the second assertion for each check. $this->assertNoRaw(t('Error message'), 'No error messages are shown when trying to delete a published node with no scheduling information.'); $this->assertRaw(t('Are you sure you want to delete the content'), 'The deletion warning message is shown immediately when trying to delete a published node with no scheduling information.'); $this->drupalGet('node/' . $unpublished_node->id() . '/delete'); $this->assertNoRaw(t('Error message'), 'No error messages are shown when trying to delete an unpublished node with no scheduling information.'); $this->assertRaw(t('Are you sure you want to delete the content'), 'The deletion warning message is shown immediately when trying to delete an unpublished node with no scheduling information.'); }
public function setUp() { parent::setup(); $this->installConfig(array('pathauto', 'taxonomy', 'system', 'node')); $this->installEntitySchema('user'); $this->installEntitySchema('node'); $this->installEntitySchema('taxonomy_term'); ConfigurableLanguage::createFromLangcode('fr')->save(); $this->installSchema('node', array('node_access')); $this->installSchema('system', array('url_alias', 'sequences', 'router')); $type = NodeType::create(['type' => 'page']); $type->save(); node_add_body_field($type); $this->nodePattern = $this->createPattern('node', '/content/[node:title]'); $this->userPattern = $this->createPattern('user', '/users/[user:name]'); \Drupal::service('router.builder')->rebuild(); $this->currentUser = entity_create('user', array('name' => $this->randomMachineName())); $this->currentUser->save(); }
/** * Prepare a node to get suggestions from. * * Creates a node with two file fields. The first one is not translatable, * the second one is. Both fields got two files attached, where one has * translatable content (title and atl-text) and the other one not. * * @return object * The node which is prepared with all needed fields for the suggestions. */ protected function prepareTranslationSuggestions() { // Create a content type with fields. // Only the first field is a translatable reference. $type = NodeType::create(['type' => $this->randomMachineName()]); $type->save(); $content_translation_manager = \Drupal::service('content_translation.manager'); $content_translation_manager->setEnabled('node', $type->id(), TRUE); $field1 = FieldStorageConfig::create(array('field_name' => 'field1', 'entity_type' => 'node', 'type' => 'entity_reference', 'cardinality' => -1, 'settings' => array('target_type' => 'node'))); $field1->save(); $field2 = FieldStorageConfig::create(array('field_name' => 'field2', 'entity_type' => 'node', 'type' => 'entity_reference', 'cardinality' => -1, 'settings' => array('target_type' => 'node'))); $field2->save(); // Create field instances on the content type. FieldConfig::create(array('field_storage' => $field1, 'bundle' => $type->id(), 'label' => 'Field 1', 'translatable' => FALSE, 'settings' => array()))->save(); FieldConfig::create(array('field_storage' => $field2, 'bundle' => $type->id(), 'label' => 'Field 2', 'translatable' => TRUE, 'settings' => array()))->save(); // Create a translatable body field. node_add_body_field($type); $field = FieldConfig::loadByName('node', $type->id(), 'body'); $field->setTranslatable(TRUE); $field->save(); // Create 4 nodes to be referenced. $references = array(); for ($i = 0; $i < 4; $i++) { $references[$i] = Node::create(array('title' => $this->randomMachineName(), 'body' => $this->randomMachineName(), 'type' => $type->id())); $references[$i]->save(); } // Create a node with two translatable and two non-translatable references. $node = Node::create(array('title' => $this->randomMachineName(), 'type' => $type->id(), 'language' => 'en', 'body' => $this->randomMachineName(), $field1->getName() => array(array('target_id' => $references[0]->id()), array('target_id' => $references[1]->id())), $field2->getName() => array(array('target_id' => $references[2]->id()), array('target_id' => $references[3]->id())))); $node->save(); $link = MenuLinkContent::create(['link' => [['uri' => 'entity:node/' . $node->id()]], 'title' => 'Node menu link', 'menu_name' => 'main']); $link->save(); $node->link = $link; return $node; }
/** * Tests exportContentWithReferences(). */ public function testExportWithReferences() { \Drupal::service('module_installer')->install(['node', 'default_content']); \Drupal::service('router.builder')->rebuild(); $this->defaultContentManager = \Drupal::service('default_content.manager'); $user = User::create(['name' => 'my username']); $user->save(); // Reload the user to get the proper casted values from the DB. $user = User::load($user->id()); $node_type = NodeType::create(['type' => 'test']); $node_type->save(); $node = Node::create(['type' => $node_type->id(), 'title' => 'test node', 'uid' => $user->id()]); $node->save(); // Reload the node to get the proper casted values from the DB. $node = Node::load($node->id()); /** @var \Symfony\Component\Serializer\Serializer $serializer */ $serializer = \Drupal::service('serializer'); \Drupal::service('rest.link_manager')->setLinkDomain(DefaultContentManager::LINK_DOMAIN); $expected_node = $serializer->serialize($node, 'hal_json', ['json_encode_options' => JSON_PRETTY_PRINT]); $expected_user = $serializer->serialize($user, 'hal_json', ['json_encode_options' => JSON_PRETTY_PRINT]); $exported_by_entity_type = $this->defaultContentManager->exportContentWithReferences('node', $node->id()); // Ensure that the node type is not tryed to be exported. $this->assertEqual(array_keys($exported_by_entity_type), ['node', 'user']); // Ensure the right UUIDs are exported. $this->assertEqual([$node->uuid()], array_keys($exported_by_entity_type['node'])); $this->assertEqual([$user->uuid()], array_keys($exported_by_entity_type['user'])); // Compare the actual serialized data. $this->assertEqual(reset($exported_by_entity_type['node']), $expected_node); $this->assertEqual(reset($exported_by_entity_type['user']), $expected_user); }
/** * Checks that unchecking 'Create new revision' works when editing a node. */ function testNodeFormSaveWithoutRevision() { $this->drupalLogin($this->editor); $node_storage = $this->container->get('entity.manager')->getStorage('node'); // Set page revision setting 'create new revision'. This will mean new // revisions are created by default when the node is edited. $type = NodeType::load('page'); $type->setNewRevision(TRUE); $type->save(); // Create the node. $node = $this->drupalCreateNode(); // Verify the checkbox is checked on the node edit form. $this->drupalGet('node/' . $node->id() . '/edit'); $this->assertFieldChecked('edit-revision', "'Create new revision' checkbox is checked"); // Uncheck the create new revision checkbox and save the node. $edit = array('revision' => FALSE); $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published')); // Load the node again and check the revision is the same as before. $node_storage->resetCache(array($node->id())); $node_revision = $node_storage->load($node->id(), TRUE); $this->assertEqual($node_revision->getRevisionId(), $node->getRevisionId(), "After an existing node is saved with 'Create new revision' unchecked, a new revision is not created."); // Verify the checkbox is checked on the node edit form. $this->drupalGet('node/' . $node->id() . '/edit'); $this->assertFieldChecked('edit-revision', "'Create new revision' checkbox is checked"); // Submit the form without changing the checkbox. $edit = array(); $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published')); // Load the node again and check the revision is different from before. $node_storage->resetCache(array($node->id())); $node_revision = $node_storage->load($node->id()); $this->assertNotEqual($node_revision->getRevisionId(), $node->getRevisionId(), "After an existing node is saved with 'Create new revision' checked, a new revision is created."); }
protected function setUp() { parent::setUp(); node_access_test_add_field(NodeType::load('article')); node_access_rebuild(); \Drupal::state()->set('node_access_test.private', TRUE); }
/** * Tests Drupal 6 node type to Drupal 8 migration. */ public function testNodeType() { $migration = entity_load('migration', 'd6_node_type'); // Test the test_page content type. $node_type_page = NodeType::load('test_page'); $this->assertIdentical('test_page', $node_type_page->id(), 'Node type test_page loaded'); $this->assertIdentical(TRUE, $node_type_page->displaySubmitted()); $this->assertIdentical(FALSE, $node_type_page->isNewRevision()); $this->assertIdentical(DRUPAL_OPTIONAL, $node_type_page->getPreviewMode()); $this->assertIdentical($migration->getIdMap()->lookupDestinationID(array('test_page')), array('test_page')); // Test we have a body field. $field = FieldConfig::loadByName('node', 'test_page', 'body'); $this->assertIdentical('This is the body field label', $field->getLabel(), 'Body field was found.'); // Test the test_story content type. $node_type_story = NodeType::load('test_story'); $this->assertIdentical('test_story', $node_type_story->id(), 'Node type test_story loaded'); $this->assertIdentical(TRUE, $node_type_story->displaySubmitted()); $this->assertIdentical(FALSE, $node_type_story->isNewRevision()); $this->assertIdentical(DRUPAL_OPTIONAL, $node_type_story->getPreviewMode()); $this->assertIdentical($migration->getIdMap()->lookupDestinationID(array('test_story')), array('test_story')); // Test we don't have a body field. $field = FieldConfig::loadByName('node', 'test_story', 'body'); $this->assertIdentical(NULL, $field, 'No body field found'); // Test the test_event content type. $node_type_event = NodeType::load('test_event'); $this->assertIdentical('test_event', $node_type_event->id(), 'Node type test_event loaded'); $this->assertIdentical(TRUE, $node_type_event->displaySubmitted()); $this->assertIdentical(TRUE, $node_type_event->isNewRevision()); $this->assertIdentical(DRUPAL_OPTIONAL, $node_type_event->getPreviewMode()); $this->assertIdentical($migration->getIdMap()->lookupDestinationID(array('test_event')), array('test_event')); // Test we have a body field. $field = FieldConfig::loadByName('node', 'test_event', 'body'); $this->assertIdentical('Body', $field->getLabel(), 'Body field was found.'); }
/** * Create fields for the selected properties. */ protected function createField() { $entity_type = 'node'; $bundle = $this->entity->id(); foreach ($this->properties as $key => $value) { $label = $this->converter->label($key); // Add the field prefix and truncate if longer than 32 char. $field_name = $this->prefix . strtolower($label); if (strlen($field_name) > 32) { $field_name = substr($field_name, 0, 32); } $field_storage = array( 'field_name' => $field_name, 'entity_type' => $entity_type, 'type' => $value['type'], 'translatable' => TRUE, ); $instance = array( 'field_name' => $field_name, 'entity_type' => $entity_type, 'bundle' => $bundle, 'label' => $label, // Field translatability should be explicitly enabled by the users. 'translatable' => FALSE, ); // Create the field and instance. try { \Drupal::entityTypeManager()->getStorage('field_storage_config')->create($field_storage)->save(); \Drupal::entityTypeManager()->getStorage('field_config')->create($instance)->save(); // Make sure the field is displayed in the 'default' form mode (using // default widget and settings). It stays hidden for other form modes // until it is explicitly configured. entity_get_form_display($entity_type, $bundle, 'default') ->setComponent($field_name) ->save(); // Make sure the field is displayed in the 'default' view mode (using // default formatter and settings). It stays hidden for other view // modes until it is explicitly configured. entity_get_display($entity_type, $bundle, 'default') ->setComponent($field_name) ->save(); // RDF Mapping. $this->rdfMapping->setFieldMapping($field_name, array( 'properties' => array($key), ) ); } catch (\Exception $e) { drupal_set_message($this->t('There was a problem creating field %label: !message', array( '%label' => $instance['label'], '!message' => $e->getMessage(), )), 'error'); } } }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->installEntitySchema('node'); $this->installEntitySchema('comment'); $this->installEntitySchema('taxonomy_term'); CommentType::create(['id' => 'comment_node_page', 'label' => $this->randomMachineName()])->save(); CommentType::create(['id' => 'comment_node_article', 'label' => $this->randomMachineName()])->save(); CommentType::create(['id' => 'comment_node_blog', 'label' => $this->randomMachineName()])->save(); CommentType::create(['id' => 'comment_node_book', 'label' => $this->randomMachineName()])->save(); CommentType::create(['id' => 'comment_node_forum', 'label' => $this->randomMachineName()])->save(); CommentType::create(['id' => 'comment_node_test_content_type', 'label' => $this->randomMachineName()])->save(); NodeType::create(['type' => 'page', 'label' => $this->randomMachineName()])->save(); NodeType::create(['type' => 'article', 'label' => $this->randomMachineName()])->save(); NodeType::create(['type' => 'blog', 'label' => $this->randomMachineName()])->save(); NodeType::create(['type' => 'book', 'label' => $this->randomMachineName()])->save(); NodeType::create(['type' => 'forum', 'label' => $this->randomMachineName()])->save(); NodeType::create(['type' => 'test_content_type', 'label' => $this->randomMachineName()])->save(); Vocabulary::create(['vid' => 'test_vocabulary'])->save(); // Give one unfortunate field instance invalid display settings to ensure // that the migration provides an empty array as a default (thus avoiding // an "unsupported operand types" fatal). Database::getConnection('default', 'migrate')->update('field_config_instance')->fields(array('data' => serialize(array('label' => 'Body', 'widget' => array('type' => 'text_textarea_with_summary', 'settings' => array('rows' => 20, 'summary_rows' => 5), 'weight' => -4, 'module' => 'text'), 'settings' => array('display_summary' => TRUE, 'text_processing' => 1, 'user_register_form' => FALSE), 'display' => array('default' => array('label' => 'hidden', 'type' => 'text_default', 'settings' => array(), 'module' => 'text', 'weight' => 0), 'teaser' => array('label' => 'hidden', 'type' => 'text_summary_or_trimmed', 'settings' => NULL, 'module' => 'text', 'weight' => 0)), 'required' => FALSE, 'description' => ''))))->condition('entity_type', 'node')->condition('bundle', 'article')->condition('field_name', 'body')->execute(); $this->executeMigrations(['d7_field', 'd7_field_instance', 'd7_view_modes', 'd7_field_formatter_settings']); }
/** * {@inheritdoc} */ public function settingsForm(array $form, FormStateInterface $form_state) { $options = array(); $types = NodeType::loadMultiple(); $comment_fields = $this->commentManager ? $this->commentManager->getFields('node') : array(); $map = array($this->t('Hidden'), $this->t('Closed'), $this->t('Open')); foreach ($types as $type) { $options[$type->id()] = array('type' => array('#markup' => $this->t($type->label()))); if ($this->commentManager) { $fields = array(); foreach ($comment_fields as $field_name => $info) { // Find all comment fields for the bundle. if (in_array($type->id(), $info['bundles'])) { $instance = FieldConfig::loadByName('node', $type->id(), $field_name); $default_mode = reset($instance->default_value); $fields[] = SafeMarkup::format('@field: !state', array('@field' => $instance->label(), '!state' => $map[$default_mode['status']])); } } // @todo Refactor display of comment fields. if (!empty($fields)) { $options[$type->id()]['comments'] = array('data' => array('#theme' => 'item_list', '#items' => $fields)); } else { $options[$type->id()]['comments'] = $this->t('No comment fields'); } } } if (empty($options)) { $create_url = $this->urlGenerator->generateFromRoute('node.type_add'); $this->setMessage($this->t('You do not have any content types that can be generated. <a href="@create-type">Go create a new content type</a> already!</a>', array('@create-type' => $create_url)), 'error', FALSE); return; } $header = array('type' => $this->t('Content type')); if ($this->commentManager) { $header['comments'] = array('data' => $this->t('Comments'), 'class' => array(RESPONSIVE_PRIORITY_MEDIUM)); } $form['node_types'] = array('#type' => 'tableselect', '#header' => $header, '#options' => $options); $form['kill'] = array('#type' => 'checkbox', '#title' => $this->t('<strong>Delete all content</strong> in these content types before generating new content.'), '#default_value' => $this->getSetting('kill')); $form['num'] = array('#type' => 'textfield', '#title' => $this->t('How many nodes would you like to generate?'), '#default_value' => $this->getSetting('num'), '#size' => 10); $options = array(1 => $this->t('Now')); foreach (array(3600, 86400, 604800, 2592000, 31536000) as $interval) { $options[$interval] = \Drupal::service('date.formatter')->formatInterval($interval, 1) . ' ' . $this->t('ago'); } $form['time_range'] = array('#type' => 'select', '#title' => $this->t('How far back in time should the nodes be dated?'), '#description' => $this->t('Node creation dates will be distributed randomly from the current time, back to the selected time.'), '#options' => $options, '#default_value' => 604800); $form['max_comments'] = array('#type' => $this->moduleHandler->moduleExists('comment') ? 'textfield' : 'value', '#title' => $this->t('Maximum number of comments per node.'), '#description' => $this->t('You must also enable comments for the content types you are generating. Note that some nodes will randomly receive zero comments. Some will receive the max.'), '#default_value' => $this->getSetting('max_comments'), '#size' => 3, '#access' => $this->moduleHandler->moduleExists('comment')); $form['title_length'] = array('#type' => 'textfield', '#title' => $this->t('Maximum number of words in titles'), '#default_value' => $this->getSetting('title_length'), '#size' => 10); $form['add_alias'] = array('#type' => 'checkbox', '#disabled' => !$this->moduleHandler->moduleExists('path'), '#description' => $this->t('Requires path.module'), '#title' => $this->t('Add an url alias for each node.'), '#default_value' => FALSE); $form['add_statistics'] = array('#type' => 'checkbox', '#title' => $this->t('Add statistics for each node (node_counter table).'), '#default_value' => TRUE, '#access' => $this->moduleHandler->moduleExists('statistics')); $options = array(); // We always need a language $languages = \Drupal::languageManager()->getLanguages(LanguageInterface::STATE_ALL); foreach ($languages as $langcode => $language) { $options[$langcode] = $language->getName(); } $default_language = \Drupal::service('language.default')->get(); $default_langcode = $default_language->getId(); $form['add_language'] = array('#type' => 'select', '#title' => $this->t('Set language on nodes'), '#multiple' => TRUE, '#description' => $this->t('Requires locale.module'), '#options' => $options, '#default_value' => array($default_langcode)); $form['submit'] = array('#type' => 'submit', '#value' => $this->t('Generate'), '#tableselect' => TRUE); $form['#redirect'] = FALSE; return $form; }
protected function setUp() { parent::setUp(); // Ensure the page node type exists. NodeType::create(['type' => 'page', 'name' => 'page'])->save(); ViewTestData::createTestViews(get_class($this), array('field_test_views')); }
/** * Tests the normalization of comments. */ public function testComment() { $node_type = NodeType::create(['type' => 'example_type']); $node_type->save(); $account = User::create(['name' => $this->randomMachineName()]); $account->save(); // Add comment type. $this->container->get('entity.manager')->getStorage('comment_type')->create(array('id' => 'comment', 'label' => 'comment', 'target_entity_type_id' => 'node'))->save(); $this->addDefaultCommentField('node', 'example_type'); $node = Node::create(['title' => $this->randomMachineName(), 'uid' => $account->id(), 'type' => $node_type->id(), 'status' => NODE_PUBLISHED, 'promote' => 1, 'sticky' => 0, 'body' => [['value' => $this->randomMachineName(), 'format' => $this->randomMachineName()]]]); $node->save(); $parent_comment = Comment::create(array('uid' => $account->id(), 'subject' => $this->randomMachineName(), 'comment_body' => ['value' => $this->randomMachineName(), 'format' => NULL], 'entity_id' => $node->id(), 'entity_type' => 'node', 'field_name' => 'comment')); $parent_comment->save(); $comment = Comment::create(array('uid' => $account->id(), 'subject' => $this->randomMachineName(), 'comment_body' => ['value' => $this->randomMachineName(), 'format' => NULL], 'entity_id' => $node->id(), 'entity_type' => 'node', 'field_name' => 'comment', 'pid' => $parent_comment->id(), 'mail' => '*****@*****.**', 'homepage' => 'http://buytaert.net')); $comment->save(); $original_values = $comment->toArray(); // Hostname will always be denied view access. // No value will exist for name as this is only for anonymous users. unset($original_values['hostname'], $original_values['name']); $normalized = $this->serializer->normalize($comment, $this->format, ['account' => $account]); // Assert that the hostname field does not appear at all in the normalized // data. $this->assertFalse(array_key_exists('hostname', $normalized), 'Hostname was not found in normalized comment data.'); /** @var \Drupal\comment\CommentInterface $denormalized_comment */ $denormalized_comment = $this->serializer->denormalize($normalized, 'Drupal\\comment\\Entity\\Comment', $this->format, ['account' => $account]); // Before comparing, unset values that are expected to differ. $denormalized_comment_values = $denormalized_comment->toArray(); unset($denormalized_comment_values['hostname'], $denormalized_comment_values['name']); $this->assertEqual($original_values, $denormalized_comment_values, 'The expected comment values are restored after normalizing and denormalizing.'); }
public function testRecreateEntity() { $type_name = Unicode::strtolower($this->randomMachineName(16)); $content_type = entity_create('node_type', array('type' => $type_name, 'name' => 'Node type one')); $content_type->save(); node_add_body_field($content_type); /** @var \Drupal\Core\Config\StorageInterface $active */ $active = $this->container->get('config.storage'); /** @var \Drupal\Core\Config\StorageInterface $sync */ $sync = $this->container->get('config.storage.sync'); $config_name = $content_type->getEntityType()->getConfigPrefix() . '.' . $content_type->id(); $this->copyConfig($active, $sync); // Delete the content type. This will also delete a field storage, a field, // an entity view display and an entity form display. $content_type->delete(); $this->assertFalse($active->exists($config_name), 'Content type\'s old name does not exist active store.'); // Recreate with the same type - this will have a different UUID. $content_type = entity_create('node_type', array('type' => $type_name, 'name' => 'Node type two')); $content_type->save(); node_add_body_field($content_type); $this->configImporter->reset(); // A node type, a field, an entity view display and an entity form display // will be recreated. $creates = $this->configImporter->getUnprocessedConfiguration('create'); $deletes = $this->configImporter->getUnprocessedConfiguration('delete'); $this->assertEqual(5, count($creates), 'There are 5 configuration items to create.'); $this->assertEqual(5, count($deletes), 'There are 5 configuration items to delete.'); $this->assertEqual(0, count($this->configImporter->getUnprocessedConfiguration('update')), 'There are no configuration items to update.'); $this->assertIdentical($creates, array_reverse($deletes), 'Deletes and creates contain the same configuration names in opposite orders due to dependencies.'); $this->configImporter->import(); // Verify that there is nothing more to import. $this->assertFalse($this->configImporter->reset()->hasUnprocessedConfigurationChanges()); $content_type = NodeType::load($type_name); $this->assertEqual('Node type one', $content_type->label()); }
/** * Creates a new node type. * * @param string $label * The human-readable label of the type to create. * @param string $machine_name * The machine name of the type to create. * * @return NodeType * The node type just created. */ protected function createNodeType($label, $machine_name) { /** @var NodeType $node_type */ $node_type = NodeType::create(['type' => $machine_name, 'label' => $label]); $node_type->save(); return $node_type; }
/** * Tests the node type config entity. */ public function testNodeType() { // Create an english test entity. $node_type = NodeType::create(array('type' => 'test', 'name' => 'Node type name', 'description' => 'Node type description', 'title_label' => 'Title label', 'langcode' => 'en')); $node_type->save(); $job = tmgmt_job_create('en', 'de'); $job->translator = 'test_translator'; $job->save(); $job_item = tmgmt_job_item_create('config', 'node_type', 'node.type.' . $node_type->id(), array('tjid' => $job->id())); $job_item->save(); $source_plugin = $this->container->get('plugin.manager.tmgmt.source')->createInstance('config'); $data = $source_plugin->getData($job_item); // Test the name property. $this->assertEqual($data['name']['#label'], 'Name'); $this->assertEqual($data['name']['#text'], $node_type->label()); $this->assertEqual($data['name']['#translate'], TRUE); $this->assertEqual($data['description']['#label'], 'Description'); $this->assertEqual($data['description']['#text'], $node_type->getDescription()); $this->assertEqual($data['description']['#translate'], TRUE); // Test item types. $this->assertEqual($source_plugin->getItemTypes()['node_type'], t('Content type')); // Now request a translation and save it back. $job->requestTranslation(); $items = $job->getItems(); $item = reset($items); $item->acceptTranslation(); $data = $item->getData(); // Check that the translations were saved correctly. $language_manager = \Drupal::languageManager(); $language_manager->setConfigOverrideLanguage($language_manager->getLanguage('de')); $node_type = entity_load('node_type', $node_type->id()); $this->assertEqual($node_type->label(), $data['name']['#translation']['#text']); $this->assertEqual($node_type->getDescription(), $data['description']['#translation']['#text']); }
/** * Set the default field storage backend for fields created during tests. */ protected function setUp() { parent::setUp(); // Create a node type for testing. $type = NodeType::create(['type' => 'page', 'name' => 'page']); $type->save(); }
/** * Tests the normalization of node translations. */ public function testNodeTranslation() { $node_type = NodeType::create(['type' => 'example_type']); $node_type->save(); $this->container->get('content_translation.manager')->setEnabled('node', 'example_type', TRUE); $user = User::create(['name' => $this->randomMachineName()]); $user->save(); $node = Node::create(['title' => $this->randomMachineName(), 'uid' => $user->id(), 'type' => $node_type->id(), 'status' => NODE_PUBLISHED, 'langcode' => 'en', 'promote' => 1, 'sticky' => 0, 'body' => ['value' => $this->randomMachineName(), 'format' => $this->randomMachineName()], 'revision_log' => $this->randomString()]); $node->addTranslation('de', ['title' => 'German title', 'body' => ['value' => $this->randomMachineName(), 'format' => $this->randomMachineName()]]); $node->save(); $original_values = $node->toArray(); $translation = $node->getTranslation('de'); $original_translation_values = $node->getTranslation('en')->toArray(); $normalized = $this->serializer->normalize($node, $this->format); $this->assertContains(['lang' => 'en', 'value' => $node->getTitle()], $normalized['title'], 'Original language title has been normalized.'); $this->assertContains(['lang' => 'de', 'value' => $translation->getTitle()], $normalized['title'], 'Translation language title has been normalized.'); /** @var \Drupal\node\NodeInterface $denormalized_node */ $denormalized_node = $this->serializer->denormalize($normalized, 'Drupal\\node\\Entity\\Node', $this->format); $this->assertSame($denormalized_node->language()->getId(), $denormalized_node->getUntranslated()->language()->getId(), 'Untranslated object is returned from serializer.'); $this->assertSame('en', $denormalized_node->language()->getId()); $this->assertTrue($denormalized_node->hasTranslation('de')); $this->assertSame($node->getTitle(), $denormalized_node->getTitle()); $this->assertSame($translation->getTitle(), $denormalized_node->getTranslation('de')->getTitle()); $this->assertEquals($original_values, $denormalized_node->toArray(), 'Node values are restored after normalizing and denormalizing.'); $this->assertEquals($original_translation_values, $denormalized_node->getTranslation('en')->toArray(), 'Node values are restored after normalizing and denormalizing.'); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->installConfig(array('filter', 'node')); $node_type = NodeType::create(['type' => 'article', 'name' => 'Article']); $node_type->save(); node_add_body_field($node_type); }
/** * Creates a new node type. * * @param string $label * The human-readable label of the type to create. * @param string $machine_name * The machine name of the type to create. * * @return NodeType * The node type just created. */ protected function createNodeType($label, $machine_name) { /** @var NodeType $node_type */ $node_type = NodeType::create(['type' => $machine_name, 'label' => $label]); $node_type->setThirdPartySetting('content_moderation', 'enabled', TRUE); $node_type->save(); return $node_type; }
/** * Implements setUp(). */ function setUp() { parent::setUp(); NodeType::create(['type' => 'page', 'name' => 'Page'])->save(); NodeType::create(['type' => 'article', 'name' => 'Article'])->save(); $web_user = $this->drupalCreateUser(array('create page content')); $this->drupalLogin($web_user); }
/** * 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 = Term::create(['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 = Comment::create(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->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->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->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.'); // Double check everything by reinstalling the forum module again. $this->drupalPostForm('admin/modules', ['modules[Core][forum][enable]' => 1], 'Install'); $this->assertText('Module Forum has been enabled.'); }
/** * {@inheritdoc} */ protected function createEntity() { // Create a "Camelids" node type. NodeType::create(['name' => 'Camelids', 'type' => 'camelids'])->save(); // Create a "Llama" node. $node = Node::create(['type' => 'camelids']); $node->setTitle('Llama')->setPublished(TRUE)->save(); return $node; }
/** * {@inheritdoc} */ public function getOptions() { $sharethis_config = $this->configFactory->get('sharethis.settings'); $view_modes = array(); foreach (array_keys(NodeType::loadMultiple()) as $type) { $view_modes[$type] = array('article' => 'article', 'page' => 'page'); } return ['buttons' => $sharethis_config->get('button_option', 'stbc_button'), 'publisherID' => $sharethis_config->get('publisherID'), 'services' => $sharethis_config->get('service_option'), 'option_extras' => $sharethis_config->get('option_extras'), 'widget' => $sharethis_config->get('widget_option'), 'onhover' => $sharethis_config->get('option_onhover'), 'neworzero' => $sharethis_config->get('option_neworzero'), 'twitter_suffix' => $sharethis_config->get('twitter_suffix'), 'twitter_handle' => $sharethis_config->get('twitter_handle'), 'twitter_recommends' => $sharethis_config->get('twitter_recommends'), 'late_load' => $sharethis_config->get('late_load'), 'view_modes' => $view_modes, 'cns' => $sharethis_config->get('cns'), 'callesi' => NULL == $sharethis_config->get('cns') ? 1 : 0, 'node_types' => $sharethis_config->get('node_types'), 'shorten' => $sharethis_config->get('option_shorten')]; }
/** * Define permissions. */ public function permissions() { $permissions = array('assign domain editors' => array('title' => $this->t('Assign additional editors to assigned domains')), 'assign editors to any domain' => array('title' => $this->t('Assign additional editors to any domains')), 'publish to any domain' => array('title' => $this->t('Publish to any domain')), 'publish to any assigned domain' => array('title' => $this->t('Publish content to any assigned domain')), 'create domain content' => array('title' => $this->t('Create any content on assigned domains')), 'edit domain content' => array('title' => $this->t('Edit any content on assigned domains')), 'delete domain content' => array('title' => $this->t('Delete any content on assigned domains')), 'view unpublished domain content' => array('title' => $this->t('View unpublished content on assigned domains'))); // Generate standard node permissions for all applicable node types. foreach (NodeType::loadMultiple() as $type) { $permissions += $this->nodePermissions($type); } return $permissions; }
/** * Create the referrer entity. */ protected function createReferrerEntity() { /** @var \Drupal\node\Entity\Node $node */ $node = entity_create($this->testEntityTypeName, array('title' => $this->randomMachineName(), 'type' => $this->referrerType->id(), 'description' => array('value' => $this->randomMachineName(), 'format' => 'basic_html'), $this->referenceFieldName => array(array('target_id' => $this->referencedEntityWithTranslation->id()), array('target_id' => $this->referencedEntityWithoutTranslation->id())), 'langcode' => $this->baseLangcode)); $node->save(); $node->addTranslation($this->translateToLangcode, $node->toArray()); $node->save(); return $node; }