Chris@0: [1], Chris@0: 'taxonomy_term' => [1], Chris@0: 'glossary' => ['all'], Chris@0: ]; Chris@0: Chris@0: protected function setUp($import_test_views = TRUE) { Chris@0: parent::setUp($import_test_views); Chris@0: Chris@0: $this->drupalPlaceBlock('page_title_block'); Chris@0: Chris@0: // Create Basic page node type. Chris@0: $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']); Chris@0: Chris@0: $vocabulary = Vocabulary::create([ Chris@0: 'name' => $this->randomMachineName(), Chris@0: 'description' => $this->randomMachineName(), Chris@17: 'vid' => mb_strtolower($this->randomMachineName()), Chris@0: 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, Chris@0: 'help' => '', Chris@0: 'nodes' => ['page' => 'page'], Chris@0: 'weight' => mt_rand(0, 10), Chris@0: ]); Chris@0: $vocabulary->save(); Chris@0: Chris@0: // Create a field. Chris@17: $field_name = mb_strtolower($this->randomMachineName()); Chris@0: Chris@0: $handler_settings = [ Chris@0: 'target_bundles' => [ Chris@0: $vocabulary->id() => $vocabulary->id(), Chris@0: ], Chris@0: 'auto_create' => TRUE, Chris@0: ]; Chris@0: $this->createEntityReferenceField('node', 'page', $field_name, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); Chris@0: Chris@0: // Create a time in the past for the archive. Chris@0: $time = REQUEST_TIME - 3600; Chris@0: Chris@0: $this->addDefaultCommentField('node', 'page'); Chris@0: Chris@0: for ($i = 0; $i <= 10; $i++) { Chris@0: $user = $this->drupalCreateUser(); Chris@0: $term = $this->createTerm($vocabulary); Chris@0: Chris@0: $values = ['created' => $time, 'type' => 'page']; Chris@0: $values[$field_name][]['target_id'] = $term->id(); Chris@0: Chris@0: // Make every other node promoted. Chris@0: if ($i % 2) { Chris@0: $values['promote'] = TRUE; Chris@0: } Chris@0: $values['body'][]['value'] = \Drupal::l('Node ' . 1, new Url('entity.node.canonical', ['node' => 1])); Chris@0: Chris@0: $node = $this->drupalCreateNode($values); Chris@0: Chris@0: $comment = [ Chris@0: 'uid' => $user->id(), Chris@0: 'status' => CommentInterface::PUBLISHED, Chris@0: 'entity_id' => $node->id(), Chris@0: 'entity_type' => 'node', Chris@17: 'field_name' => 'comment', Chris@0: ]; Chris@0: Comment::create($comment)->save(); Chris@0: Chris@0: $unpublished_comment = [ Chris@0: 'uid' => $user->id(), Chris@0: 'status' => CommentInterface::NOT_PUBLISHED, Chris@0: 'entity_id' => $node->id(), Chris@0: 'entity_type' => 'node', Chris@0: 'field_name' => 'comment', Chris@0: ]; Chris@0: Comment::create($unpublished_comment)->save(); Chris@0: } Chris@0: Chris@0: // Some views, such as the "Who's Online" view, only return results if at Chris@0: // least one user is logged in. Chris@0: $account = $this->drupalCreateUser([]); Chris@0: $this->drupalLogin($account); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test that all Default views work as expected. Chris@0: */ Chris@0: public function testDefaultViews() { Chris@0: // Get all default views. Chris@0: $controller = $this->container->get('entity.manager')->getStorage('view'); Chris@0: $views = $controller->loadMultiple(); Chris@0: Chris@0: foreach ($views as $name => $view_storage) { Chris@0: $view = $view_storage->getExecutable(); Chris@0: $view->initDisplay(); Chris@0: foreach ($view->storage->get('display') as $display_id => $display) { Chris@0: $view->setDisplay($display_id); Chris@0: Chris@0: // Add any args if needed. Chris@0: if (array_key_exists($name, $this->viewArgMap)) { Chris@0: $view->preExecute($this->viewArgMap[$name]); Chris@0: } Chris@0: Chris@0: $this->assert(TRUE, format_string('View @view will be executed.', ['@view' => $view->storage->id()])); Chris@0: $view->execute(); Chris@0: Chris@0: $tokens = ['@name' => $name, '@display_id' => $display_id]; Chris@0: $this->assertTrue($view->executed, format_string('@name:@display_id has been executed.', $tokens)); Chris@0: Chris@0: $count = count($view->result); Chris@0: $this->assertTrue($count > 0, format_string('@count results returned', ['@count' => $count])); Chris@0: $view->destroy(); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a new term with random properties in vocabulary $vid. Chris@0: */ Chris@0: public function createTerm($vocabulary) { Chris@0: $filter_formats = filter_formats(); Chris@0: $format = array_pop($filter_formats); Chris@0: $term = Term::create([ Chris@0: 'name' => $this->randomMachineName(), Chris@0: 'description' => $this->randomMachineName(), Chris@0: // Use the first available text format. Chris@0: 'format' => $format->id(), Chris@0: 'vid' => $vocabulary->id(), Chris@0: 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, Chris@0: ]); Chris@0: $term->save(); Chris@0: return $term; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the archive view. Chris@0: */ Chris@0: public function testArchiveView() { Chris@0: // Create additional nodes compared to the one in the setup method. Chris@0: // Create two nodes in the same month, and one in each following month. Chris@0: $node = [ Chris@0: // Sun, 19 Nov 1978 05:00:00 GMT. Chris@0: 'created' => 280299600, Chris@0: ]; Chris@0: $this->drupalCreateNode($node); Chris@0: $this->drupalCreateNode($node); Chris@0: $node = [ Chris@0: // Tue, 19 Dec 1978 05:00:00 GMT. Chris@0: 'created' => 282891600, Chris@0: ]; Chris@0: $this->drupalCreateNode($node); Chris@0: $node = [ Chris@0: // Fri, 19 Jan 1979 05:00:00 GMT. Chris@0: 'created' => 285570000, Chris@0: ]; Chris@0: $this->drupalCreateNode($node); Chris@0: Chris@0: $view = Views::getView('archive'); Chris@0: $view->setDisplay('page_1'); Chris@0: $this->executeView($view); Chris@0: $columns = ['nid', 'created_year_month', 'num_records']; Chris@0: $column_map = array_combine($columns, $columns); Chris@0: // Create time of additional nodes created in the setup method. Chris@0: $created_year_month = date('Ym', REQUEST_TIME - 3600); Chris@0: $expected_result = [ Chris@0: [ Chris@0: 'nid' => 1, Chris@0: 'created_year_month' => $created_year_month, Chris@0: 'num_records' => 11, Chris@0: ], Chris@0: [ Chris@0: 'nid' => 15, Chris@0: 'created_year_month' => 197901, Chris@0: 'num_records' => 1, Chris@0: ], Chris@0: [ Chris@0: 'nid' => 14, Chris@0: 'created_year_month' => 197812, Chris@0: 'num_records' => 1, Chris@0: ], Chris@0: [ Chris@0: 'nid' => 12, Chris@0: 'created_year_month' => 197811, Chris@0: 'num_records' => 2, Chris@0: ], Chris@0: ]; Chris@0: $this->assertIdenticalResultset($view, $expected_result, $column_map); Chris@0: Chris@0: $view->storage->setStatus(TRUE); Chris@0: $view->save(); Chris@0: \Drupal::service('router.builder')->rebuild(); Chris@0: Chris@0: $this->drupalGet('archive'); Chris@0: $this->assertResponse(200); Chris@0: } Chris@0: Chris@0: }