invoicemgmt-1.0.0/tests/src/Functional/InvoiceMgmtSettingsFormTest.php

tests/src/Functional/InvoiceMgmtSettingsFormTest.php
<?php

declare(strict_types=1);

namespace Drupal\Tests\invoicemgmt\Functional;

use Drupal\Tests\BrowserTestBase;
use Drupal\file\Entity\File;

/**
 * Functional tests for InvoiceMgmtSettingsForm.
 *
 * @group invoicemgmt
 */
class InvoiceMgmtSettingsFormTest extends BrowserTestBase {

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

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'system',
    'user',
    'field',
    'text',
    'node',
    'file',
    'invoicemgmt',
  ];

  /**
   * Tests access to the settings form.
   */
  public function testSettingsFormAccess(): void {
    // Anonymous users should not have access.
    $this->drupalGet('/admin/config/invoicemgmt/settings');
    $this->assertSession()->statusCodeEquals(403);

    // Regular users should not have access.
    $user = $this->drupalCreateUser();
    $this->drupalLogin($user);
    $this->drupalGet('/admin/config/invoicemgmt/settings');
    $this->assertSession()->statusCodeEquals(403);

    // Admin users should have access.
    $admin_user = $this->drupalCreateUser(['administer invoice management settings']);
    $this->drupalLogin($admin_user);
    $this->drupalGet('/admin/config/invoicemgmt/settings');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->pageTextContains('Invoice Management Settings');
  }

  /**
   * Tests the settings form fields.
   */
  public function testSettingsFormFields(): void {
    $admin_user = $this->drupalCreateUser(['administer invoice management settings']);
    $this->drupalLogin($admin_user);
    $this->drupalGet('/admin/config/invoicemgmt/settings');

    // Check that all expected fields are present.
    $this->assertSession()->fieldExists('logo[0][fids]');
    $this->assertSession()->fieldExists('terms_conditions');
    $this->assertSession()->fieldExists('bank_details');
    $this->assertSession()->fieldExists('currency');
    $this->assertSession()->fieldExists('seller_name');
    $this->assertSession()->fieldExists('seller_address');
    $this->assertSession()->fieldExists('invoice_number_prefix');
    $this->assertSession()->fieldExists('footer_text');
    $this->assertSession()->fieldExists('primary_color');
    $this->assertSession()->fieldExists('secondary_color');
    $this->assertSession()->fieldExists('accent_color');

    // Check currency options.
    $this->assertSession()->optionExists('currency', 'GBP');
    $this->assertSession()->optionExists('currency', 'USD');
    $this->assertSession()->optionExists('currency', 'AED');
    $this->assertSession()->optionExists('currency', 'INR');
    $this->assertSession()->optionExists('currency', 'EUR');

    // Check default values.
    $this->assertSession()->fieldValueEquals('currency', 'GBP');
    $this->assertSession()->fieldValueEquals('invoice_number_prefix', 'INV');
    $this->assertSession()->fieldValueEquals('footer_text', 'Thank you for your business!');
    $this->assertSession()->fieldValueEquals('primary_color', '#667eea');
    $this->assertSession()->fieldValueEquals('secondary_color', '#764ba2');
    $this->assertSession()->fieldValueEquals('accent_color', '#4299e1');
  }

  /**
   * Tests form submission with valid data.
   */
  public function testSettingsFormSubmission(): void {
    $admin_user = $this->drupalCreateUser(['administer invoice management settings']);
    $this->drupalLogin($admin_user);

    $edit = [
      'terms_conditions' => 'Test terms and conditions',
      'bank_details' => 'Test bank details',
      'currency' => 'USD',
      'seller_name' => 'Test Company Ltd',
      'seller_address' => '123 Test Street, Test City',
      'invoice_number_prefix' => 'TEST',
      'footer_text' => 'Custom footer text',
      'primary_color' => '#ff0000',
      'secondary_color' => '#00ff00',
      'accent_color' => '#0000ff',
    ];

    $this->drupalGet('/admin/config/invoicemgmt/settings');
    $this->submitForm($edit, 'Save configuration');

    // Check success message.
    $this->assertSession()->pageTextContains('The configuration options have been saved.');

    // Verify that values are saved.
    $config = $this->config('invoicemgmt.settings');
    $this->assertEquals('Test terms and conditions', $config->get('terms_conditions'));
    $this->assertEquals('Test bank details', $config->get('bank_details'));
    $this->assertEquals('USD', $config->get('currency'));
    $this->assertEquals('Test Company Ltd', $config->get('seller_name'));
    $this->assertEquals('123 Test Street, Test City', $config->get('seller_address'));
    $this->assertEquals('TEST', $config->get('invoice_number_prefix'));
    $this->assertEquals('Custom footer text', $config->get('footer_text'));
    $this->assertEquals('#ff0000', $config->get('primary_color'));
    $this->assertEquals('#00ff00', $config->get('secondary_color'));
    $this->assertEquals('#0000ff', $config->get('accent_color'));
  }

  /**
   * Tests form validation for required fields.
   */
  public function testSettingsFormValidation(): void {
    $admin_user = $this->drupalCreateUser(['administer invoice management settings']);
    $this->drupalLogin($admin_user);

    // Test with empty required fields.
    $edit = [
      'seller_name' => '',
      'seller_address' => '',
      'invoice_number_prefix' => '',
    ];

    $this->drupalGet('/admin/config/invoicemgmt/settings');
    $this->submitForm($edit, 'Save configuration');

    // Should show validation errors.
    $this->assertSession()->pageTextContains('Seller Name field is required.');
    $this->assertSession()->pageTextContains('Seller Address field is required.');
    $this->assertSession()->pageTextContains('Invoice Number Prefix field is required.');
  }

  /**
   * Tests form preserves existing configuration.
   */
  public function testSettingsFormPreservesConfiguration(): void {
    // Set initial configuration.
    $config = $this->config('invoicemgmt.settings');
    $config->set('seller_name', 'Initial Company')
      ->set('currency', 'EUR')
      ->set('primary_color', '#123456')
      ->save();

    $admin_user = $this->drupalCreateUser(['administer invoice management settings']);
    $this->drupalLogin($admin_user);
    $this->drupalGet('/admin/config/invoicemgmt/settings');

    // Check that form shows saved values.
    $this->assertSession()->fieldValueEquals('seller_name', 'Initial Company');
    $this->assertSession()->fieldValueEquals('currency', 'EUR');
    $this->assertSession()->fieldValueEquals('primary_color', '#123456');
  }

  /**
   * Tests file upload functionality.
   */
  public function testLogoFileUpload(): void {
    $admin_user = $this->drupalCreateUser([
      'administer invoice management settings',
      'access content',
    ]);
    $this->drupalLogin($admin_user);

    // Create a test file.
    $test_file = File::create([
      'filename' => 'test-logo.png',
      'uri' => 'public://test-logo.png',
      'status' => 1,
    ]);
    $test_file->save();

    // Set the logo in configuration.
    $config = $this->config('invoicemgmt.settings');
    $config->set('logo', $test_file->id())->save();

    $this->drupalGet('/admin/config/invoicemgmt/settings');

    // The file should be referenced in the form.
    // Note: This is a simplified test - in a real scenario you'd test actual file upload.
    $this->assertSession()->statusCodeEquals(200);
  }

  /**
   * Tests color field default values.
   */
  public function testColorFieldDefaults(): void {
    $admin_user = $this->drupalCreateUser(['administer invoice management settings']);
    $this->drupalLogin($admin_user);
    $this->drupalGet('/admin/config/invoicemgmt/settings');

    // Check that color fields have proper default values.
    $this->assertSession()->fieldValueEquals('primary_color', '#667eea');
    $this->assertSession()->fieldValueEquals('secondary_color', '#764ba2');
    $this->assertSession()->fieldValueEquals('accent_color', '#4299e1');
  }

  /**
   * Tests form ID.
   */
  public function testFormId(): void {
    $form = new \Drupal\invoicemgmt\Form\InvoiceMgmtSettingsForm();
    $this->assertEquals('invoicemgmt_settings', $form->getFormId());
  }

  /**
   * Tests getEditableConfigNames method.
   */
  public function testGetEditableConfigNames(): void {
    $form = new \Drupal\invoicemgmt\Form\InvoiceMgmtSettingsForm();
    $reflection = new \ReflectionClass($form);
    $method = $reflection->getMethod('getEditableConfigNames');
    $method->setAccessible(TRUE);
    $result = $method->invoke($form);
    $this->assertEquals(['invoicemgmt.settings'], $result);
  }

  /**
   * Tests field descriptions are present.
   */
  public function testFieldDescriptions(): void {
    $admin_user = $this->drupalCreateUser(['administer invoice management settings']);
    $this->drupalLogin($admin_user);
    $this->drupalGet('/admin/config/invoicemgmt/settings');

    // Check for specific field descriptions.
    $this->assertSession()->pageTextContains('Upload a logo for invoices.');
    $this->assertSession()->pageTextContains('Enter the terms and conditions text.');
    $this->assertSession()->pageTextContains('Enter bank account details.');
    $this->assertSession()->pageTextContains('Select the default currency for invoices.');
    $this->assertSession()->pageTextContains('Enter the seller/company name.');
    $this->assertSession()->pageTextContains('Enter the seller/company address.');
    $this->assertSession()->pageTextContains('Enter the prefix for invoice numbers');
    $this->assertSession()->pageTextContains('Enter custom footer text for invoices');
    $this->assertSession()->pageTextContains('Choose the primary color for invoice header');
    $this->assertSession()->pageTextContains('Choose the secondary color for invoice header gradient');
    $this->assertSession()->pageTextContains('Choose the accent color for highlights and borders');
  }

  /**
   * Tests textarea field row counts.
   */
  public function testTextareaRows(): void {
    $admin_user = $this->drupalCreateUser(['administer invoice management settings']);
    $this->drupalLogin($admin_user);
    $this->drupalGet('/admin/config/invoicemgmt/settings');

    // Check that textareas have the expected number of rows.
    $terms_field = $this->getSession()->getPage()->find('css', 'textarea[name="terms_conditions"]');
    $this->assertEquals('8', $terms_field->getAttribute('rows'));

    $bank_field = $this->getSession()->getPage()->find('css', 'textarea[name="bank_details"]');
    $this->assertEquals('6', $bank_field->getAttribute('rows'));

    $address_field = $this->getSession()->getPage()->find('css', 'textarea[name="seller_address"]');
    $this->assertEquals('4', $address_field->getAttribute('rows'));

    $footer_field = $this->getSession()->getPage()->find('css', 'textarea[name="footer_text"]');
    $this->assertEquals('3', $footer_field->getAttribute('rows'));
  }

  /**
   * Tests field maxlength attributes.
   */
  public function testFieldMaxLength(): void {
    $admin_user = $this->drupalCreateUser(['administer invoice management settings']);
    $this->drupalLogin($admin_user);
    $this->drupalGet('/admin/config/invoicemgmt/settings');

    // Check maxlength attributes.
    $seller_name_field = $this->getSession()->getPage()->find('css', 'input[name="seller_name"]');
    $this->assertEquals('255', $seller_name_field->getAttribute('maxlength'));

    $prefix_field = $this->getSession()->getPage()->find('css', 'input[name="invoice_number_prefix"]');
    $this->assertEquals('10', $prefix_field->getAttribute('maxlength'));
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc