View source  
  <?php
namespace Drupal\Tests\views\Functional\Wizard;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Url;
use Drupal\views\Views;
class BasicTest extends WizardTestBase {
  
  protected $defaultTheme = 'stark';
  protected function setUp($import_test_views = TRUE) {
    parent::setUp($import_test_views);
    $this
      ->drupalPlaceBlock('page_title_block');
  }
  public function testViewsWizardAndListing() {
    $this
      ->drupalCreateContentType([
      'type' => 'article',
    ]);
    $this
      ->drupalCreateContentType([
      'type' => 'page',
    ]);
    
    $this
      ->drupalGet('admin/structure/views');
    $this
      ->assertText(t('Add view'));
    
    $view1 = [];
    $view1['label'] = $this
      ->randomMachineName(16);
    $view1['id'] = strtolower($this
      ->randomMachineName(16));
    $view1['description'] = $this
      ->randomMachineName(16);
    $view1['page[create]'] = FALSE;
    $this
      ->drupalPostForm('admin/structure/views/add', $view1, t('Save and edit'));
    $this
      ->assertSession()
      ->statusCodeEquals(200);
    $this
      ->drupalGet('admin/structure/views');
    $this
      ->assertText($view1['label']);
    $this
      ->assertText($view1['description']);
    $this
      ->assertLinkByHref(Url::fromRoute('entity.view.edit_form', [
      'view' => $view1['id'],
    ])
      ->toString());
    $this
      ->assertLinkByHref(Url::fromRoute('entity.view.delete_form', [
      'view' => $view1['id'],
    ])
      ->toString());
    $this
      ->assertLinkByHref(Url::fromRoute('entity.view.duplicate_form', [
      'view' => $view1['id'],
    ])
      ->toString());
    
    $this
      ->assertNoText('REST export', 'When no options are enabled in the wizard, the resulting view does not have a REST export display.');
    
    $this
      ->drupalGet('admin/structure/block');
    $this
      ->assertNoText($view1['label']);
    
    $node1 = $this
      ->drupalCreateNode([
      'type' => 'page',
    ]);
    $node2 = $this
      ->drupalCreateNode([
      'type' => 'article',
    ]);
    
    $view2 = [];
    $view2['label'] = $this
      ->randomMachineName(16);
    $view2['id'] = strtolower($this
      ->randomMachineName(16));
    $view2['description'] = $this
      ->randomMachineName(16);
    $view2['page[create]'] = 1;
    $view2['page[title]'] = $this
      ->randomMachineName(16);
    $view2['page[path]'] = $this
      ->randomMachineName(16);
    $view2['page[feed]'] = 1;
    $view2['page[feed_properties][path]'] = $this
      ->randomMachineName(16);
    $this
      ->drupalPostForm('admin/structure/views/add', $view2, t('Save and edit'));
    $this
      ->drupalGet($view2['page[path]']);
    $this
      ->assertSession()
      ->statusCodeEquals(200);
    
    $this
      ->assertUrl($view2['page[path]']);
    $this
      ->assertText($view2['page[title]']);
    $this
      ->assertText($node1
      ->label());
    $this
      ->assertText($node2
      ->label());
    
    $this
      ->assertLinkByHref(Url::fromRoute('view.' . $view2['id'] . '.feed_1')
      ->toString());
    $elements = $this
      ->cssSelect('link[href="' . Url::fromRoute('view.' . $view2['id'] . '.feed_1', [], [
      'absolute' => TRUE,
    ])
      ->toString() . '"]');
    $this
      ->assertCount(1, $elements, 'Feed found.');
    $this
      ->drupalGet($view2['page[feed_properties][path]']);
    
    $this
      ->assertEquals('2.0', $this
      ->getSession()
      ->getDriver()
      ->getAttribute('//rss', 'version'));
    
    $this
      ->assertText($view2['page[title]']);
    $this
      ->assertRaw($node1
      ->toUrl('canonical', [
      'absolute' => TRUE,
    ])
      ->toString());
    $this
      ->assertText($node1
      ->label());
    $this
      ->assertRaw($node2
      ->toUrl('canonical', [
      'absolute' => TRUE,
    ])
      ->toString());
    $this
      ->assertText($node2
      ->label());
    
    $this
      ->drupalGet('admin/structure/views');
    $this
      ->assertText($view2['label']);
    $this
      ->assertText($view2['description']);
    $this
      ->assertLinkByHref(Url::fromRoute('view.' . $view2['id'] . '.page_1')
      ->toString());
    
    $this
      ->assertNoText('REST export', 'If only the page option was enabled in the wizard, the resulting view does not have a REST export display.');
    
    $this
      ->drupalGet('admin/structure/block');
    $this
      ->assertNoText('View: ' . $view2['label']);
    
    $view3 = [];
    $view3['label'] = $this
      ->randomMachineName(16);
    $view3['id'] = strtolower($this
      ->randomMachineName(16));
    $view3['description'] = $this
      ->randomMachineName(16);
    $view3['show[wizard_key]'] = 'node';
    $view3['show[type]'] = 'page';
    $view3['page[create]'] = 1;
    $view3['page[title]'] = $this
      ->randomMachineName(16);
    $view3['page[path]'] = $this
      ->randomMachineName(16);
    $view3['block[create]'] = 1;
    $view3['block[title]'] = $this
      ->randomMachineName(16);
    $this
      ->drupalPostForm('admin/structure/views/add', $view3, t('Save and edit'));
    $this
      ->drupalGet($view3['page[path]']);
    $this
      ->assertSession()
      ->statusCodeEquals(200);
    
    $this
      ->assertUrl($view3['page[path]']);
    $this
      ->assertText($view3['page[title]']);
    $this
      ->assertText($node1
      ->label());
    $this
      ->assertNoText($node2
      ->label());
    
    $this
      ->drupalGet('admin/structure/views');
    $this
      ->assertText($view3['label']);
    $this
      ->assertText($view3['description']);
    $this
      ->assertLinkByHref(Url::fromRoute('view.' . $view3['id'] . '.page_1')
      ->toString());
    
    $this
      ->assertNoText('REST export', 'If only the page and block options were enabled in the wizard, the resulting view does not have a REST export display.');
    
    $this
      ->drupalGet('admin/structure/block/list/' . $this
      ->config('system.theme')
      ->get('default'));
    $this
      ->clickLink('Place block');
    $this
      ->assertText($view3['label']);
    
    $this
      ->drupalPlaceBlock("views_block:{$view3['id']}-block_1");
    
    $this
      ->drupalGet('user');
    $this
      ->assertText($node1
      ->label());
    $this
      ->assertNoText($node2
      ->label());
    
    $this
      ->assertNoText('tracker', 'Default tracker view does not show on the listing page.');
    
    $view4 = [];
    $view4['label'] = $this
      ->randomMachineName(16);
    $view4['id'] = strtolower($this
      ->randomMachineName(16));
    $view4['description'] = $this
      ->randomMachineName(16);
    $view4['show[wizard_key]'] = 'node';
    $view4['show[type]'] = 'page';
    $view4['rest_export[create]'] = 1;
    $view4['rest_export[path]'] = $this
      ->randomMachineName(16);
    $this
      ->drupalPostForm('admin/structure/views/add', $view4, t('Save and edit'));
    $this
      ->assertRaw(t('The view %view has been saved.', [
      '%view' => $view4['label'],
    ]));
    
    $this
      ->drupalGet($view4['rest_export[path]'], [
      'query' => [
        '_format' => 'json',
      ],
    ]);
    $this
      ->assertSession()
      ->statusCodeEquals(200);
    $data = Json::decode($this
      ->getSession()
      ->getPage()
      ->getContent());
    $this
      ->assertCount(1, $data, 'Only the node of type page is exported.');
    $node = reset($data);
    $this
      ->assertEqual($node['nid'][0]['value'], $node1
      ->id(), 'The node of type page is exported.');
    
    $leading_slash_view = [];
    $leading_slash_view['label'] = $this
      ->randomMachineName(16);
    $leading_slash_view['id'] = strtolower($this
      ->randomMachineName(16));
    $leading_slash_view['description'] = $this
      ->randomMachineName(16);
    $leading_slash_view['show[wizard_key]'] = 'node';
    $leading_slash_view['show[type]'] = 'page';
    $leading_slash_view['page[create]'] = 1;
    $leading_slash_view['page[title]'] = $this
      ->randomMachineName(16);
    $leading_slash_view['page[path]'] = '/' . $this
      ->randomMachineName(16);
    $this
      ->drupalPostForm('admin/structure/views/add', $leading_slash_view, t('Save and edit'));
    $this
      ->assertEquals($leading_slash_view['page[path]'], $this
      ->cssSelect('#views-page-1-path')[0]
      ->getText());
  }
  
  public function testWizardDefaultValues() {
    $random_id = strtolower($this
      ->randomMachineName(16));
    
    $view = [];
    $view['label'] = $this
      ->randomMachineName(16);
    $view['id'] = $random_id;
    $view['description'] = $this
      ->randomMachineName(16);
    $view['page[create]'] = FALSE;
    $this
      ->drupalPostForm('admin/structure/views/add', $view, t('Save and edit'));
    
    $view = Views::getView($random_id);
    $displays = $view->storage
      ->get('display');
    foreach ($displays as $display) {
      foreach ([
        'query',
        'exposed_form',
        'pager',
        'style',
        'row',
      ] as $type) {
        $this
          ->assertFalse(empty($display['display_options'][$type]['options']), new FormattableMarkup('Default options found for @plugin.', [
          '@plugin' => $type,
        ]));
      }
    }
  }
}