Skip to content
Snippets Groups Projects
Commit 520bb734 authored by Ryan Bateman's avatar Ryan Bateman
Browse files

code sniffer compliance

parent 6cfa1a4f
No related branches found
No related tags found
No related merge requests found
backstop_generator.backstop_configuration_form:
title: 'Configure Backstop Generator'
route_name: backstop_generator.backstop_configuration_form
description: 'Welcome message admin'
description: 'Manage and copy a backstop.json visual regression testing configuration'
parent: system.admin_config_development
weight: 99
<?php
/**
* @file
* Contains Drupal\backstop_generator\BackstopConfigRetrieverService.
*/
namespace Drupal\backstop_generator;
/**
* A service definiton for retreiving backstop config.
*/
class BackstopConfigRetrieverService {
protected $configuration_object;
protected $configurationObject;
/**
* Create config.
*/
public function __construct() {
$config = \Drupal::config('backstop_generator.backstopconfiguration');
$this->configuration_object = array();
$this->configuration_object['id'] = strtolower(str_replace(' ', '_', \Drupal::config('system.site')->get('name')));
$this->configuration_object['viewports'] = array();
$this->configurationObject = [];
$this->configurationObject['id'] = strtolower(str_replace(' ', '_', \Drupal::config('system.site')->get('name')));
$this->configurationObject['viewports'] = [];
if ($config->get('viewports.Mobile')) {
$viewport = array(
$viewport = [
'label' => 'Mobile',
'width' => 320,
'height' => 480
);
array_push($this->configuration_object['viewports'], $viewport);
'height' => 480,
];
array_push($this->configurationObject['viewports'], $viewport);
}
if ($config->get('viewports.Tablet')) {
$viewport = array(
$viewport = [
'label' => 'Tablet',
'width' => 1024,
'height' => 768
);
array_push($this->configuration_object['viewports'], $viewport);
'height' => 768,
];
array_push($this->configurationObject['viewports'], $viewport);
}
if ($config->get('viewports.HD')) {
$viewport = array(
$viewport = [
'label' => 'HD',
'width' => 1920,
'height' => 1080
);
array_push($this->configuration_object['viewports'], $viewport);
'height' => 1080,
];
array_push($this->configurationObject['viewports'], $viewport);
}
if ($config->get('viewports.HD+')) {
$viewport = array(
$viewport = [
'label' => 'HD+',
'width' => 2560,
'height' => 1440
);
array_push($this->configuration_object['viewports'], $viewport);
'height' => 1440,
];
array_push($this->configurationObject['viewports'], $viewport);
}
if ($config->get('viewports.UHD')) {
$viewport = array(
$viewport = [
'label' => 'UHD',
'width' => 3840,
'height' => 2160
);
array_push($this->configuration_object['viewports'], $viewport);
'height' => 2160,
];
array_push($this->configurationObject['viewports'], $viewport);
}
$this->configuration_object['onBeforeScript'] = "chromy/onBefore.js";
$this->configuration_object['onReadyScript'] = "chromy/onReady.js";
$this->configuration_object['scenarios'] = array();
$this->configuration_object['paths'] = array(
$this->configurationObject['onBeforeScript'] = "chromy/onBefore.js";
$this->configurationObject['onReadyScript'] = "chromy/onReady.js";
$this->configurationObject['scenarios'] = [];
$this->configurationObject['paths'] = [
'bitmaps_reference' => 'backstop_data/bitmaps_reference',
"bitmaps_test" => "backstop_data/bitmaps_test",
"engine_scripts" => "backstop_data/engine_scripts",
"html_report" => "backstop_data/html_report",
"ci_report" => "backstop_data/ci_report"
);
$this->configuration_object['report'] = array('browser');
$this->configuration_object['engine'] = "chrome";
$this->configuration_object['engineFlags'] = array();
$this->configuration_object['asyncCaptureLimit'] = 5;
$this->configuration_object['asyncCompareLimit'] = 50;
$this->configuration_object['debug'] = false;
$this->configuration_object['debugWindow'] = false;
"ci_report" => "backstop_data/ci_report",
];
$this->configurationObject['report'] = ['browser'];
$this->configurationObject['engine'] = "chrome";
$this->configurationObject['engineFlags'] = [];
$this->configurationObject['asyncCaptureLimit'] = 5;
$this->configurationObject['asyncCompareLimit'] = 50;
$this->configurationObject['debug'] = FALSE;
$this->configurationObject['debugWindow'] = FALSE;
$pages_config_array = $config->get('pre_defined_pages_table');
$randomExclude = array();
$randomExclude = [];
foreach ($pages_config_array as $key => $value) {
if ($value['page'] != null) {
if ($value['page'] != NULL) {
$node = \Drupal::entityTypeManager()->getStorage('node')->load($value['page']);
$url = $node->toUrl('canonical', ['absolute' => true])->toString();
$scenario = array(
$url = $node->toUrl('canonical', ['absolute' => TRUE])->toString();
$scenario = [
'label' => $node->getTitle(),
'cookiePath' => "backstop_data/engine_scripts/cookies.json",
'url' => $url,
......@@ -95,46 +96,54 @@ class BackstopConfigRetrieverService {
"clickSelector" => "",
"postInteractionWait" => "",
"selectors" => [],
"selectorExpansion" => true,
"selectorExpansion" => TRUE,
"misMatchThreshold" => 0.1,
"requireSameDimensions" => true
);
array_push($this->configuration_object['scenarios'], $scenario);
"requireSameDimensions" => TRUE,
];
array_push($this->configurationObject['scenarios'], $scenario);
array_push($randomExclude, $value['page']);
}
}
$randomCount = $config->get('additional_random_pages');
$nids = \Drupal::entityQuery('node')
->condition('nid', $randomExclude, 'NOT IN')
->addTag('random')
->range(0, $randomCount)
->execute();
foreach ($nids as $nid) {
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
$url = $node->toUrl('canonical', ['absolute' => true])->toString();
$scenario = array(
'label' => $node->getTitle(),
'cookiePath' => "backstop_data/engine_scripts/cookies.json",
'url' => $url,
"referenceUrl" => "",
"readyEvent" => "",
"readySelector" => "",
"delay" => 0,
"hideSelectors" => [],
"removeSelectors" => [],
"hoverSelector" => "",
"clickSelector" => "",
"postInteractionWait" => "",
"selectors" => [],
"selectorExpansion" => true,
"misMatchThreshold" => 0.1,
"requireSameDimensions" => true
);
array_push($this->configuration_object['scenarios'], $scenario);
if (!empty($randomExclude)) {
$nids = \Drupal::entityQuery('node')
->condition('nid', $randomExclude, 'NOT IN')
->addTag('random')
->range(0, $randomCount)
->execute();
}
if ($nids) {
foreach ($nids as $nid) {
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
$url = $node->toUrl('canonical', ['absolute' => TRUE])->toString();
$scenario = [
'label' => $node->getTitle(),
'cookiePath' => "backstop_data/engine_scripts/cookies.json",
'url' => $url,
"referenceUrl" => "",
"readyEvent" => "",
"readySelector" => "",
"delay" => 0,
"hideSelectors" => [],
"removeSelectors" => [],
"hoverSelector" => "",
"clickSelector" => "",
"postInteractionWait" => "",
"selectors" => [],
"selectorExpansion" => TRUE,
"misMatchThreshold" => 0.1,
"requireSameDimensions" => TRUE,
];
array_push($this->configurationObject['scenarios'], $scenario);
}
}
}
/**
* Expose method for calling constructed config.
*/
public function getConfigurationObject() {
return $this->configuration_object;
return $this->configurationObject;
}
}
......@@ -5,11 +5,10 @@ namespace Drupal\backstop_generator\Controller;
use Drupal\Core\Controller\ControllerBase;
/**
* A controller for displaying a twig template with an embedded
* pretty-printed version of our generated backstop.json file.
* Controller for displaying twig template with backstop.json file.
*/
class BackstopConfigurationView extends ControllerBase {
/**
* Returns a renderable array for the page.
*/
......@@ -18,14 +17,15 @@ class BackstopConfigurationView extends ControllerBase {
'#theme' => 'backstop_configuration_view',
'#markup' => t('Your configuration:'),
'#attached' =>
array(
[
'library' =>
array('backstop_generator/prettyprint')
),
'#cache' => array(
['backstop_generator/prettyprint'],
],
'#cache' => [
'max_age' => 0,
),
],
];
return $build;
}
}
......@@ -14,15 +14,15 @@ class BackstopConfigurationForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames()
{
return [
'backstop_generator.backstopconfiguration'
];
protected function getEditableConfigNames() {
return [
'backstop_generator.backstopconfiguration',
];
}
/**
* {@inheritdoc} */
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'backstop_configuration_form';
}
......@@ -31,40 +31,46 @@ class BackstopConfigurationForm extends ConfigFormBase {
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('backstop_generator.backstopconfiguration');
if (empty($form_state->get('pre_defined_pages_table'))) {
$form_state->set('pre_defined_pages_table', $config->get('pre_defined_pages_table'));
$entity_count = count($form_state->get('pre_defined_pages_table'));
if ($entity_count > 0) {
$form_state->set('num_pages', $entity_count);
$value = $form_state->get('num_pages');
}
else {
$value = 1;
}
$config = $this->config('backstop_generator.backstopconfiguration');
if (empty($form_state->get('pre_defined_pages_table'))) {
$form_state->set('pre_defined_pages_table', $config->get('pre_defined_pages_table'));
$entity_count = count($form_state->get('pre_defined_pages_table'));
if ($entity_count > 0) {
$form_state->set('num_pages', $entity_count);
$value = $form_state->get('num_pages');
}
$form['viewports'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Viewport(s)'),
'#description' => $this->t('Select any viewports you&#039;d like to be included for each scenario.'),
'#options' => ['Mobile' => $this->t('Mobile'), 'Tablet' => $this->t('Tablet'), 'HD' => $this->t('HD'), 'HD+' => $this->t('HD+'), 'UHD' => $this->t('UHD')],
'#weight' => '0',
'#ajax' => [
'callback' => '::changeViewportsCallback',
'wrapper' => 'viewports-wrapper',
],
'#default_value' => $config->get('viewports'),
'#prefix' => '<div id="viewports-wrapper">',
'#suffix' => '</div>',
];
$form['#tree'] = TRUE;
$form['pre_defined_pages_table'] = [
'#type' => 'table',
'#title' => $this->t('Pre-defined Pages'),
'#header' => array('Pages', 'Actions'),
'#prefix' => '<div id="pre-defined-pages-table-wrapper">',
'#suffix' => '</div>',
];
else {
$value = 1;
}
}
$form['viewports'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Viewport(s)'),
'#description' => $this->t('Select any viewports you&#039;d like to be included for each scenario.'),
'#options' => [
'Mobile' => $this->t('Mobile'),
'Tablet' => $this->t('Tablet'),
'HD' => $this->t('HD'),
'HD+' => $this->t('HD+'),
'UHD' => $this->t('UHD'),
],
'#weight' => '0',
'#ajax' => [
'callback' => '::changeViewportsCallback',
'wrapper' => 'viewports-wrapper',
],
'#default_value' => $config->get('viewports'),
'#prefix' => '<div id="viewports-wrapper">',
'#suffix' => '</div>',
];
$form['#tree'] = TRUE;
$form['pre_defined_pages_table'] = [
'#type' => 'table',
'#title' => $this->t('Pre-defined Pages'),
'#header' => ['Pages', 'Actions'],
'#prefix' => '<div id="pre-defined-pages-table-wrapper">',
'#suffix' => '</div>',
];
foreach ($config->get('pre_defined_pages_table') as $key => $value) {
$configString = 'pre_defined_pages_table.' . $key . '.page';
if (\Drupal::entityTypeManager()->getStorage('node')->load($config->get($configString))) {
......@@ -78,20 +84,20 @@ class BackstopConfigurationForm extends ConfigFormBase {
'#type' => 'entity_autocomplete',
'#target_type' => 'node',
'#weight' => 0,
'#default_value' => $node
'#default_value' => $node,
];
$form['pre_defined_pages_table'][$key]['actions'] = [
'#type' => 'actions',
'#type' => 'actions',
];
$form['pre_defined_pages_table'][$key]['actions']['remove_page'] = [
'#type' => 'submit',
'#value' => t('Remove one'),
'#id' => $key,
'#submit' => array('::removeCallback'),
'#submit' => ['::removeCallback'],
'#ajax' => [
'callback' => '::addmoreCallback',
'wrapper' => 'pre-defined-pages-table-wrapper',
]
],
];
}
}
......@@ -102,7 +108,7 @@ class BackstopConfigurationForm extends ConfigFormBase {
$form['actions']['add_page'] = [
'#type' => 'submit',
'#value' => t('Add one more'),
'#submit' => array('::addOne'),
'#submit' => ['::addOne'],
'#ajax' => [
'callback' => '::addmoreCallback',
'wrapper' => 'pre-defined-pages-table-wrapper',
......@@ -130,54 +136,50 @@ class BackstopConfigurationForm extends ConfigFormBase {
* {@inheritdoc}
*/
public function changeViewportsCallback(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValue('viewports');
$this->config('backstop_generator.backstopconfiguration')
->set('viewports', $values)
->save();
$form_state->setRebuild();
return $form['viewports'];
$values = $form_state->getValue('viewports');
$this->config('backstop_generator.backstopconfiguration')
->set('viewports', $values)
->save();
$form_state->setRebuild();
return $form['viewports'];
}
/**
* {@inheritdoc}
*/
public function addOne(array &$form, FormStateInterface $form_state) {
$uuid_service = \Drupal::service('uuid');
$uuid = $uuid_service->generate();
$newPage = array('page' => NULL);
$configString = 'pre_defined_pages_table.' . $uuid;
$this->config('backstop_generator.backstopconfiguration')
->set($configString, $newPage)
->save();
$form_state->setRebuild();
$uuid_service = \Drupal::service('uuid');
$uuid = $uuid_service->generate();
$newPage = ['page' => NULL];
$configString = 'pre_defined_pages_table.' . $uuid;
$this->config('backstop_generator.backstopconfiguration')
->set($configString, $newPage)
->save();
$form_state->setRebuild();
}
/**
* {@inheritdoc}
*/
public function addmoreCallback(array &$form, FormStateInterface $form_state) {
return $form['pre_defined_pages_table'];
return $form['pre_defined_pages_table'];
}
/**
* {@inheritdoc}
*/
public function removeCallback(array &$form, FormStateInterface $form_state) {
$parentID = $form_state->getTriggeringElement()['#id'];
$config = \Drupal::service('config.factory')->getEditable('backstop_generator.backstopconfiguration');
$config->clear('pre_defined_pages_table.' . $parentID)->save();
$form_state->setRebuild();
$parentID = $form_state->getTriggeringElement()['#id'];
$config = \Drupal::service('config.factory')->getEditable('backstop_generator.backstopconfiguration');
$config->clear('pre_defined_pages_table.' . $parentID)->save();
$form_state->setRebuild();
}
/**
* {@inheritdoc}
*/
public function removeCallbackCallback(array &$form, FormStateInterface $form_state) {
return $form['pre_defined_pages_table'];
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
return $form['pre_defined_pages_table'];
}
/**
......@@ -186,11 +188,11 @@ class BackstopConfigurationForm extends ConfigFormBase {
public function submitForm(array &$form, FormStateInterface $form_state) {
// Display result.
foreach ($form_state->getValues() as $key => $value) {
$this->config('backstop_generator.backstopconfiguration')
->set($key, $value);
$this->config('backstop_generator.backstopconfiguration')
->set($key, $value);
}
$this->config('backstop_generator.backstopconfiguration')
->save();
->save();
$redirect_url = Url::fromUri('internal:/admin/backstop_generator/backstop_configuration/view');
$form_state->setRedirectUrl($redirect_url);
}
......
......@@ -5,10 +5,10 @@ namespace Drupal\backstop_generator\Plugin\rest\resource;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\rest\ModifiedResourceResponse;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Drupal\backstop_generator\BackstopConfigRetrieverService;
/**
* Provides a resource to get view modes by entity and bundle.
......@@ -46,6 +46,8 @@ class BackstopConfigurationRest extends ResourceBase {
* A logger instance.
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* A current user instance.
* @param \Drupal\backstop_generator\BackstopConfigRetrieverService $backstopConfigRetrieverService
* The config fetcher service.
*/
public function __construct(
array $configuration,
......@@ -54,7 +56,7 @@ class BackstopConfigurationRest extends ResourceBase {
array $serializer_formats,
LoggerInterface $logger,
AccountProxyInterface $current_user,
$backstopConfigRetrieverService) {
BackstopConfigRetrieverService $backstopConfigRetrieverService) {
$this->backstopConfigRetrieverService = $backstopConfigRetrieverService;
parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);
......@@ -89,11 +91,11 @@ class BackstopConfigurationRest extends ResourceBase {
// You must to implement the logic of your REST Resource here.
// Use current user after pass authentication to validate access.
/*if (!$this->currentUser->hasPermission('access content')) {
if (!$this->currentUser->hasPermission('access content')) {
throw new AccessDeniedHttpException();
}*/
}
$entity = $this->backstopConfigRetrieverService->getConfigurationObject();
//we don't want to cache cache, so we need modifiedresourceresponse
// We don't want to cache cache, so we need modifiedresourceresponse.
return new ModifiedResourceResponse($entity, 200);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment