Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/Plugins/Autoaudio/Configurator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);

/**
* @package s9e\TextFormatter
* @copyright Copyright (c) The s9e authors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace s9e\TextFormatter\Plugins\Autoaudio;

use s9e\TextFormatter\Plugins\AbstractStaticUrlReplacer\AbstractConfigurator;

class Configurator extends AbstractConfigurator
{
public array $fileExtensions = ['aac', 'flac', 'm4a', 'mp3', 'wav', 'wave'];
protected $attrName = 'src';
protected $tagName = 'AUDIO';

protected function getTemplate(): string
{
return '<p><a href="{@' . $this->attrName . '}"><xsl:value-of select="@filename"/></a>:</p><p><audio controls="" src="{@' . $this->attrName . '}"/></p>';
}

protected function setUp(): void
{
parent::setUp();

$tag = $this->configurator->tags[$this->tagName];
$tag->attributes->add('filename')->filterChain->append('urldecode');
$tag->attributePreprocessors->add($this->attrName, '/\\/(?\'filename\'[^\\/]+)$/');
}
}
1 change: 1 addition & 0 deletions src/Plugins/Autoaudio/Parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const tagPriority = -1;
15 changes: 15 additions & 0 deletions src/Plugins/Autoaudio/Parser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types=1);

/**
* @package s9e\TextFormatter
* @copyright Copyright (c) The s9e authors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace s9e\TextFormatter\Plugins\Autoaudio;

use s9e\TextFormatter\Plugins\AbstractStaticUrlReplacer\AbstractParser;

class Parser extends AbstractParser
{
protected int $tagPriority = -1;
}
158 changes: 158 additions & 0 deletions tests/Plugins/Autoaudio/ConfiguratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php

namespace s9e\TextFormatter\Tests\Plugins\Autoaudio;

use s9e\TextFormatter\Configurator\Items\AttributeFilters\UrlFilter;
use s9e\TextFormatter\Tests\Test;

/**
* @covers s9e\TextFormatter\Plugins\AbstractStaticUrlReplacer\AbstractConfigurator
* @covers s9e\TextFormatter\Plugins\Autoaudio\Configurator
*/
class ConfiguratorTest extends Test
{
/**
* @testdox Automatically creates an "AUDIO" tag with a "src" attribute with a "#url" filter
*/
public function testCreatesTag()
{
$this->configurator->Autoaudio;
$this->assertTrue($this->configurator->tags->exists('AUDIO'));

$tag = $this->configurator->tags->get('AUDIO');
$this->assertTrue($tag->attributes->exists('src'));

$attribute = $tag->attributes->get('src');
$this->assertTrue($attribute->filterChain->contains(new UrlFilter));
}

/**
* @testdox Automatically creates a "filename" attribute
*/
public function testCreatesFilenameAttribute()
{
$this->configurator->Autoaudio;
$tag = $this->configurator->tags->get('AUDIO');
$this->assertTrue($tag->attributes->exists('filename'));
}

/**
* @testdox Does not attempt to create a tag if it already exists
*/
public function testDoesNotCreateTag()
{
$tag = $this->configurator->tags->add('AUDIO');
$this->configurator->plugins->load('Autoaudio');

$this->assertSame($tag, $this->configurator->tags->get('AUDIO'));
}

/**
* @testdox The name of the tag used can be changed through the "tagName" constructor option
*/
public function testCustomTagName()
{
$this->configurator->plugins->load('Autoaudio', ['tagName' => 'FOO']);
$this->assertTrue($this->configurator->tags->exists('FOO'));
}

/**
* @testdox The name of the attribute used can be changed through the "attrName" constructor option
*/
public function testCustomAttrName()
{
$this->configurator->plugins->load('Autoaudio', ['attrName' => 'bar']);
$this->assertTrue($this->configurator->tags['AUDIO']->attributes->exists('bar'));
}

/**
* @testdox Has a quickMatch
*/
public function testConfigQuickMatch()
{
$this->assertArrayHasKey(
'quickMatch',
$this->configurator->plugins->load('Autoaudio')->asConfig()
);
}

/**
* @testdox The config array contains a regexp
*/
public function testConfigRegexp()
{
$this->assertArrayHasKey(
'regexp',
$this->configurator->plugins->load('Autoaudio')->asConfig()
);
}

/**
* @testdox The config array contains the name of the tag
*/
public function testConfigTagName()
{
$config = $this->configurator->plugins->load('Autoaudio')->asConfig();

$this->assertArrayHasKey('tagName', $config);
$this->assertSame('AUDIO', $config['tagName']);
}

/**
* @testdox The config array contains the name of the attribute
*/
public function testConfigAttributeName()
{
$config = $this->configurator->plugins->load('Autoaudio')->asConfig();

$this->assertArrayHasKey('attrName', $config);
$this->assertSame('src', $config['attrName']);
}

/**
* @testdox getTag() returns the tag that is associated with this plugin
*/
public function testGetTag()
{
$plugin = $this->configurator->plugins->load('Autoaudio');

$this->assertSame(
$this->configurator->tags['AUDIO'],
$plugin->getTag()
);
}

/**
* @testdox The JS parser contains both parser files
*/
public function testJSParser()
{
$js = $this->configurator->Autoaudio->getJSParser();

foreach (['AbstractStaticUrlReplacer', 'Autoaudio'] as $pluginName)
{
$filepath = __DIR__ . '/../../../src/Plugins/' . $pluginName . '/Parser.js';
$this->assertStringContainsString(file_get_contents($filepath), $js);
}
}

/**
* @testdox File extensions are configurable
*/
public function testFileExtensions()
{
$this->configurator->Autoaudio->fileExtensions = ['flac', 'wma'];
$this->configurator->Autoaudio->finalize();

$config = $this->configurator->Autoaudio->asConfig();

$this->assertMatchesRegularExpression(
$config['regexp'],
'https://example.org/audio.flac'
);
$this->assertDoesNotMatchRegularExpression(
$config['regexp'],
'https://example.org/audio.mp4'
);
}
}
98 changes: 98 additions & 0 deletions tests/Plugins/Autoaudio/ParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace s9e\TextFormatter\Tests\Plugins\Autoaudio;

