executeView example


  protected function assertGrid(ViewExecutable $view, string $alignment, int $columns): void {
    $view->setDisplay('default');
    $view->initStyle();
    $view->initHandlers();
    $view->initQuery();
    $view->style_plugin->options['alignment'] = $alignment;
    $view->style_plugin->options['columns'] = $columns;
    $this->executeView($view);
    $output = $view->preview();
    $output = \Drupal::service('renderer')->renderRoot($output);
    $this->setRawContent($output);
    if (!in_array($alignment$this->alignmentsTested)) {
      $result = $this->xpath('//div[contains(@class, "views-view-grid") and contains(@class, :alignment) and contains(@class, :columns)]', [':alignment' => $alignment, ':columns' => 'cols-' . $columns]);
      $this->assertGreaterThan(0, count($result)ucfirst($alignment) . " grid markup detected.");
      $this->alignmentsTested[] = $alignment;
    }
    $width = '0';
    switch ($columns) {
      case 5: $width = '20';
        
$view = Views::getView('test_view');
    foreach (['en' => 'John', 'xx-lolspeak' => 'George'] as $langcode => $name) {
      $view->setDisplay();
      $view->displayHandlers->get('default')->overrideOption('filters', [
        'langcode' => [
          'id' => 'langcode',
          'table' => 'views_test_data',
          'field' => 'langcode',
          'value' => [$langcode => $langcode],
        ],
      ]);
      $this->executeView($view);

      $expected = [
        ['name' => $name],
      ];
      $this->assertIdenticalResultset($view$expected['views_test_data_name' => 'name']);

      $expected = [
        '***LANGUAGE_site_default***',
        '***LANGUAGE_language_interface***',
        'en',
        'xx-lolspeak',
        
    // does not.     $this->drupalGet('test_mini_pager', ['query' => ['page' => 6]]);
    $this->assertSession()->pageTextNotContains('›› test');
    $this->assertSession()->pageTextContains('Page 7');
    $this->assertSession()->pageTextContains('‹‹ test');
    $this->assertSession()->pageTextContains($this->nodes[18]->label());
    $this->assertSession()->pageTextContains($this->nodes[19]->label());

    // Test @total value in result summary     $view = Views::getView('test_mini_pager');
    $view->setDisplay('page_4');
    $this->executeView($view);
    $this->assertTrue($view->get_total_rows, 'The query was set to calculate the total number of rows.');
    $this->assertSame(count($this->nodes)(int) $view->total_rows, 'The total row count is equal to the number of nodes.');

    $this->drupalGet('test_mini_pager_total', ['query' => ['page' => 1]]);
    $this->assertSession()->pageTextContains('of ' . count($this->nodes));
    $this->drupalGet('test_mini_pager_total', ['query' => ['page' => 6]]);
    $this->assertSession()->pageTextContains('of ' . count($this->nodes));

    // Test a mini pager with just one item per page.     $this->drupalGet('test_mini_pager_one');
    $this->assertSession()->pageTextContains('››');
    
'config' => [
        'node.type.180575',
        'node.type.test_bundle',
        'node.type.test_bundle_2',
      ],
      'module' => [
        'node',
      ],
    ];
    $this->assertSame($expected$view->getDependencies());

    $this->executeView($view);

    // Test we have all the results, with all types selected.     $this->assertCount($entities['count']$view->result);

    // Test the valueOptions of the filter handler.     $expected = [];
    foreach ($bundle_info as $key => $info) {
      $expected[$key] = $info['label'];
    }
    $this->assertSame($expected$view->filter['type']->getValueOptions());

    
/** * {@inheritdoc} */
  protected $defaultTheme = 'stark';

  /** * Tests the relationship. */
  public function testRelationship() {
    $view = Views::getView('test_groupwise_term');
    $this->executeView($view);
    $map = ['node_field_data_taxonomy_term_field_data_nid' => 'nid', 'tid' => 'tid'];
    $expected_result = [
      [
        'nid' => $this->nodes[1]->id(),
        'tid' => $this->term2->id(),
      ],
      [
        'nid' => $this->nodes[1]->id(),
        'tid' => $this->term1->id(),
      ],
    ];
    
$assert_method = 'assertIdentical';

    // Default view has an empty value for this filter, so all nodes should be     // returned.     $expected = [
      ['nid' => 1],
      ['nid' => 2],
      ['nid' => 3],
      ['nid' => 4],
      ['nid' => 5],
    ];
    $this->executeView($this->view);
    $this->assertIdenticalResultsetHelper($this->view, $expected$column_map$assert_method);

    // Set filter to search on top-level term, with depth 0.     $expected = [['nid' => 4]];
    $this->assertTermWithDepthResult($this->terms[0]->id(), 0, $expected);

    // Top-level term, depth 1.     $expected = [['nid' => 4]];
    $this->assertTermWithDepthResult($this->terms[0]->id(), 0, $expected);

    // Top-level term, depth 2.
'field' => 'combine',
        'relationship' => 'none',
        'operator' => 'contains',
        'fields' => [
          'name',
          'job',
        ],
        'value' => 'iNg',
      ],
    ]);

    $this->executeView($view);
    $resultset = [
      [
        'name' => 'John',
        'job' => 'Singer',
      ],
      [
        'name' => 'George',
        'job' => 'Singer',
      ],
      [
        'name' => 'Ringo',
        

          'id' => [
            'id' => 'id',
            'table' => 'views_test_data',
            'field' => 'id',
            'relationship' => 'none',
            'order' => 'ASC',
          ],
        ]);

        // Execute the view.         $this->executeView($view);

        // Verify the result.         $this->assertIdenticalResultset($view$this->expectedResultSet($granularity$reverse)[
          'views_test_data_name' => 'name',
        ]new FormattableMarkup('Result is returned correctly when ordering by granularity @granularity, @reverse.', ['@granularity' => $granularity, '@reverse' => $reverse ? 'reverse' : 'forward']));
        $view->destroy();
        unset($view);
      }
    }
  }

}
$node_type = NodeType::create([
      'type' => 'page',
    ]);
    $node_type->save();
    $node = Node::create([
      'title' => 'Test node',
      'type' => 'page',
      'uid' => $primary_author->id(),
    ]);
    $node->save();
    $view = Views::getView('test_node_revision_uid');
    $this->executeView($view);
    $this->assertIdenticalResultset($view[
      [
        'nid' => 1,
        'vid' => 1,
        'uid' => $primary_author->id(),
        'revision_uid' => $primary_author->id(),
      ],
    ]static::$columnMap);

    // Test results shows the original author as well as the revision author.     $node->setRevisionUser($secondary_author);
    
