/** * Tests using entity query with ContentEntityNullStorage. * * @see \Drupal\Core\Entity\Query\Null\Query */ public function testEntityQuery() { $this->assertIdentical(0, \Drupal::entityQuery('contact_message')->count()->execute(), 'Counting a null storage returns 0.'); $this->assertIdentical([], \Drupal::entityQuery('contact_message')->execute(), 'Querying a null storage returns an empty array.'); $this->assertIdentical([], \Drupal::entityQuery('contact_message')->condition('contact_form', 'test')->execute(), 'Querying a null storage returns an empty array and conditions are ignored.'); $this->assertIdentical([], \Drupal::entityQueryAggregate('contact_message')->aggregate('name', 'AVG')->execute(), 'Aggregate querying a null storage returns an empty array'); }
/** * Tests various code snippets. */ function testSnippets() { $container = \Drupal::getContainer(); // Cache snippets from section "The Drupal Cache" in Chapter 1. // Make up some test data. $bin = 'default'; $cid = 'my_cid'; $data = 'my_data'; $nid = 5; $tags = array('node:' . $nid); // Cache the data. Get the class in two different ways. $cache_class = \Drupal::cache($bin); $this->assertTrue($cache_class, 'Cache class is not null'); $cache_class = $container->get('cache.' . $bin); $cache_class->set($cid, $data, CacheBackendInterface::CACHE_PERMANENT, $tags); // Check that we can retrieve data from the cache. $out = $cache_class->get($cid); $this->outputVariable($out, 'Cache get method output'); $this->assertEqual($data, $out->data, 'Cached data could be retrieved'); // Invalidate the data and check that it cannot be retrieved. Cache::invalidateTags($tags); $out = $cache_class->get($cid); $this->assertFalse($out, 'After invalidating tags, cached data cannot be retrieved'); // Theme snippets from section "Making Your Output Themeable" in Chapter 2. $build['hello'] = array('#input1' => t('Hello World!'), '#theme' => 'mymodule_hookname'); $output = drupal_render_root($build); $expected = '<div>Hello World!</div>'; $this->outputHTML($output, 'Theme template output'); $this->assertEqual(trim($output), $expected, 'Theme template worked in render array'); // Config API snippets from "Configuration API in Drupal 8" section in // chapter 2. // Test reading the settings several different ways. $config = \Drupal::config('mymodule.settings'); $this->assertTrue($config, 'Config class is not null'); $config = $container->get('config.factory')->getEditable('mymodule.settings'); $all = $config->get(); $this->outputVariable($all, 'Full configuration output'); $button_label = $all['submit_button_label']; $this->assertEqual($button_label, 'Submit', 'Read correct button label from overall get'); $button_label = $config->get('submit_button_label'); $this->assertEqual($button_label, 'Submit', 'Read correct button label with specific get'); $name_field_info = $all['name_field_settings']; $name_label = $name_field_info['field_label']; $this->assertEqual($name_label, 'Your name', 'Read correct name field label from overall get'); $name_field_info = $config->get('name_field_settings'); $this->outputVariable($name_field_info, 'Name field configuration output'); $name_label = $name_field_info['field_label']; $this->assertEqual($name_label, 'Your name', 'Read correct name field label from field settings get'); $name_label = $config->get('name_field_settings.field_label'); $this->assertEqual($name_label, 'Your name', 'Read correct name field label from specific get'); // Change the submit label. $new_label = "Save"; $config->set('submit_button_label', $new_label); $config->save(); // Get a new config object, to make sure it was really saved. $new_config = \Drupal::config('mymodule.settings'); $button_label = $new_config->get('submit_button_label'); $this->assertEqual($button_label, 'Save', 'Read correct button label after save'); // State API snippets from "State API in Drupal 8" section in chapter 2. // Get $state in two ways. $state = \Drupal::state(); $this->assertTrue($state, 'State is not null'); $state = $container->get('state'); $value = 'Some test data'; $state->set('mymodule.my_state_variable_name', $value); $new_value = $state->get('mymodule.my_state_variable_name'); $this->assertEqual($value, $new_value, 'State get worked correctly'); $state->delete('mymodule.my_state_variable_name'); $new_value = $state->get('mymodule.my_state_variable_name'); $this->assertNull($new_value, 'After delete, could not retrieve state value'); // Snippets from "Internationalizing User Interface Text" section in // chapter 2. Good code only; bad code examples are omitted. $button_text = t('Save'); $this->assertEqual($button_text, 'Save', 't() worked OK on simple string'); $user_name = 'foo'; $message_string = t('Hello @user_name', array('@user_name' => $user_name)); $this->outputHTML($message_string, 't() with variables output'); $this->assertEqual($message_string, 'Hello foo', 't() worked OK on string with variable'); $test = (object) array(); $foo = $test instanceof MyClass; // Database snippets from "Querying the Database with the Database API" // section in Chapter 2. // Make a blocked user for querying purposes. $account = $this->drupalCreateUser(array()); $account->status = 0; $account->save(); // Query by status 0 (blocked). $desired_status = 0; $found = FALSE; $result = db_query('SELECT * FROM {users_field_data} u WHERE u.status = :status', array(':status' => $desired_status)); foreach ($result as $record) { $this->outputVariable($record, 'User database record'); if ($record->uid == $account->id()) { $found = TRUE; } } $this->assertTrue($found, 'Created user was found by status query'); // Test the ability to query by user name. $found = FALSE; $result = db_query('SELECT * FROM {users_field_data} u WHERE u.name = :name', array(':name' => $account->getUsername())); foreach ($result as $record) { if ($record->uid == $account->id()) { $found = TRUE; } } $this->assertTrue($found, 'Created user was found by name query'); // Create a node, for query purposes. $newnode = $this->drupalCreateNode(); // Log in as user who can access content. $account = $this->drupalCreateUser(array('access content')); $this->drupalLogin($account); $query = db_select('node', 'n'); $query->innerJoin('node_field_data', 'nd', 'n.nid = nd.nid AND n.vid = nd.vid'); $query->innerJoin('users_field_data', 'u', 'u.uid = nd.uid'); $query->addField('nd', 'changed', 'last_updated'); $query->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')->limit(20)->fields('nd', array('title', 'nid'))->fields('u', array('name'))->addTag('node_access')->condition('nd.status', 1); $result = $query->execute(); $found = FALSE; foreach ($result as $node) { $title = $node->title; if ($node->nid == $newnode->id()) { $found = TRUE; $this->verbose("Found node with title {$title}"); } } $this->assertTrue($found, "Found node in query"); // Snippets from the "Cleansing and Checking User-Provided Input" // section in Chapter 2. Only "good" code is included. $text = '<h2>Text with HTML</h2>'; $plain_text = htmlentities($text); $url = 'http://example.com'; $url_object = Url::fromUri($url); $output = Drupal::l($text, $url_object); $this->outputHTML($output, 'l() output'); $this->assertEqual($output, '<a href="' . $url . '">' . $plain_text . '</a>', 'l output is as expected'); // Form builder snippet from "Basic Form Generation and Processing in // Drupal 8", chapter 4. $my_render_array['personal_data_form'] = \Drupal::formBuilder()->getForm('Drupal\\mymodule\\Form\\PersonalDataForm'); $this->outputVariable($my_render_array, 'Personal data form array'); $this->assertTrue(isset($my_render_array['personal_data_form']['first_name']), 'First name field is present in form array'); $builder = $container->get('form_builder'); $my_render_array['personal_data_form'] = $builder->getForm('Drupal\\mymodule\\Form\\PersonalDataForm'); $this->assertTrue(isset($my_render_array['personal_data_form']['first_name']), 'First name field is present in form array'); // Entity query snippets from "Querying and Loading Entities in Drupal 8", // chapter 4. // Try various methods for retrieving the query. $query = \Drupal::entityQuery('node'); $this->assertTrue($query, 'Query is not null'); $query = \Drupal::entityQueryAggregate('node'); $this->assertTrue($query, 'Query is not null'); $query_service = $container->get('entity.query'); $query = $query_service->getAggregate('node'); $this->assertTrue($query, 'Query is not null'); // Try an actual query. $query = $query_service->get('node'); $query->condition('type', $newnode->getType()); $ids = $query->execute(); // Try different methods of getting storage manager. $storage = \Drupal::entityManager()->getStorage('node'); $this->assertTrue($storage, 'Storage is not null'); // Load the entities and verify. $storage = $container->get('entity.manager')->getStorage('node'); $entities = $storage->loadMultiple($ids); $this->assertEqual(count($entities), 1, 'One node was found'); $first = reset($entities); $this->assertEqual($first->getTitle(), $newnode->getTitle(), 'Correct node was found'); }
/** * Tests the entityQueryAggregate() method. * * @covers ::entityQueryAggregate */ public function testEntityQueryAggregate() { $query = $this->getMockBuilder('Drupal\\Core\\Entity\\Query\\QueryFactory')->disableOriginalConstructor()->getMock(); $query->expects($this->once())->method('getAggregate')->with('test_entity', 'OR')->will($this->returnValue(TRUE)); $this->setMockContainerService('entity.query', $query); $this->assertNotNull(\Drupal::entityQueryAggregate('test_entity', 'OR')); }