Version 1
[yaffs-website] / web / core / modules / comment / src / Tests / Views / WizardTest.php
1 <?php
2
3 namespace Drupal\comment\Tests\Views;
4
5 use Drupal\comment\Tests\CommentTestTrait;
6 use Drupal\views\Views;
7 use Drupal\views\Tests\Wizard\WizardTestBase;
8
9 /**
10  * Tests the comment module integration into the wizard.
11  *
12  * @group comment
13  * @see \Drupal\comment\Plugin\views\wizard\Comment
14  */
15 class WizardTest extends WizardTestBase {
16
17   use CommentTestTrait;
18
19   /**
20    * Modules to install.
21    *
22    * @var array
23    */
24   public static $modules = ['node', 'comment'];
25
26
27   /**
28    * {@inheritdoc}
29    */
30   protected function setUp() {
31     parent::setUp();
32     $this->drupalCreateContentType(['type' => 'page', 'name' => t('Basic page')]);
33     // Add comment field to page node type.
34     $this->addDefaultCommentField('node', 'page');
35   }
36
37   /**
38    * Tests adding a view of comments.
39    */
40   public function testCommentWizard() {
41     $view = [];
42     $view['label'] = $this->randomMachineName(16);
43     $view['id'] = strtolower($this->randomMachineName(16));
44     $view['show[wizard_key]'] = 'comment';
45     $view['page[create]'] = TRUE;
46     $view['page[path]'] = $this->randomMachineName(16);
47
48     // Just triggering the saving should automatically choose a proper row
49     // plugin.
50     $this->drupalPostForm('admin/structure/views/add', $view, t('Save and edit'));
51     $this->assertUrl('admin/structure/views/view/' . $view['id'], [], 'Make sure the view saving was successful and the browser got redirected to the edit page.');
52
53     // If we update the type first we should get a selection of comment valid
54     // row plugins as the select field.
55
56     $this->drupalGet('admin/structure/views/add');
57     $this->drupalPostForm('admin/structure/views/add', $view, t('Update "of type" choice'));
58
59     // Check for available options of the row plugin.
60     $xpath = $this->constructFieldXpath('name', 'page[style][row_plugin]');
61     $fields = $this->xpath($xpath);
62     $options = [];
63     foreach ($fields as $field) {
64       $items = $this->getAllOptions($field);
65       foreach ($items as $item) {
66         $options[] = $item->attributes()->value;
67       }
68     }
69     $expected_options = ['entity:comment', 'fields'];
70     $this->assertEqual($options, $expected_options);
71
72     $view['id'] = strtolower($this->randomMachineName(16));
73     $this->drupalPostForm(NULL, $view, t('Save and edit'));
74     $this->assertUrl('admin/structure/views/view/' . $view['id'], [], 'Make sure the view saving was successful and the browser got redirected to the edit page.');
75
76     $user = $this->drupalCreateUser(['access comments']);
77     $this->drupalLogin($user);
78
79     $view = Views::getView($view['id']);
80     $view->initHandlers();
81     $row = $view->display_handler->getOption('row');
82     $this->assertEqual($row['type'], 'entity:comment');
83
84     // Check for the default filters.
85     $this->assertEqual($view->filter['status']->table, 'comment_field_data');
86     $this->assertEqual($view->filter['status']->field, 'status');
87     $this->assertTrue($view->filter['status']->value);
88     $this->assertEqual($view->filter['status_node']->table, 'node_field_data');
89     $this->assertEqual($view->filter['status_node']->field, 'status');
90     $this->assertTrue($view->filter['status_node']->value);
91
92     // Check for the default fields.
93     $this->assertEqual($view->field['subject']->table, 'comment_field_data');
94     $this->assertEqual($view->field['subject']->field, 'subject');
95   }
96
97 }