/** * Tests adding and editing values using metatag. */ public function testMetatag() { // Create a test entity. $edit = ['name[0][value]' => 'Barfoo', 'user_id[0][target_id]' => 'foo (' . $this->adminUser->id() . ')']; $this->drupalPostForm('entity_test/add', $edit, t('Save')); $entities = entity_load_multiple_by_properties('entity_test', ['name' => 'Barfoo']); $this->assertEqual(1, count($entities), 'Entity was saved'); $entity = reset($entities); // Update the Global defaults and test them. $values = array('keywords' => 'Purple monkey dishwasher'); $this->drupalPostForm('admin/structure/metatag_defaults/global', $values, 'Save'); $this->assertText('Saved the Global Metatag defaults.'); $this->drupalGet('entity_test/' . $entity->id()); $elements = $this->cssSelect('meta[name=keywords]'); $this->assertTrue(count($elements) === 1, 'Found keywords metatag from defaults'); $this->assertEqual((string) $elements[0]['content'], $values['keywords'], 'Default keywords applied'); // Tests metatags with urls work. $edit = ['name[0][value]' => 'UrlTags', 'user_id[0][target_id]' => 'foo (' . $this->adminUser->id() . ')', 'field_metatag[0][open_graph][og_url]' => 'http://example.com/foo.html']; $this->drupalPostForm('entity_test/add', $edit, t('Save')); $entities = entity_load_multiple_by_properties('entity_test', ['name' => 'UrlTags']); $this->assertEqual(1, count($entities), 'Entity was saved'); $entity = reset($entities); $this->drupalGet('entity_test/' . $entity->id()); $elements = $this->cssSelect("meta[property='og:url']"); $this->assertTrue(count($elements) === 1, 'Found keywords metatag from defaults'); $this->assertEqual((string) $elements[0]['content'], $edit['field_metatag[0][open_graph][og_url]']); }
/** * Loads a test entity by name always resetting the storage cache. */ protected function loadEntityByName($entity_type, $name) { // Always load the entity from the database to ensure that changes are // correctly picked up. $this->container->get('entity.manager')->getStorage($entity_type)->resetCache(); return current(entity_load_multiple_by_properties($entity_type, array('name' => $name))); }
/** * @file * Save Breadcrumb CSS to file */ function at_core_submit_mobile_blocks($values, $theme, $generated_files_path) { $mobile_blocks_css = array(); $theme_blocks = entity_load_multiple_by_properties('block', ['theme' => $theme]); if (!empty($theme_blocks)) { foreach ($theme_blocks as $block_key => $block_values) { $block_id = $block_values->id(); if (isset($values['settings_mobile_block_show_' . $block_id]) && $values['settings_mobile_block_show_' . $block_id] == 1) { $block_selector = '#' . Html::getUniqueId('block-' . $block_id); $mobile_blocks_css[] = $block_selector . ' {display:none}' . "\n"; $mobile_blocks_css[] = '.is-mobile ' . $block_selector . ' {display:block}' . "\n"; } if (isset($values['settings_mobile_block_hide_' . $block_id]) && $values['settings_mobile_block_hide_' . $block_id] == 1) { $block_selector = '#' . Html::getUniqueId('block-' . $block_id); $mobile_blocks_css[] = '.is-mobile ' . $block_selector . ' {display:none}' . "\n"; $mobile_blocks_css[] = $block_selector . ' {display:block}' . "\n"; } } } if (!empty($mobile_blocks_css)) { $file_name = 'mobile-blocks.css'; $filepath = $generated_files_path . '/' . $file_name; file_unmanaged_save_data($mobile_blocks_css, $filepath, FILE_EXISTS_REPLACE); } }
/** * Tests creation of feeds with a language. */ public function testFeedLanguage() { $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages', 'administer news feeds', 'access news feeds', 'create article content']); $this->drupalLogin($admin_user); // Enable language selection for feeds. $edit['entity_types[aggregator_feed]'] = TRUE; $edit['settings[aggregator_feed][aggregator_feed][settings][language][language_alterable]'] = TRUE; $this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration')); /** @var \Drupal\aggregator\FeedInterface[] $feeds */ $feeds = array(); // Create feeds. $feeds[1] = $this->createFeed(NULL, array('langcode[0][value]' => $this->langcodes[1])); $feeds[2] = $this->createFeed(NULL, array('langcode[0][value]' => $this->langcodes[2])); // Make sure that the language has been assigned. $this->assertEqual($feeds[1]->language()->getId(), $this->langcodes[1]); $this->assertEqual($feeds[2]->language()->getId(), $this->langcodes[2]); // Create example nodes to create feed items from and then update the feeds. $this->createSampleNodes(); $this->cronRun(); // Loop over the created feed items and verify that their language matches // the one from the feed. foreach ($feeds as $feed) { /** @var \Drupal\aggregator\ItemInterface[] $items */ $items = entity_load_multiple_by_properties('aggregator_item', array('fid' => $feed->id())); $this->assertTrue(count($items) > 0, 'Feed items were created.'); foreach ($items as $item) { $this->assertEqual($item->language()->getId(), $feed->language()->getId()); } } }
/** * Creates four nodes and ensures that they are loaded correctly. */ function testNodeMultipleLoad() { $node1 = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1)); $node2 = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1)); $node3 = $this->drupalCreateNode(array('type' => 'article', 'promote' => 0)); $node4 = $this->drupalCreateNode(array('type' => 'page', 'promote' => 0)); // Confirm that promoted nodes appear in the default node listing. $this->drupalGet('node'); $this->assertText($node1->label(), 'Node title appears on the default listing.'); $this->assertText($node2->label(), 'Node title appears on the default listing.'); $this->assertNoText($node3->label(), 'Node title does not appear in the default listing.'); $this->assertNoText($node4->label(), 'Node title does not appear in the default listing.'); // Load nodes with only a condition. Nodes 3 and 4 will be loaded. $nodes = entity_load_multiple_by_properties('node', array('promote' => 0)); $this->assertEqual($node3->label(), $nodes[$node3->id()]->label(), 'Node was loaded.'); $this->assertEqual($node4->label(), $nodes[$node4->id()]->label(), 'Node was loaded.'); $count = count($nodes); $this->assertTrue($count == 2, format_string('@count nodes loaded.', array('@count' => $count))); // Load nodes by nid. Nodes 1, 2 and 4 will be loaded. $nodes = Node::loadMultiple(array(1, 2, 4)); $count = count($nodes); $this->assertTrue(count($nodes) == 3, format_string('@count nodes loaded', array('@count' => $count))); $this->assertTrue(isset($nodes[$node1->id()]), 'Node is correctly keyed in the array'); $this->assertTrue(isset($nodes[$node2->id()]), 'Node is correctly keyed in the array'); $this->assertTrue(isset($nodes[$node4->id()]), 'Node is correctly keyed in the array'); foreach ($nodes as $node) { $this->assertTrue(is_object($node), 'Node is an object'); } }
public function testTaxonomyImageAccess() { $user = $this->drupalCreateUser(array('administer site configuration', 'administer taxonomy', 'access user profiles')); $this->drupalLogin($user); // Create a term and upload the image. $files = $this->drupalGetTestFiles('image'); $image = array_pop($files); $edit['name[0][value]'] = $this->randomMachineName(); $edit['files[field_test_0]'] = drupal_realpath($image->uri); $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add', $edit, t('Save')); $this->drupalPostForm(NULL, ['field_test[0][alt]' => $this->randomMachineName()], t('Save')); $terms = entity_load_multiple_by_properties('taxonomy_term', array('name' => $edit['name[0][value]'])); $term = reset($terms); $this->assertText(t('Created new term @name.', array('@name' => $term->getName()))); // Create a user that should have access to the file and one that doesn't. $access_user = $this->drupalCreateUser(array('access content')); $no_access_user = $this->drupalCreateUser(); $image = File::load($term->field_test->target_id); $this->drupalLogin($access_user); $this->drupalGet(file_create_url($image->getFileUri())); $this->assertResponse(200, 'Private image on term is accessible with right permission'); $this->drupalLogin($no_access_user); $this->drupalGet(file_create_url($image->getFileUri())); $this->assertResponse(403, 'Private image on term not accessible without right permission'); }
/** * Tests if forum module uninstallation properly deletes the field. */ public function testForumUninstallWithField() { $this->drupalLogin($this->drupalCreateUser(['administer taxonomy', 'administer nodes', 'administer modules', 'delete any forum content', 'administer content types'])); // Ensure that the field exists before uninstallation. $field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums'); $this->assertNotNull($field_storage, 'The taxonomy_forums field storage exists.'); // Create a taxonomy term. $term = entity_create('taxonomy_term', array('name' => t('A term'), 'langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId(), 'description' => '', 'parent' => array(0), 'vid' => 'forums', 'forum_container' => 0)); $term->save(); // Create a forum node. $node = $this->drupalCreateNode(array('title' => 'A forum post', 'type' => 'forum', 'taxonomy_forums' => array(array('target_id' => $term->id())))); // Create at least one comment against the forum node. $comment = entity_create('comment', array('entity_id' => $node->nid->value, 'entity_type' => 'node', 'field_name' => 'comment_forum', 'pid' => 0, 'uid' => 0, 'status' => CommentInterface::PUBLISHED, 'subject' => $this->randomMachineName(), 'hostname' => '127.0.0.1')); $comment->save(); // Attempt to uninstall forum. $this->drupalGet('admin/modules/uninstall'); // Assert forum is required. $this->assertNoFieldByName('uninstall[forum]'); $this->drupalGet('admin/modules'); $this->assertText('To uninstall Forum first delete all Forum content'); // Delete the node. $this->drupalPostForm('node/' . $node->id() . '/delete', array(), t('Delete')); // Attempt to uninstall forum. $this->drupalGet('admin/modules/uninstall'); // Assert forum is still required. $this->assertNoFieldByName('uninstall[forum]'); $this->drupalGet('admin/modules'); $this->assertText('To uninstall Forum first delete all Forums terms'); // Delete any forum terms. $vid = $this->config('forum.settings')->get('vocabulary'); $terms = entity_load_multiple_by_properties('taxonomy_term', ['vid' => $vid]); foreach ($terms as $term) { $term->delete(); } // Ensure that the forum node type can not be deleted. $this->drupalGet('admin/structure/types/manage/forum'); $this->assertNoLink(t('Delete')); // Now attempt to uninstall forum. $this->drupalGet('admin/modules/uninstall'); // Assert forum is no longer required. $this->assertFieldByName('uninstall[forum]'); $this->drupalGet('admin/modules'); $this->assertNoText('To uninstall Forum first delete all Forum content'); $this->drupalPostForm('admin/modules/uninstall', array('uninstall[forum]' => 1), t('Uninstall')); $this->drupalPostForm(NULL, [], t('Uninstall')); // Check that the field is now deleted. $field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums'); $this->assertNull($field_storage, 'The taxonomy_forums field storage has been deleted.'); // Check that a node type with a machine name of forum can be created after // uninstalling the forum module and the node type is not locked. $edit = array('name' => 'Forum', 'title_label' => 'title for forum', 'type' => 'forum'); $this->drupalPostForm('admin/structure/types/add', $edit, t('Save content type')); $this->assertTrue((bool) NodeType::load('forum'), 'Node type with machine forum created.'); $this->drupalGet('admin/structure/types/manage/forum'); $this->clickLink(t('Delete')); $this->drupalPostForm(NULL, array(), t('Delete')); $this->assertResponse(200); $this->assertFalse((bool) NodeType::load('forum'), 'Node type with machine forum deleted.'); }
/** * Purges a field data. */ public static function purgeFieldData() { do { field_purge_batch(1000); $properties = array('deleted' => TRUE, 'include_deleted' => TRUE); $fields = entity_load_multiple_by_properties('field_config', $properties); } while ($fields); }
/** * Test deleting functionality. */ public function testDelete() { $feed = $this->createFeed(); $this->updateAndDelete($feed, NULL); // Make sure the feed title is changed. $entities = entity_load_multiple_by_properties('aggregator_feed', array('description' => $feed->description->value)); $this->assertTrue(empty($entities)); }
/** * Get a custom block from the database based on its title. * * @param $info * A block title, usually generated by $this->randomMachineName(). * @param $reset * (optional) Whether to reset the entity cache. * * @return \Drupal\block\BlockInterface * A block entity matching $info. */ function drupalGetBlockByInfo($info, $reset = FALSE) { if ($reset) { \Drupal::entityManager()->getStorage('block_content')->resetCache(); } $blocks = entity_load_multiple_by_properties('block_content', array('info' => $info)); // Get the first block returned from the database. $returned_block = reset($blocks); return $returned_block; }
/** * Deleting terms should also remove related vocabulary. * Deleting an invalid term should silently fail. */ public function testTermDelete() { $vocabulary = $this->createVocabulary(); $valid_term = $this->createTerm($vocabulary); // Delete a valid term. $valid_term->delete(); $terms = entity_load_multiple_by_properties('taxonomy_term', array('vid' => $vocabulary->id())); $this->assertTrue(empty($terms), 'Vocabulary is empty after deletion'); // Delete an invalid term. Should not throw any notices. entity_delete_multiple('taxonomy_term', array(42)); }
/** * Enrich the $object that is about to be saved with arbitrary * information in each of its fields. */ public static function generateFields(EntityInterface &$object, $entity_type, $bundle_name, $form_mode = 'default', $namespace = 'devel_generate') { $instances = entity_load_multiple_by_properties('field_instance_config', array('entity_type' => $entity_type, 'bundle' => $bundle_name)); $field_types = \Drupal::service('plugin.manager.field.field_type')->getDefinitions(); $skips = function_exists('drush_get_option') ? drush_get_option('skip-fields', '') : @$_REQUEST['skip-fields']; foreach (explode(',', $skips) as $skip) { unset($instances[$skip]); } foreach ($instances as $instance) { $field = $instance->getFieldStorageDefinition(); $cardinality = $field->getCardinality(); $field_name = $field->getName(); $object_field = array(); // If module handles own multiples, then only call its hook once. $form_display_options = entity_get_form_display($entity_type, $bundle_name, $form_mode)->getComponent($field_name); $plugin_definition = \Drupal::service('plugin.manager.field.widget')->getDefinition($form_display_options['type']); if (isset($plugin_definition['multiple_values']) && $plugin_definition['multiple_values'] === TRUE) { $max = 0; } else { switch ($cardinality) { case FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED: $max = rand(0, 3); //just an arbitrary number for 'unlimited' break; default: $max = $cardinality - 1; break; } } for ($i = 0; $i <= $max; $i++) { $provider = $field_types[$field->type]['provider']; if (!in_array($provider, array('file', 'image', 'taxonomy', 'number', 'text', 'options', 'email', 'link'))) { continue; } $devel_generate_field_factory = new DevelGenerateFieldFactory(); $devel_generate_field_object = $devel_generate_field_factory->createInstance($provider, $namespace); if ($devel_generate_field_object instanceof DevelGenerateFieldBaseInterface) { if ($result = $devel_generate_field_object->generate($object, $instance, $plugin_definition, $form_display_options)) { if (isset($plugin_definition['multiple_values']) && $plugin_definition['multiple_values'] === TRUE) { // Fields that handle their own multiples will add their own deltas. $object_field = $result; } else { // When multiples are handled by the content module, add a delta for each result. $object_field[$i] = $result; } } } } $object->{$field_name} = $object_field; } }
/** * Tests menu language settings and the defaults for menu link items. */ function testMenuLanguage() { // Create a test menu to test the various language-related settings. // Machine name has to be lowercase. $menu_name = Unicode::strtolower($this->randomMachineName(16)); $label = $this->randomString(); $edit = array('id' => $menu_name, 'description' => '', 'label' => $label, 'langcode' => 'aa'); $this->drupalPostForm('admin/structure/menu/add', $edit, t('Save')); language_save_default_configuration('menu_link_content', 'menu_link_content', array('langcode' => 'bb', 'language_show' => TRUE)); // Check menu language. $this->assertOptionSelected('edit-langcode', $edit['langcode'], 'The menu language was correctly selected.'); // Test menu link language. $link_path = '<front>'; // Add a menu link. $link_title = $this->randomString(); $edit = array('title[0][value]' => $link_title, 'url' => $link_path); $this->drupalPostForm("admin/structure/menu/manage/{$menu_name}/add", $edit, t('Save')); // Check the link was added with the correct menu link default language. $menu_links = entity_load_multiple_by_properties('menu_link_content', array('title' => $link_title)); $menu_link = reset($menu_links); $this->assertMenuLink($menu_link->getPluginId(), array('menu_name' => $menu_name, 'route_name' => '<front>', 'langcode' => 'bb')); // Edit menu link default, changing it to cc. language_save_default_configuration('menu_link_content', 'menu_link_content', array('langcode' => 'cc', 'language_show' => TRUE)); // Add a menu link. $link_title = $this->randomString(); $edit = array('title[0][value]' => $link_title, 'url' => $link_path); $this->drupalPostForm("admin/structure/menu/manage/{$menu_name}/add", $edit, t('Save')); // Check the link was added with the correct new menu link default language. $menu_links = entity_load_multiple_by_properties('menu_link_content', array('title' => $link_title)); $menu_link = reset($menu_links); $this->assertMenuLink($menu_link->getPluginId(), array('menu_name' => $menu_name, 'route_name' => '<front>', 'langcode' => 'cc')); // Now change the language of the new link to 'bb'. $edit = array('langcode' => 'bb'); $this->drupalPostForm('admin/structure/menu/item/' . $menu_link->id() . '/edit', $edit, t('Save')); $this->assertMenuLink($menu_link->getPluginId(), array('menu_name' => $menu_name, 'route_name' => '<front>', 'langcode' => 'bb')); // Saving menu link items ends up on the edit menu page. To check the menu // link has the correct language default on edit, go to the menu link edit // page first. $this->drupalGet('admin/structure/menu/item/' . $menu_link->id() . '/edit'); // Check that the language selector has the correct default value. $this->assertOptionSelected('edit-langcode', 'bb', 'The menu link language was correctly selected.'); // Edit menu to hide the language select on menu link item add. language_save_default_configuration('menu_link_content', 'menu_link_content', array('langcode' => 'cc', 'language_show' => FALSE)); // Check that the language selector is not available on menu link add page. $this->drupalGet("admin/structure/menu/manage/{$menu_name}/add"); $this->assertNoField('edit-langcode', 'The language selector field was hidden the page'); }
/** * Populate the fields on a given entity with sample values. * * @param $entity * The entity to be enriched with sample field values. */ public function populateFields(EntityInterface $entity) { $instances = entity_load_multiple_by_properties('field_instance_config', array('entity_type' => $entity->getEntityType()->id(), 'bundle' => $entity->bundle())); if ($skips = function_exists('drush_get_option') ? drush_get_option('skip-fields', '') : @$_REQUEST['skip-fields']) { foreach (explode(',', $skips) as $skip) { unset($instances[$skip]); } } foreach ($instances as $instance) { /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */ $field_storage = $instance->getFieldStorageDefinition(); $max = $cardinality = $field_storage->getCardinality(); if ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) { // Just an arbitrary number for 'unlimited' $max = rand(1, 3); } $field_name = $field_storage->getName(); $entity->{$field_name}->generateSampleItems($max); } }
/** * Executes a test set for a defined entity type and user. * * @param string $entity_type * The entity type to run the tests with. * @param \Drupal\user\UserInterface $user1 * The user to run the tests with. */ protected function assertCRUD($entity_type, UserInterface $user1) { // Create some test entities. $entity = entity_create($entity_type, array('name' => 'test', 'user_id' => $user1->id())); $entity->save(); $entity = entity_create($entity_type, array('name' => 'test2', 'user_id' => $user1->id())); $entity->save(); $entity = entity_create($entity_type, array('name' => 'test', 'user_id' => NULL)); $entity->save(); $entities = array_values(entity_load_multiple_by_properties($entity_type, array('name' => 'test'))); $this->assertEqual($entities[0]->name->value, 'test', format_string('%entity_type: Created and loaded entity', array('%entity_type' => $entity_type))); $this->assertEqual($entities[1]->name->value, 'test', format_string('%entity_type: Created and loaded entity', array('%entity_type' => $entity_type))); // Test loading a single entity. $loaded_entity = entity_load($entity_type, $entity->id()); $this->assertEqual($loaded_entity->id(), $entity->id(), format_string('%entity_type: Loaded a single entity by id.', array('%entity_type' => $entity_type))); // Test deleting an entity. $entities = array_values(entity_load_multiple_by_properties($entity_type, array('name' => 'test2'))); $entities[0]->delete(); $entities = array_values(entity_load_multiple_by_properties($entity_type, array('name' => 'test2'))); $this->assertEqual($entities, array(), format_string('%entity_type: Entity deleted.', array('%entity_type' => $entity_type))); // Test updating an entity. $entities = array_values(entity_load_multiple_by_properties($entity_type, array('name' => 'test'))); $entities[0]->name->value = 'test3'; $entities[0]->save(); $entity = entity_load($entity_type, $entities[0]->id()); $this->assertEqual($entity->name->value, 'test3', format_string('%entity_type: Entity updated.', array('%entity_type' => $entity_type))); // Try deleting multiple test entities by deleting all. $ids = array_keys(entity_load_multiple($entity_type)); entity_delete_multiple($entity_type, $ids); $all = entity_load_multiple($entity_type); $this->assertTrue(empty($all), format_string('%entity_type: Deleted all entities.', array('%entity_type' => $entity_type))); // Verify that all data got deleted. $definition = \Drupal::entityManager()->getDefinition($entity_type); $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {' . $definition->getBaseTable() . '}')->fetchField(), 'Base table was emptied'); if ($data_table = $definition->getDataTable()) { $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {' . $data_table . '}')->fetchField(), 'Data table was emptied'); } if ($revision_table = $definition->getRevisionTable()) { $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {' . $revision_table . '}')->fetchField(), 'Data table was emptied'); } }
/** * This will test loading file data from the database. */ function testMultiple() { // Create a new file entity. $file = $this->createFile('druplicon.txt', NULL, 'public'); // Load by path. file_test_reset(); $by_path_files = entity_load_multiple_by_properties('file', array('uri' => $file->getFileUri())); $this->assertFileHookCalled('load'); $this->assertEqual(1, count($by_path_files), 'file_load_multiple() returned an array of the correct size.'); $by_path_file = reset($by_path_files); $this->assertTrue($by_path_file->file_test['loaded'], 'file_test_file_load() was able to modify the file during load.'); $this->assertEqual($by_path_file->id(), $file->id(), 'Loading by filepath got the correct fid.', 'File'); // Load by fid. file_test_reset(); $by_fid_files = file_load_multiple(array($file->id())); $this->assertFileHooksCalled(array()); $this->assertEqual(1, count($by_fid_files), 'file_load_multiple() returned an array of the correct size.'); $by_fid_file = reset($by_fid_files); $this->assertTrue($by_fid_file->file_test['loaded'], 'file_test_file_load() was able to modify the file during load.'); $this->assertEqual($by_fid_file->getFileUri(), $file->getFileUri(), 'Loading by fid got the correct filepath.', 'File'); }
/** * Tests creation of feeds with a language. */ public function testFeedLanguage() { $feeds = array(); // Create feeds. $feeds[1] = $this->createFeed(NULL, array('langcode' => $this->langcodes[1]->id)); $feeds[2] = $this->createFeed(NULL, array('langcode' => $this->langcodes[2]->id)); // Make sure that the language has been assigned. $this->assertEqual($feeds[1]->language()->id, $this->langcodes[1]->id); $this->assertEqual($feeds[2]->language()->id, $this->langcodes[2]->id); // Create example nodes to create feed items from and then update the feeds. $this->createSampleNodes(); $this->cronRun(); // Loop over the created feed items and verify that their language matches // the one from the feed. foreach ($feeds as $feed) { $items = entity_load_multiple_by_properties('aggregator_item', array('fid' => $feed->id())); $this->assertTrue(count($items) > 0, 'Feed items were created.'); foreach ($items as $item) { $this->assertEqual($item->language()->id, $feed->language()->id); } } }
/** * Tests menus. */ public function testMenus() { // Assert that main menu is not included in the sitemap by default. $this->drupalGet('/sitemap'); $elements = $this->cssSelect(".sitemap-box h2:contains('Main navigation')"); $this->assertEqual(count($elements), 0, 'Main menu is not included.'); // Configure module to show main menu, with enabled menu items only. $edit = array('show_menus[main]' => 'main', 'show_menus_hidden' => FALSE); $this->drupalPostForm('admin/config/search/sitemap', $edit, t('Save configuration')); // Create dummy node with enabled menu item. $node_1_title = $this->randomString(); $edit = array('title[0][value]' => $node_1_title, 'menu[enabled]' => TRUE, 'menu[title]' => $node_1_title, 'menu[menu_parent]' => 'main:'); $this->drupalPostForm('node/add/article', $edit, t('Save and publish')); // Create dummy node with disabled menu item. $node_2_title = $this->randomString(); $edit = array('title[0][value]' => $node_2_title, 'menu[enabled]' => TRUE, 'menu[title]' => $node_2_title, 'menu[menu_parent]' => 'main:'); $this->drupalPostForm('node/add/article', $edit, t('Save and publish')); // Disable menu item. $menu_links = entity_load_multiple_by_properties('menu_link_content', array('title' => $node_2_title)); $menu_link = reset($menu_links); $mlid = $menu_link->id(); $edit = array('enabled[value]' => FALSE); $this->drupalPostForm("admin/structure/menu/item/{$mlid}/edit", $edit, t('Save')); // Assert that main menu is included in the sitemap. $this->drupalGet('/sitemap'); $elements = $this->cssSelect(".sitemap-box h2:contains('Main navigation')"); $this->assertEqual(count($elements), 1, 'Main menu is included.'); // Assert that node 1 is listed in the sitemap, but not node 2. $this->assertLink($node_1_title); $this->assertNoLink($node_2_title); // Configure module to show all menu items. $edit = array('show_menus_hidden' => TRUE); $this->drupalPostForm('admin/config/search/sitemap', $edit, t('Save configuration')); // Assert that both node 1 and node 2 are listed in the sitemap. $this->drupalGet('/sitemap'); $this->assertLink($node_1_title); $this->assertLink($node_2_title); }
/** * {@inheritdoc} */ public function process(FeedInterface $feed) { if (!is_array($feed->items)) { return; } foreach ($feed->items as $item) { // @todo: The default entity view builder always returns an empty // array, which is ignored in aggregator_save_item() currently. Should // probably be fixed. if (empty($item['title'])) { continue; } // Save this item. Try to avoid duplicate entries as much as possible. If // we find a duplicate entry, we resolve it and pass along its ID is such // that we can update it if needed. if (!empty($item['guid'])) { $values = array('fid' => $feed->id(), 'guid' => $item['guid']); } elseif ($item['link'] && $item['link'] != $feed->link && $item['link'] != $feed->url) { $values = array('fid' => $feed->id(), 'link' => $item['link']); } else { $values = array('fid' => $feed->id(), 'title' => $item['title']); } // Try to load an existing entry. if ($entry = entity_load_multiple_by_properties('aggregator_item', $values)) { $entry = reset($entry); } else { $entry = entity_create('aggregator_item', array('langcode' => $feed->language()->getId())); } if ($item['timestamp']) { $entry->setPostedTime($item['timestamp']); } // Make sure the item title and author fit in the 255 varchar column. $entry->setTitle(truncate_utf8($item['title'], 255, TRUE, TRUE)); $entry->setAuthor(truncate_utf8($item['author'], 255, TRUE, TRUE)); $entry->setFeedId($feed->id()); $entry->setLink($item['link']); $entry->setGuid($item['guid']); $description = ''; if (!empty($item['description'])) { $description = $item['description']; } $entry->setDescription($description); $entry->save(); } }
/** * Tests that parent options are limited by depth when adding menu links. */ function checkInvalidParentMenuLinks() { $last_link = null; $created_links = array(); // Get the max depth of the tree. $menu_link_tree = \Drupal::service('menu.link_tree'); $max_depth = $menu_link_tree->maxDepth(); // Create a maximum number of menu links, each a child of the previous. for ($i = 0; $i <= $max_depth - 1; $i++) { $parent = $last_link ? 'tools:' . $last_link->getPluginId() : 'tools:'; $title = 'title' . $i; $edit = array('link[0][uri]' => '/', 'title[0][value]' => $title, 'menu_parent' => $parent, 'description[0][value]' => '', 'enabled[value]' => 1, 'expanded[value]' => FALSE, 'weight[0][value]' => '0'); $this->drupalPostForm("admin/structure/menu/manage/{$this->menu->id()}/add", $edit, t('Save')); $menu_links = entity_load_multiple_by_properties('menu_link_content', array('title' => $title)); $last_link = reset($menu_links); $created_links[] = 'tools:' . $last_link->getPluginId(); } // The last link cannot be a parent in the new menu link form. $this->drupalGet('admin/structure/menu/manage/admin/add'); $value = 'tools:' . $last_link->getPluginId(); $this->assertNoOption('edit-menu-parent', $value, 'The invalid option is not there.'); // All but the last link can be parents in the new menu link form. array_pop($created_links); foreach ($created_links as $key => $link) { $this->assertOption('edit-menu-parent', $link, 'The valid option number ' . ($key + 1) . ' is there.'); } }
/** * Asserts that the reference field values are correct. * * @param string $entity_name * The name of the test entity. * @param \Drupal\Core\Entity\EntityInterface[] $referenced_entities * An array of referenced entities. */ protected function assertFieldValues($entity_name, $referenced_entities) { $entity = current(entity_load_multiple_by_properties($this->entityType, array('name' => $entity_name))); $this->assertTrue($entity, format_string('%entity_type: Entity found in the database.', array('%entity_type' => $this->entityType))); $this->assertEqual($entity->{$this->fieldName}->target_id, $referenced_entities[0]->id()); $this->assertEqual($entity->{$this->fieldName}->entity->id(), $referenced_entities[0]->id()); $this->assertEqual($entity->{$this->fieldName}->entity->label(), $referenced_entities[0]->label()); $this->assertEqual($entity->{$this->fieldName}[1]->target_id, $referenced_entities[1]->id()); $this->assertEqual($entity->{$this->fieldName}[1]->entity->id(), $referenced_entities[1]->id()); $this->assertEqual($entity->{$this->fieldName}[1]->entity->label(), $referenced_entities[1]->label()); }
/** * Uninstalls a module and asserts that it was done correctly. * * @param string $module * The name of the module to uninstall. * @param string $package * (optional) The package of the module to uninstall. Defaults * to 'Core'. */ protected function assertSuccessfulUninstall($module, $package = 'Core') { $edit = array(); if ($module == 'forum') { // Forum cannot be uninstalled until all of the content entities related // to it have been deleted. $vid = $this->config('forum.settings')->get('vocabulary'); $terms = entity_load_multiple_by_properties('taxonomy_term', ['vid' => $vid]); foreach ($terms as $term) { $term->delete(); } } $edit['uninstall[' . $module . ']'] = TRUE; $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall')); $this->drupalPostForm(NULL, NULL, t('Uninstall')); $this->assertText(t('The selected modules have been uninstalled.'), 'Modules status has been updated.'); $this->assertModules(array($module), FALSE); // Check that the appropriate hook was fired and the appropriate log // message appears. (But don't check for the log message if the dblog // module was just uninstalled, since the {watchdog} table won't be there // anymore.) $this->assertText(t('hook_modules_uninstalled fired for @module', array('@module' => $module))); $this->assertLogMessage('system', "%module module uninstalled.", array('%module' => $module), RfcLogLevel::INFO); // Check that the module's database tables no longer exist. $this->assertModuleTablesDoNotExist($module); // Check that the module's config files no longer exist. $this->assertNoModuleConfig($module); $this->assertUninstallModuleUpdates($module); }
public function adminSummary() { // set up $this->valueOptions for the parent summary $this->valueOptions = array(); if ($this->value) { $result = entity_load_multiple_by_properties('user', array('uid' => $this->value)); foreach ($result as $account) { if ($account->id()) { $this->valueOptions[$account->id()] = $account->label(); } else { $this->valueOptions[$account->id()] = 'Anonymous'; // Intentionally NOT translated. } } } return parent::adminSummary(); }
/** * Adds a menu link using the UI. * * @param string $parent * Optional parent menu link id. * @param string $path * The path to enter on the form. Defaults to the front page. * @param string $menu_name * Menu name. Defaults to 'tools'. * @param bool $expanded * Whether or not this menu link is expanded. Setting this to TRUE should * test whether it works when we do the authenticated_user tests. Defaults * to FALSE. * @param string $weight * Menu weight. Defaults to 0. * * @return \Drupal\menu_link_content\Entity\MenuLinkContent * A menu link entity. */ function addMenuLink($parent = '', $path = '<front>', $menu_name = 'tools', $expanded = FALSE, $weight = '0') { // View add menu link page. $this->drupalGet("admin/structure/menu/manage/{$menu_name}/add"); $this->assertResponse(200); $title = '!link_' . $this->randomMachineName(16); $edit = array('url' => $path, 'title[0][value]' => $title, 'description[0][value]' => '', 'enabled' => 1, 'expanded[value]' => $expanded, 'menu_parent' => $menu_name . ':' . $parent, 'weight[0][value]' => $weight); // Add menu link. $this->drupalPostForm(NULL, $edit, t('Save')); $this->assertResponse(200); $this->assertText('The menu link has been saved.'); $menu_links = entity_load_multiple_by_properties('menu_link_content', array('title' => $title)); $menu_link = reset($menu_links); $this->assertTrue($menu_link, 'Menu link was found in database.'); $this->assertMenuLink($menu_link->getPluginId(), array('menu_name' => $menu_name, 'children' => array(), 'parent' => $parent)); return $menu_link; }
/** * Tests attempting to uninstall a module that has installed dependents. */ function testUninstallDependents() { // Enable the forum module. $edit = array('modules[Core][forum][enable]' => 'forum'); $this->drupalPostForm('admin/modules', $edit, t('Save configuration')); $this->drupalPostForm(NULL, array(), t('Continue')); $this->assertModules(array('forum'), TRUE); // Check that the comment module cannot be uninstalled. $this->drupalGet('admin/modules/uninstall'); $checkbox = $this->xpath('//input[@type="checkbox" and @name="uninstall[comment]"]'); $this->assert(count($checkbox) == 0, 'Checkbox for uninstalling the comment module not found.'); // Delete any forum terms. $vid = \Drupal::config('forum.settings')->get('vocabulary'); // Ensure taxonomy has been loaded into the test-runner after forum was // enabled. \Drupal::moduleHandler()->load('taxonomy'); $terms = entity_load_multiple_by_properties('taxonomy_term', ['vid' => $vid]); foreach ($terms as $term) { $term->delete(); } // Uninstall the forum module, and check that taxonomy now can also be // uninstalled. $edit = array('uninstall[forum]' => 'forum'); $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall')); $this->drupalPostForm(NULL, NULL, t('Uninstall')); $this->assertText(t('The selected modules have been uninstalled.'), 'Modules status has been updated.'); // Uninstall comment module. $edit = array('uninstall[comment]' => 'comment'); $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall')); $this->drupalPostForm(NULL, NULL, t('Uninstall')); $this->assertText(t('The selected modules have been uninstalled.'), 'Modules status has been updated.'); }
/** * Test the deletion of a field. */ function testDeleteField() { // TODO: Test deletion of the data stored in the field also. // Need to check that data for a 'deleted' field / storage doesn't get loaded // Need to check data marked deleted is cleaned on cron (not implemented yet...) // Create two fields for the same field storage so we can test that only one // is deleted. FieldConfig::create($this->fieldDefinition)->save(); $another_field_definition = $this->fieldDefinition; $another_field_definition['bundle'] .= '_another_bundle'; entity_test_create_bundle($another_field_definition['bundle']); FieldConfig::create($another_field_definition)->save(); // Test that the first field is not deleted, and then delete it. $field = current(entity_load_multiple_by_properties('field_config', array('entity_type' => 'entity_test', 'field_name' => $this->fieldDefinition['field_name'], 'bundle' => $this->fieldDefinition['bundle'], 'include_deleted' => TRUE))); $this->assertTrue(!empty($field) && empty($field->deleted), 'A new field is not marked for deletion.'); $field->delete(); // Make sure the field is marked as deleted when it is specifically loaded. $field = current(entity_load_multiple_by_properties('field_config', array('entity_type' => 'entity_test', 'field_name' => $this->fieldDefinition['field_name'], 'bundle' => $this->fieldDefinition['bundle'], 'include_deleted' => TRUE))); $this->assertTrue($field->isDeleted(), 'A deleted field is marked for deletion.'); // Try to load the field normally and make sure it does not show up. $field = FieldConfig::load('entity_test.' . '.' . $this->fieldDefinition['bundle'] . '.' . $this->fieldDefinition['field_name']); $this->assertTrue(empty($field), 'A deleted field is not loaded by default.'); // Make sure the other field is not deleted. $another_field = FieldConfig::load('entity_test.' . $another_field_definition['bundle'] . '.' . $another_field_definition['field_name']); $this->assertTrue(!empty($another_field) && empty($another_field->deleted), 'A non-deleted field is not marked for deletion.'); }
/** * Executes the multilingual property tests for the given entity type. * * @param string $entity_type * The entity type to run the tests with. */ protected function _testMultilingualProperties($entity_type) { $name = $this->randomMachineName(); $uid = mt_rand(0, 127); $langcode = $this->langcodes[0]; // Create a language neutral entity and check that properties are stored // as language neutral. $entity = entity_create($entity_type, array('name' => $name, 'user_id' => $uid)); $entity->save(); $entity = entity_load($entity_type, $entity->id()); $default_langcode = $entity->language()->id; $this->assertEqual($default_langcode, LanguageInterface::LANGCODE_NOT_SPECIFIED, format_string('%entity_type: Entity created as language neutral.', array('%entity_type' => $entity_type))); $field = $entity->getTranslation(LanguageInterface::LANGCODE_DEFAULT)->get('name'); $this->assertEqual($name, $field->value, format_string('%entity_type: The entity name has been correctly stored as language neutral.', array('%entity_type' => $entity_type))); $this->assertEqual($default_langcode, $field->getLangcode(), format_string('%entity_type: The field object has the expect langcode.', array('%entity_type' => $entity_type))); $this->assertEqual($uid, $entity->getTranslation(LanguageInterface::LANGCODE_DEFAULT)->get('user_id')->target_id, format_string('%entity_type: The entity author has been correctly stored as language neutral.', array('%entity_type' => $entity_type))); $field = $entity->getTranslation($langcode)->get('name'); $this->assertEqual($name, $field->value, format_string('%entity_type: The entity name defaults to neutral language.', array('%entity_type' => $entity_type))); $this->assertEqual($default_langcode, $field->getLangcode(), format_string('%entity_type: The field object has the expect langcode.', array('%entity_type' => $entity_type))); $this->assertEqual($uid, $entity->getTranslation($langcode)->get('user_id')->target_id, format_string('%entity_type: The entity author defaults to neutral language.', array('%entity_type' => $entity_type))); $field = $entity->get('name'); $this->assertEqual($name, $field->value, format_string('%entity_type: The entity name can be retrieved without specifying a language.', array('%entity_type' => $entity_type))); $this->assertEqual($default_langcode, $field->getLangcode(), format_string('%entity_type: The field object has the expect langcode.', array('%entity_type' => $entity_type))); $this->assertEqual($uid, $entity->get('user_id')->target_id, format_string('%entity_type: The entity author can be retrieved without specifying a language.', array('%entity_type' => $entity_type))); // Create a language-aware entity and check that properties are stored // as language-aware. $entity = entity_create($entity_type, array('name' => $name, 'user_id' => $uid, 'langcode' => $langcode)); $entity->save(); $entity = entity_load($entity_type, $entity->id()); $default_langcode = $entity->language()->id; $this->assertEqual($default_langcode, $langcode, format_string('%entity_type: Entity created as language specific.', array('%entity_type' => $entity_type))); $field = $entity->getTranslation($langcode)->get('name'); $this->assertEqual($name, $field->value, format_string('%entity_type: The entity name has been correctly stored as a language-aware property.', array('%entity_type' => $entity_type))); $this->assertEqual($default_langcode, $field->getLangcode(), format_string('%entity_type: The field object has the expect langcode.', array('%entity_type' => $entity_type))); $this->assertEqual($uid, $entity->getTranslation($langcode)->get('user_id')->target_id, format_string('%entity_type: The entity author has been correctly stored as a language-aware property.', array('%entity_type' => $entity_type))); // Translatable properties on a translatable entity should use default // language if LanguageInterface::LANGCODE_NOT_SPECIFIED is passed. $field = $entity->getTranslation(LanguageInterface::LANGCODE_NOT_SPECIFIED)->get('name'); $this->assertEqual($name, $field->value, format_string('%entity_type: The entity name defaults to the default language.', array('%entity_type' => $entity_type))); $this->assertEqual($default_langcode, $field->getLangcode(), format_string('%entity_type: The field object has the expect langcode.', array('%entity_type' => $entity_type))); $this->assertEqual($uid, $entity->getTranslation(LanguageInterface::LANGCODE_NOT_SPECIFIED)->get('user_id')->target_id, format_string('%entity_type: The entity author defaults to the default language.', array('%entity_type' => $entity_type))); $field = $entity->get('name'); $this->assertEqual($name, $field->value, format_string('%entity_type: The entity name can be retrieved without specifying a language.', array('%entity_type' => $entity_type))); $this->assertEqual($default_langcode, $field->getLangcode(), format_string('%entity_type: The field object has the expect langcode.', array('%entity_type' => $entity_type))); $this->assertEqual($uid, $entity->get('user_id')->target_id, format_string('%entity_type: The entity author can be retrieved without specifying a language.', array('%entity_type' => $entity_type))); // Create property translations. $properties = array(); $default_langcode = $langcode; foreach ($this->langcodes as $langcode) { if ($langcode != $default_langcode) { $properties[$langcode] = array('name' => array(0 => $this->randomMachineName()), 'user_id' => array(0 => mt_rand(128, 256))); } else { $properties[$langcode] = array('name' => array(0 => $name), 'user_id' => array(0 => $uid)); } $translation = $entity->getTranslation($langcode); foreach ($properties[$langcode] as $field_name => $values) { $translation->set($field_name, $values); } } $entity->save(); // Check that property translation were correctly stored. $entity = entity_load($entity_type, $entity->id()); foreach ($this->langcodes as $langcode) { $args = array('%entity_type' => $entity_type, '%langcode' => $langcode); $field = $entity->getTranslation($langcode)->get('name'); $this->assertEqual($properties[$langcode]['name'][0], $field->value, format_string('%entity_type: The entity name has been correctly stored for language %langcode.', $args)); $field_langcode = $langcode == $entity->language()->id ? $default_langcode : $langcode; $this->assertEqual($field_langcode, $field->getLangcode(), format_string('%entity_type: The field object has the expected langcode %langcode.', $args)); $this->assertEqual($properties[$langcode]['user_id'][0], $entity->getTranslation($langcode)->get('user_id')->target_id, format_string('%entity_type: The entity author has been correctly stored for language %langcode.', $args)); } // Test query conditions (cache is reset at each call). $translated_id = $entity->id(); // Create an additional entity with only the uid set. The uid for the // original language is the same of one used for a translation. $langcode = $this->langcodes[1]; entity_create($entity_type, array('user_id' => $properties[$langcode]['user_id'], 'name' => 'some name'))->save(); $entities = entity_load_multiple($entity_type); $this->assertEqual(count($entities), 3, format_string('%entity_type: Three entities were created.', array('%entity_type' => $entity_type))); $entities = entity_load_multiple($entity_type, array($translated_id)); $this->assertEqual(count($entities), 1, format_string('%entity_type: One entity correctly loaded by id.', array('%entity_type' => $entity_type))); $entities = entity_load_multiple_by_properties($entity_type, array('name' => $name)); $this->assertEqual(count($entities), 2, format_string('%entity_type: Two entities correctly loaded by name.', array('%entity_type' => $entity_type))); // @todo The default language condition should go away in favor of an // explicit parameter. $entities = entity_load_multiple_by_properties($entity_type, array('name' => $properties[$langcode]['name'][0], 'default_langcode' => 0)); $this->assertEqual(count($entities), 1, format_string('%entity_type: One entity correctly loaded by name translation.', array('%entity_type' => $entity_type))); $entities = entity_load_multiple_by_properties($entity_type, array('langcode' => $default_langcode, 'name' => $name)); $this->assertEqual(count($entities), 1, format_string('%entity_type: One entity correctly loaded by name and language.', array('%entity_type' => $entity_type))); $entities = entity_load_multiple_by_properties($entity_type, array('langcode' => $langcode, 'name' => $properties[$langcode]['name'][0])); $this->assertEqual(count($entities), 0, format_string('%entity_type: No entity loaded by name translation specifying the translation language.', array('%entity_type' => $entity_type))); $entities = entity_load_multiple_by_properties($entity_type, array('langcode' => $langcode, 'name' => $properties[$langcode]['name'][0], 'default_langcode' => 0)); $this->assertEqual(count($entities), 1, format_string('%entity_type: One entity loaded by name translation and language specifying to look for translations.', array('%entity_type' => $entity_type))); $entities = entity_load_multiple_by_properties($entity_type, array('user_id' => $properties[$langcode]['user_id'][0], 'default_langcode' => NULL)); $this->assertEqual(count($entities), 2, format_string('%entity_type: Two entities loaded by uid without caring about property translatability.', array('%entity_type' => $entity_type))); // Test property conditions and orders with multiple languages in the same // query. $query = \Drupal::entityQuery($entity_type); $group = $query->andConditionGroup()->condition('user_id', $properties[$default_langcode]['user_id'], '=', $default_langcode)->condition('name', $properties[$default_langcode]['name'], '=', $default_langcode); $result = $query->condition($group)->condition('name', $properties[$langcode]['name'], '=', $langcode)->execute(); $this->assertEqual(count($result), 1, format_string('%entity_type: One entity loaded by name and uid using different language meta conditions.', array('%entity_type' => $entity_type))); // Test mixed property and field conditions. $entity = entity_load($entity_type, reset($result), TRUE); $field_value = $this->randomString(); $entity->getTranslation($langcode)->set($this->field_name, array(array('value' => $field_value))); $entity->save(); $query = \Drupal::entityQuery($entity_type); $default_langcode_group = $query->andConditionGroup()->condition('user_id', $properties[$default_langcode]['user_id'], '=', $default_langcode)->condition('name', $properties[$default_langcode]['name'], '=', $default_langcode); $langcode_group = $query->andConditionGroup()->condition('name', $properties[$langcode]['name'], '=', $langcode)->condition("{$this->field_name}.value", $field_value, '=', $langcode); $result = $query->condition('langcode', $default_langcode)->condition($default_langcode_group)->condition($langcode_group)->execute(); $this->assertEqual(count($result), 1, format_string('%entity_type: One entity loaded by name, uid and field value using different language meta conditions.', array('%entity_type' => $entity_type))); }
/** * {@inheritdoc} */ public function getInstances() { return entity_load_multiple_by_properties('block', array('plugin' => 'block_content:' . $this->uuid())); }
/** * Test the deletion of a field storage. */ function testDelete() { // TODO: Also test deletion of the data stored in the field ? // Create two fields (so we can test that only one is deleted). $field_storage_definition = array('field_name' => 'field_1', 'type' => 'test_field', 'entity_type' => 'entity_test'); FieldStorageConfig::create($field_storage_definition)->save(); $another_field_storage_definition = array('field_name' => 'field_2', 'type' => 'test_field', 'entity_type' => 'entity_test'); FieldStorageConfig::create($another_field_storage_definition)->save(); // Create fields for each. $field_definition = array('field_name' => $field_storage_definition['field_name'], 'entity_type' => 'entity_test', 'bundle' => 'entity_test'); FieldConfig::create($field_definition)->save(); $another_field_definition = $field_definition; $another_field_definition['field_name'] = $another_field_storage_definition['field_name']; FieldConfig::create($another_field_definition)->save(); // Test that the first field is not deleted, and then delete it. $field_storage = current(entity_load_multiple_by_properties('field_storage_config', array('field_name' => $field_storage_definition['field_name'], 'include_deleted' => TRUE))); $this->assertTrue(!empty($field_storage) && !$field_storage->isDeleted(), 'A new storage is not marked for deletion.'); FieldStorageConfig::loadByName('entity_test', $field_storage_definition['field_name'])->delete(); // Make sure that the field is marked as deleted when it is specifically // loaded. $field_storage = current(entity_load_multiple_by_properties('field_storage_config', array('field_name' => $field_storage_definition['field_name'], 'include_deleted' => TRUE))); $this->assertTrue($field_storage->isDeleted(), 'A deleted storage is marked for deletion.'); // Make sure that this field is marked as deleted when it is // specifically loaded. $field = current(entity_load_multiple_by_properties('field_config', array('entity_type' => 'entity_test', 'field_name' => $field_definition['field_name'], 'bundle' => $field_definition['bundle'], 'include_deleted' => TRUE))); $this->assertTrue($field->isDeleted(), 'A field whose storage was deleted is marked for deletion.'); // Try to load the storage normally and make sure it does not show up. $field_storage = FieldStorageConfig::load('entity_test.' . $field_storage_definition['field_name']); $this->assertTrue(empty($field_storage), 'A deleted storage is not loaded by default.'); // Try to load the field normally and make sure it does not show up. $field = FieldConfig::load('entity_test.' . '.' . $field_definition['bundle'] . '.' . $field_definition['field_name']); $this->assertTrue(empty($field), 'A field whose storage was deleted is not loaded by default.'); // Make sure the other field and its storage are not deleted. $another_field_storage = FieldStorageConfig::load('entity_test.' . $another_field_storage_definition['field_name']); $this->assertTrue(!empty($another_field_storage) && !$another_field_storage->isDeleted(), 'A non-deleted storage is not marked for deletion.'); $another_field = FieldConfig::load('entity_test.' . $another_field_definition['bundle'] . '.' . $another_field_definition['field_name']); $this->assertTrue(!empty($another_field) && !$another_field->isDeleted(), 'A field whose storage was not deleted is not marked for deletion.'); // Try to create a new field the same name as a deleted field and // write data into it. FieldStorageConfig::create($field_storage_definition)->save(); FieldConfig::create($field_definition)->save(); $field_storage = FieldStorageConfig::load('entity_test.' . $field_storage_definition['field_name']); $this->assertTrue(!empty($field_storage) && !$field_storage->isDeleted(), 'A new storage with a previously used name is created.'); $field = FieldConfig::load('entity_test.' . $field_definition['bundle'] . '.' . $field_definition['field_name']); $this->assertTrue(!empty($field) && !$field->isDeleted(), 'A new field for a previously used field name is created.'); // Save an entity with data for the field $entity = EntityTest::create(); $values[0]['value'] = mt_rand(1, 127); $entity->{$field_storage->getName()}->value = $values[0]['value']; $entity = $this->entitySaveReload($entity); // Verify the field is present on load $this->assertIdentical(count($entity->{$field_storage->getName()}), count($values), "Data in previously deleted field saves and loads correctly"); foreach ($values as $delta => $value) { $this->assertEqual($entity->{$field_storage->getName()}[$delta]->value, $values[$delta]['value'], "Data in previously deleted field saves and loads correctly"); } }
/** * Create a vocabulary and some taxonomy terms, ensuring they're loaded * correctly using entity_load_multiple(). */ function testTaxonomyTermMultipleLoad() { // Create a vocabulary. $vocabulary = $this->createVocabulary(); // Create five terms in the vocabulary. $i = 0; while ($i < 5) { $i++; $this->createTerm($vocabulary); } // Load the terms from the vocabulary. $terms = entity_load_multiple_by_properties('taxonomy_term', array('vid' => $vocabulary->id())); $count = count($terms); $this->assertEqual($count, 5, format_string('Correct number of terms were loaded. @count terms.', array('@count' => $count))); // Load the same terms again by tid. $terms2 = Term::loadMultiple(array_keys($terms)); $this->assertEqual($count, count($terms2), 'Five terms were loaded by tid.'); $this->assertEqual($terms, $terms2, 'Both arrays contain the same terms.'); // Remove one term from the array, then delete it. $deleted = array_shift($terms2); $deleted->delete(); $deleted_term = Term::load($deleted->id()); $this->assertFalse($deleted_term); // Load terms from the vocabulary by vid. $terms3 = entity_load_multiple_by_properties('taxonomy_term', array('vid' => $vocabulary->id())); $this->assertEqual(count($terms3), 4, 'Correct number of terms were loaded.'); $this->assertFalse(isset($terms3[$deleted->id()])); // Create a single term and load it by name. $term = $this->createTerm($vocabulary); $loaded_terms = entity_load_multiple_by_properties('taxonomy_term', array('name' => $term->getName())); $this->assertEqual(count($loaded_terms), 1, 'One term was loaded.'); $loaded_term = reset($loaded_terms); $this->assertEqual($term->id(), $loaded_term->id(), 'Term loaded by name successfully.'); }