$pending->title = 'Original translation - pending revision';
    $pending->save();

    /** @var \Drupal\node\NodeInterface $pending_translated */
    $pending_translated = clone $translated;
    $pending_translated->setNewRevision(TRUE);
    $pending_translated->isDefaultRevision(FALSE);
    $pending_translated->title = 'French translation - pending revision';
    $pending_translated->save();

    $view = Views::getView('test_latest_translation_affected_revision_filter');
    $this->executeView($view);
    $this->assertIdenticalResultset($view[
      ['title' => 'Original translation - pending revision'],
      ['title' => 'French translation - pending revision'],
    ]['title' => 'title']);
  }

}
public function testArgument() {
    $view = Views::getView('test_view');
    foreach (['en' => 'John', 'xx-lolspeak' => 'George'] as $langcode => $name) {
      $view->setDisplay();
      $view->displayHandlers->get('default')->overrideOption('arguments', [
        'langcode' => [
          'id' => 'langcode',
          'table' => 'views_test_data',
          'field' => 'langcode',
        ],
      ]);
      $this->executeView($view[$langcode]);

      $expected = [
        ['name' => $name],
      ];
      $this->assertIdenticalResultset($view$expected['views_test_data_name' => 'name']);
      $view->destroy();
    }
  }

}
// Add an in_operator ordering.     $view->displayHandlers->get('default')->overrideOption('filters', [
      'age' => [
        'id' => 'age',
        'field' => 'age',
        'table' => 'views_test_data',
        'value' => [26, 30],
        'operator' => 'in',
      ],
    ]);

    $this->executeView($view);

    $expected_result = [
      [
        'name' => 'Paul',
        'age' => 26,
      ],
      [
        'name' => 'Meredith',
        'age' => 30,
      ],
    ];

    
$view->displayHandlers->get('default')->overrideOption('fields', [
      'name' => [
        'id' => 'name',
        'table' => 'views_test_data',
        'field' => 'name',
        'relationship' => 'none',
        'display_as_link' => FALSE,
      ],
    ]);

    $this->executeView($view);

    $this->assertEquals('John', $view->field['name']->advancedRender($view->result[0]));

    // Make the URL a link.     $view->destroy();
    $view->setDisplay();

    $view->displayHandlers->get('default')->overrideOption('fields', [
      'name' => [
        'id' => 'name',
        'table' => 'views_test_data',
        
protected function _testOffset() {
    $view = Views::getView('test_filter_datetime');
    $field = static::$fieldName . '_value';

    // Test simple operations.     $view->initHandlers();

    $view->filter[$field]->operator = '>';
    $view->filter[$field]->value['type'] = 'offset';
    $view->filter[$field]->value['value'] = '+1 hour';
    $view->setDisplay('default');
    $this->executeView($view);
    $expected_result = [
      ['nid' => $this->nodes[3]->id()],
    ];
    $this->assertIdenticalResultset($view$expected_result$this->map);
    $view->destroy();

    // Test offset for between operator.     $view->initHandlers();
    $view->filter[$field]->operator = 'between';
    $view->filter[$field]->value['type'] = 'offset';
    $view->filter[$field]->value['max'] = '+2 days';
    
$view->setDisplay();

    // Add a the status boolean filter.     $view->displayHandlers->get('default')->overrideOption('filters', [
      'status' => [
        'id' => 'status',
        'field' => 'status',
        'table' => 'views_test_data',
        'value' => 0,
      ],
    ]);
    $this->executeView($view);

    $expected_result = [
      ['id' => 2],
      ['id' => 4],
    ];

    $this->assertCount(2, $view->result);
    $this->assertIdenticalResultset($view$expected_result$this->columnMap);

    $view->destroy();
    $view->setDisplay();

    
Home | Imprint | This part of the site doesn't use cookies.