View source
<?php
namespace Drupal\views_xml_backend\Tests;
use Drupal\views\Views;
use Drupal\views_ui\Tests\UITestBase;
use Drupal\Component\Serialization\Json;
abstract class ViewsXMLBackendBase extends UITestBase {
protected $strictConfigSchema = FALSE;
public static $modules = [
'views',
'views_ui',
'views_xml_backend',
];
protected $viewsXMLBackendUser;
protected $viewsXMLBackendViewFieldId;
protected $viewsXMLBackendViewFieldName;
protected $viewsXMLBackendViewValue;
protected $viewsXMLBackendTitle;
protected $viewsXMLBackendViewId;
protected $viewsXMLBackendViewAddPath;
protected $viewsXMLBackendViewEditPath;
protected $viewsXMLBackendViewQueryPath;
protected $viewsXMLBackendFile;
protected function setUp() {
parent::setUp();
$permissions = [
'administer users',
'administer permissions',
'administer views',
'access user profiles',
'administer permissions',
'administer blocks',
'bypass node access',
'view all revisions',
];
$this->viewsXMLBackendUser = $this
->createTestUser($permissions);
$this
->drupalLogin($this->viewsXMLBackendUser);
}
private function createTestUser(array $permissions = []) {
return $this
->drupalCreateUser($permissions);
}
private function setUpViewsXMLBackendVariables() {
return $settings = [
'field_id' => 'edit-show-wizard-key',
'field_name' => 'show[wizard_key]',
'value' => 'standard:views_xml_backend',
'file' => 'https://updates.drupal.org/release-history/views/7.x',
];
}
private function setUpViewsVariables() {
$settings = $this
->setUpViewsXMLBackendVariables();
$this->viewsXMLBackendViewFieldId = $settings['field_id'];
$this->viewsXMLBackendViewFieldName = $settings['field_name'];
$this->viewsXMLBackendViewValue = $settings['value'];
$this->viewsXMLBackendFile = $settings['file'];
$this->viewsXMLBackendViewId = strtolower($this
->randomMachineName(16));
$this->viewsXMLBackendTitle = $this
->randomMachineName(16);
$this->viewsXMLBackendViewAddPath = '/admin/structure/views/add';
$this->viewsXMLBackendViewEditPath = "/admin/structure/views/view/{$this->viewsXMLBackendViewId}/edit/default";
$this->viewsXMLBackendViewQueryPath = "admin/structure/views/nojs/display/{$this->viewsXMLBackendViewId}/default/query";
}
protected function addXMLBackendView() {
$this
->drupalGet('admin/structure/views/add');
$settings = $this
->setUpViewsXMLBackendVariables();
$msg = "Select option '{$settings['value']}' was found in '{$settings['field_id']}'";
$this
->assertOption($settings['field_id'], $settings['value'], $msg);
}
protected function addMinimalXMLBackendView() {
$this
->setUpViewsVariables();
$default = [
$this->viewsXMLBackendViewFieldName => $this->viewsXMLBackendViewValue,
];
$this
->drupalPostAjaxForm($this->viewsXMLBackendViewAddPath, $default, $this->viewsXMLBackendViewFieldName);
$new_id = $this
->xpath("//*[starts-with(@id, 'edit-show-wizard-key')]/@id");
$new_wizard_id = (string) $new_id[0]['id'];
$this
->assertOptionSelected($new_wizard_id, $this->viewsXMLBackendViewValue, "The XML select option 'standard:views_xml_backend' was selected on {$new_wizard_id}");
$default = [
'label' => $this->viewsXMLBackendTitle,
'id' => $this->viewsXMLBackendViewId,
'description' => $this
->randomMachineName(16),
$this->viewsXMLBackendViewFieldName => $this->viewsXMLBackendViewValue,
];
$this
->drupalPostForm($this->viewsXMLBackendViewAddPath, $default, t('Save and edit'));
$this
->assertText("The view {$this->viewsXMLBackendTitle} has been saved");
}
protected function addStandardXMLBackendView() {
$this
->addMinimalXMLBackendView();
$this
->drupalGet($this->viewsXMLBackendViewQueryPath);
$this
->assertField('query[options][xml_file]', "The XML select option 'query[options][xml_file]' was found");
$this
->assertField('query[options][row_xpath]', "The XML select option 'query[options][row_xpath]' was found");
$xml_setting = [
'query[options][xml_file]' => $this->viewsXMLBackendFile,
'query[options][row_xpath]' => "/project/releases/release",
];
$this
->drupalPostForm($this->viewsXMLBackendViewQueryPath, $xml_setting, t('Apply'));
$this
->drupalPostForm($this->viewsXMLBackendViewEditPath, array(), t('Save'));
$view = Views::getView($this->viewsXMLBackendViewId);
$view
->initDisplay();
$view
->initQuery();
$this
->assertEqual($this->viewsXMLBackendFile, $view->query->options['xml_file'], 'Query settings were saved');
$this
->drupalGet("admin/structure/views/view/{$this->viewsXMLBackendViewId}/edit");
$this
->assertResponse(200);
$this
->drupalPostForm(NULL, $edit = array(), t('Update preview'));
$edit_handler_url = "admin/structure/views/nojs/handler/{$this->viewsXMLBackendViewId}/default/field/text";
$this
->drupalGet($edit_handler_url);
$fields = [
'options[xpath_selector]' => 'version_major',
];
$this
->drupalPostForm(NULL, $fields, t('Apply'));
$this
->drupalPostForm(NULL, $edit = array(), t('Update preview'));
$edit_handler_url = "admin/structure/views/nojs/handler/{$this->viewsXMLBackendViewId}/default/field/text";
$this
->drupalGet($edit_handler_url);
$field_id = $this
->xpath("//*[starts-with(@id, 'edit-options-xpath-selector')]/@id");
$new_field_id = (string) $field_id[0]['id'];
$this
->assertFieldByXPath("//input[@id='{$new_field_id}']", 'version_major', "Value 'version_major' found in field {$new_field_id}");
$field_add = "/admin/structure/views/nojs/add-handler/{$this->viewsXMLBackendViewId}/default/field";
$this
->drupalGet($field_add);
$this
->assertField('name[views_xml_backend.text]', "The XML check field 'name[views_xml_backend.text]' was found");
$fields = [
'name[views_xml_backend.text]' => 'views_xml_backend.text',
];
$this
->drupalPostForm(NULL, $fields, t('Add and configure fields'));
$this
->assertField('options[xpath_selector]', "The XML input 'options[xpath_selector]' was found");
$fields = [
'options[xpath_selector]' => 'download_link',
];
$this
->drupalPostForm(NULL, $fields, t('Apply'));
$this
->drupalPostForm(NULL, $edit = array(), t('Update preview'));
$edit_handler_url = "admin/structure/views/nojs/handler/{$this->viewsXMLBackendViewId}/default/field/text_1";
$this
->drupalGet($edit_handler_url);
$field_id = $this
->xpath("//*[starts-with(@id, 'edit-options-xpath-selector')]/@id");
$new_field_id = (string) $field_id[0]['id'];
$this
->assertFieldByXPath("//input[@id='{$new_field_id}']", 'download_link', "Value 'download_link' found in field {$new_field_id}");
}
protected function navigateViewsPager($pager_path) {
$content = $this->content;
$drupal_settings = $this->drupalSettings;
$ajax_settings = array(
'wrapper' => 'views-preview-wrapper',
'method' => 'replaceWith',
);
$url = $this
->getAbsoluteUrl($pager_path);
$post = array(
'js' => 'true',
) + $this
->getAjaxPageStatePostData();
$result = Json::decode($this
->drupalPost($url, 'application/vnd.drupal-ajax', $post));
if (!empty($result)) {
$this
->drupalProcessAjaxResponse($content, $result, $ajax_settings, $drupal_settings);
}
}
}