Skip to content
Snippets Groups Projects
SophronTest.php 3.54 KiB
Newer Older
<?php

declare(strict_types=1);
mondrake's avatar
mondrake committed

mondrake's avatar
mondrake committed
namespace Drupal\Tests\sophron\Functional;
mondrake's avatar
mondrake committed

use Drupal\sophron\MimeMapManagerInterface;
mondrake's avatar
mondrake committed
use Drupal\Tests\BrowserTestBase;
use FileEye\MimeMap\MappingException;
use PHPUnit\Framework\Attributes\Group;
mondrake's avatar
mondrake committed

/**
mondrake's avatar
mondrake committed
 * Tests Sophron functionality.
mondrake's avatar
mondrake committed
 *
mondrake's avatar
mondrake committed
 * @group sophron
mondrake's avatar
mondrake committed
 */
#[Group('sophron')]
mondrake's avatar
mondrake committed
class SophronTest extends BrowserTestBase {
mondrake's avatar
mondrake committed

mondrake's avatar
mondrake committed
  /**
   * {@inheritdoc}
   */
mondrake's avatar
mondrake committed
  protected static $modules = ['sophron'];
mondrake's avatar
mondrake committed

mondrake's avatar
mondrake committed
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

mondrake's avatar
mondrake committed
  /**
   * {@inheritdoc}
   */
mondrake's avatar
mondrake committed
    parent::setUp();
    $this->drupalLogin($this->drupalCreateUser([
      'administer site configuration',
    ]));
  }

  /**
   * Test settings form.
   */
  public function testFormAndSettings(): void {
mondrake's avatar
mondrake committed
    // The default map has been set by install.
    $this->assertSame(MimeMapManagerInterface::DRUPAL_MAP, \Drupal::configFactory()->get('sophron.settings')->get('map_option'));
mondrake's avatar
mondrake committed
    $this->assertSame('', \Drupal::configFactory()->get('sophron.settings')->get('map_class'));
mondrake's avatar
mondrake committed

mondrake's avatar
mondrake committed
    // Load the form, and change the default map class.
    $this->drupalGet('admin/config/system/sophron');
mondrake's avatar
mondrake committed
    $edit = [
      'map_option' => (string) MimeMapManagerInterface::DEFAULT_MAP,
mondrake's avatar
mondrake committed
    ];
    $this->submitForm($edit, 'Save configuration');
mondrake's avatar
mondrake committed

mondrake's avatar
mondrake committed
    // FileEye map has been set as default, and gaps exists.
    $this->assertSession()->responseContains('Mapping gaps');
    $this->assertSame(MimeMapManagerInterface::DEFAULT_MAP, \Drupal::configFactory()->get('sophron.settings')->get('map_option'));
mondrake's avatar
mondrake committed
    $this->assertSame('', \Drupal::configFactory()->get('sophron.settings')->get('map_class'));

    // Set an invalid custom mapping class.
    $edit = [
      'map_option' => (string) MimeMapManagerInterface::CUSTOM_MAP,
mondrake's avatar
mondrake committed
      'map_class' => BrowserTestBase::class,
    ];
    $this->submitForm($edit, 'Save configuration');
mondrake's avatar
mondrake committed
    $this->assertSession()->responseContains('The map class is invalid.');
    $edit = [
      'map_option' => (string) MimeMapManagerInterface::DEFAULT_MAP,
mondrake's avatar
mondrake committed
    ];
    $this->submitForm($edit, 'Save configuration');
mondrake's avatar
mondrake committed

      $this->assertEquals('application/octet-stream', \Drupal::service(MimeMapManagerInterface::class)->getExtension('quxqux')->getDefaultType());
      $this->fail('MappingException was expected.');
    }
    catch (MappingException $e) {
      // Expected.
    }
mondrake's avatar
mondrake committed
    $this->assertSession()->fieldExists('map_commands');
    $edit = [
      'map_commands' => '- {method: addTypeExtensionMapping, arguments: [foo/bar, quxqux]}',
mondrake's avatar
mondrake committed
    ];
    $this->submitForm($edit, 'Save configuration');
mondrake's avatar
mondrake committed

    // Mapping errors: wrongly typed commands.
    $edit = [
      'map_commands' => "- {foo: aaa}\n- {method: addTypeExtensionMapping, arguments: [a/c, bbbb]}\n- bar: [bbb, ccc]\n",
mondrake's avatar
mondrake committed
    ];
    $this->submitForm($edit, 'Save configuration');
mondrake's avatar
mondrake committed
    $this->assertSession()->responseContains('The items at line(s) 1, 3 are wrongly typed.');

    // Mapping errors: YAML syntax.
    $edit = [
      'map_commands' => "- {method: aaa}\n{method: bbb}\n",
mondrake's avatar
mondrake committed
    ];
    $this->submitForm($edit, 'Save configuration');
mondrake's avatar
mondrake committed
    $this->assertSession()->responseContains('YAML syntax error');

    // Mapping errors: invalid method.
    $edit = [
      'map_commands' => '- {method: aaa, arguments: [paramA, paramB]}',
mondrake's avatar
mondrake committed
    ];
    $this->submitForm($edit, 'Save configuration');
mondrake's avatar
mondrake committed
    $this->assertSession()->responseContains('Mapping errors');
    $this->assertEquals([
      [
        'method' => 'aaa',
        'arguments' => ['paramA', 'paramB'],
      ],
mondrake's avatar
mondrake committed
    ], \Drupal::configFactory()->get('sophron.settings')->get('map_commands'));
mondrake's avatar
mondrake committed
  }

}