FileFieldPathsPathautoTest.php in File (Field) Paths 8
File
tests/src/Functional/FileFieldPathsPathautoTest.php
View source
<?php
namespace Drupal\Tests\filefield_paths\Functional;
use Drupal\Component\Utility\Unicode;
class FileFieldPathsPathautoTest extends FileFieldPathsTestBase {
protected static $modules = [
'filefield_paths_test',
'file_test',
'image',
'pathauto',
'token',
];
public function testUi() {
$field_name = mb_strtolower($this
->randomMachineName());
$this
->createFileField($field_name, 'node', $this->contentType);
$this
->drupalGet("admin/structure/types/manage/{$this->contentType}/fields/node.{$this->contentType}.{$field_name}");
$session = $this
->assertSession();
foreach ([
'path',
'name',
] as $field) {
$session
->fieldExists("third_party_settings[filefield_paths][file_{$field}][options][pathauto]");
$element = $this
->xpath('//input[@name=:name]/@disabled', [
':name' => "third_party_settings[filefield_paths][file_{$field}][options][pathauto]",
]);
$this
->assertEmpty($element, 'Pathauto checkbox is not disabled in File ' . Unicode::ucfirst($field) . ' settings.');
}
}
public function testPathauto() {
$field_name = mb_strtolower($this
->randomMachineName());
$third_party_settings['filefield_paths']['file_path']['value'] = 'node/[node:title]';
$third_party_settings['filefield_paths']['file_path']['options']['pathauto'] = TRUE;
$third_party_settings['filefield_paths']['file_name']['value'] = '[node:title].[file:ffp-extension-original]';
$third_party_settings['filefield_paths']['file_name']['options']['pathauto'] = TRUE;
$this
->createFileField($field_name, 'node', $this->contentType, [], [], $third_party_settings);
$test_file = $this
->getTestFile('text');
$node_title = $this
->randomString() . ' ' . $this
->randomString();
$edit['title[0][value]'] = $node_title;
$edit['files[' . $field_name . '_0]'] = \Drupal::service('file_system')
->realpath($test_file
->getFileUri());
$this
->drupalGet("node/add/{$this->contentType}");
$this
->submitForm($edit, 'Save');
$nodes = \Drupal::service('entity_type.manager')
->getStorage('node')
->loadByProperties([
'title' => $node_title,
]);
$node = reset($nodes);
$parts = explode('/', $node
->getTitle());
foreach ($parts as &$part) {
$part = \Drupal::service('pathauto.alias_cleaner')
->cleanString($part);
}
$title = implode('/', $parts);
$this
->assertSame("public://node/{$title}/{$title}.txt", $node->{$field_name}[0]->entity
->getFileUri(), 'File path/name has been processed correctly by Pathauto');
}
}