You are here

QuoteSettingsFormTest.php in Quote 8.2

File

tests/src/Functional/QuoteSettingsFormTest.php
View source
<?php

namespace Drupal\Tests\quote\Functional;

use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;

/**
 * Tests settings form.
 *
 * @group quote
 */
class QuoteSettingsFormTest extends BrowserTestBase {

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

  /**
   * {@inheritdoc}
   */
  public static $modules = [
    'node',
    'quote',
  ];

  /**
   * The configuration factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Settings form url.
   *
   * @var \Drupal\Core\Url
   */
  protected $settingsRoute;

  /**
   * User with correct permissions.
   *
   * @var \Drupal\user\Entity\User
   */
  protected $user;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->user = $this
      ->drupalCreateUser([
      'administer quote',
    ]);
    $this->configFactory = $this->container
      ->get('config.factory');
    $this->settingsRoute = Url::fromRoute('quote.settings_form');
  }

  /**
   * Tests permissions to setting form.
   */
  public function testPermissionsToSettingsForm() {
    $this
      ->drupalGet($this->settingsRoute);
    $this
      ->assertSession()
      ->statusCodeEquals(403);
    $user = $this
      ->drupalCreateUser();
    $this
      ->drupalLogin($user);
    $this
      ->drupalGet($this->settingsRoute);
    $this
      ->assertSession()
      ->statusCodeEquals(403);
    $this
      ->drupalLogin($this->user);
    $this
      ->drupalGet($this->settingsRoute);
    $this
      ->assertSession()
      ->statusCodeEquals(200);
  }

  /**
   * Tests settings save.
   */
  public function testSettingsSaving() {
    $types = [
      'page',
      'article',
    ];
    foreach ($types as $type_id) {
      $this
        ->drupalCreateContentType([
        'type' => $type_id,
      ]);
    }
    $this
      ->drupalLogin($this->user);
    $this
      ->drupalGet($this->settingsRoute);
    $expected_values = $edit = [
      'quote_modes_quote_sel' => random_int(0, 1),
      'quote_modes_quote_all' => random_int(0, 1),
      'quote_modes_quote_reply_all' => random_int(0, 1),
      'quote_modes_quote_reply_sel' => random_int(0, 1),
      'quote_allow_comments' => random_int(0, 1),
      'quote_selector' => $this
        ->randomString(),
      'quote_limit' => random_int(1, 999),
      'quote_selector_comment_quote_all' => $this
        ->randomString(),
      'quote_selector_node_quote_all' => $this
        ->randomString(),
    ];
    $allowed_types = array_slice($types, 0, random_int(0, 1));
    foreach ($allowed_types as $type_id) {
      $edit['quote_allow_types[' . $type_id . ']'] = $type_id;
      $expected_values['quote_allow_types'][$type_id] = $type_id;
    }
    $this
      ->drupalPostForm(NULL, $edit, t('Save configuration'));
    foreach ($expected_values as $field => $expected_value) {
      $actual_value = $this
        ->config('quote.settings')
        ->get($field);
      $this
        ->assertEquals($expected_value, $actual_value);
    }
  }

}

Classes

Namesort descending Description
QuoteSettingsFormTest Tests settings form.