use s9e\TextFormatter\Configurator;
use s9e\TextFormatter\Plugins\Autoaudio\Parser;
use s9e\TextFormatter\Tests\Plugins\ParsingTestsRunner;
use s9e\TextFormatter\Tests\Plugins\ParsingTestsJavaScriptRunner;
use s9e\TextFormatter\Tests\Plugins\RenderingTestsRunner;
use s9e\TextFormatter\Tests\Test;

/**
* @covers s9e\TextFormatter\Plugins\AbstractStaticUrlReplacer\AbstractParser
* @covers s9e\TextFormatter\Plugins\Autoaudio\Parser
*/
class ParserTest extends Test
{
use ParsingTestsRunner;
use ParsingTestsJavaScriptRunner;
use RenderingTestsRunner;

public static function getParsingTests()
{
return [
[
'.. http://example.org/audio.mp3 ..',
'<r>.. <AUDIO filename="audio.mp3" src="http://example.org/audio.mp3">http://example.org/audio.mp3</AUDIO> ..</r>'
],
[
'http://example.org/audio.mp3',
'<r><AUDIO filename="audio.mp3" src="http://example.org/audio.mp3">http://example.org/audio.mp3</AUDIO></r>'
],
[
'http://example.org/audio.wav',
'<r><AUDIO filename="audio.wav" src="http://example.org/audio.wav">http://example.org/audio.wav</AUDIO></r>'
],
[
'http://example.org/audio.aac',
'<r><AUDIO filename="audio.aac" src="http://example.org/audio.aac">http://example.org/audio.aac</AUDIO></r>'
],
[
'http://example.org/audio.flac',
'<r><AUDIO filename="audio.flac" src="http://example.org/audio.flac">http://example.org/audio.flac</AUDIO></r>'
],
[
'http://example.org/audio.m4a',
'<r><AUDIO filename="audio.m4a" src="http://example.org/audio.m4a">http://example.org/audio.m4a</AUDIO></r>'
],
[
'http://example.org/audio.wave',
'<r><AUDIO filename="audio.wave" src="http://example.org/audio.wave">http://example.org/audio.wave</AUDIO></r>'
],
[
'.. HTTP://EXAMPLE.ORG/AUDIO.MP3 ..',
'<r>.. <AUDIO filename="AUDIO.MP3" src="http://EXAMPLE.ORG/AUDIO.MP3">HTTP://EXAMPLE.ORG/AUDIO.MP3</AUDIO> ..</r>'
],
[
'.. http://user:pass@example.org/audio.mp3 ..',
'<t>.. http://user:pass@example.org/audio.mp3 ..</t>'
],
[
'.. http://example.org/my%20song%20(1).mp3 ..',
'<r>.. <AUDIO filename="my song (1).mp3" src="http://example.org/my%20song%20%281%29.mp3">http://example.org/my%20song%20(1).mp3</AUDIO> ..</r>'
],
[
'http://example.org/audio.mp4',
'<t>http://example.org/audio.mp4</t>'
],
[
'http://example.org/audio.mp3',
'<r><AUDIO filename="audio.mp3" src="http://example.org/audio.mp3"><URL url="http://example.org/audio.mp3">http://example.org/audio.mp3</URL></AUDIO></r>',
[],
function ($configurator)
{
$configurator->Autolink;
}
],
[
'https://recitals.pianoworld.com/recital_files/Recital_65/11.%20navindra%20Navindra%20Umanee%20-%20Bluebird.mp3',
'<r><AUDIO filename="11. navindra Navindra Umanee - Bluebird.mp3" src="https://recitals.pianoworld.com/recital_files/Recital_65/11.%20navindra%20Navindra%20Umanee%20-%20Bluebird.mp3">https://recitals.pianoworld.com/recital_files/Recital_65/11.%20navindra%20Navindra%20Umanee%20-%20Bluebird.mp3</AUDIO></r>'
],
];
}

public static function getRenderingTests()
{
return [
[
'http://example.org/audio.mp3',
'<p><a href="http://example.org/audio.mp3">audio.mp3</a>:</p><p><audio controls="" src="http://example.org/audio.mp3"></audio></p>'
],
[
'http://example.org/my%20song.mp3',
'<p><a href="http://example.org/my%20song.mp3">my song.mp3</a>:</p><p><audio controls="" src="http://example.org/my%20song.mp3"></audio></p>'
],
];
}
}