View source  
  <?php
namespace Drupal\Tests\feeds\Unit\Feeds\Target;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\feeds\Feeds\Target\Path;
use Drupal\feeds\FeedTypeInterface;
class PathTest extends FieldTargetTestBase {
  
  protected function getTargetClass() {
    return Path::class;
  }
  
  protected function getMockFieldDefinition(array $settings = []) {
    $definition = $this
      ->createMock(FieldDefinitionInterface::class);
    $definition
      ->expects($this
      ->any())
      ->method('getSettings')
      ->will($this
      ->returnValue($settings));
    $definition
      ->expects($this
      ->atLeastOnce())
      ->method('getFieldStorageDefinition')
      ->will($this
      ->returnValue($this
      ->createMock(FieldStorageDefinitionInterface::class)));
    return $definition;
  }
  
  public function testPrepareValue($expected, array $values) {
    $method = $this
      ->getMethod(Path::class, 'prepareTarget')
      ->getClosure();
    $configuration = [
      'feed_type' => $this
        ->createMock(FeedTypeInterface::class),
      'target_definition' => $method($this
        ->getMockFieldDefinition()),
    ];
    $target = new Path($configuration, 'path', []);
    $method = $this
      ->getProtectedClosure($target, 'prepareValue');
    $method(0, $values);
    $this
      ->assertSame($expected, $values['alias']);
  }
  
  public function valuesProvider() {
    return [
      'without-slash' => [
        'expected' => '/path',
        'values' => [
          'alias' => 'path ',
        ],
      ],
      'with-slash' => [
        'expected' => '/foo',
        'values' => [
          'alias' => '/foo ',
        ],
      ],
      'starting-with-space' => [
        'expected' => '/bar',
        'values' => [
          'alias' => ' bar',
        ],
      ],
      'starting-with-space-and-with-slash' => [
        'expected' => '/qux',
        'values' => [
          'alias' => ' /qux',
        ],
      ],
      'already-correctly-formatted' => [
        'expected' => '/foo-bar',
        'values' => [
          'alias' => '/foo-bar',
        ],
      ],
    ];
  }
}