Last active
August 7, 2019 10:28
-
-
Save JLNNN/eef29bf12f17f3e0d951b24c31a8a6f8 to your computer and use it in GitHub Desktop.
Unit test for generating slugs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace My\NamespaceExt\Task; | |
use TYPO3\CMS\Core\Charset\CharsetConverter; | |
use TYPO3\CMS\Core\Utility\GeneralUtility; | |
class GenerateValidSegmentPath { | |
/** | |
* @param string $slug | |
* | |
* @return string | |
*/ | |
public function generate(string $string): string { | |
return $this->sanitize($string); | |
} | |
/** | |
* code from: https://github.com/TYPO3/TYPO3.CMS/blob/v9.5.8/typo3/sysext/core/Classes/DataHandling/SlugHelper.php | |
* Cleans a slug value so it is used directly in the path segment of a URL. | |
* | |
* @param string $slug | |
* | |
* @return string | |
*/ | |
private function sanitize(string $slug): string { | |
// Convert to lowercase + remove tags | |
$slug = mb_strtolower($slug, 'utf-8'); | |
$slug = strip_tags($slug); | |
// Convert some special tokens (space, "_" and "-") to the space character | |
$fallbackCharacter = '-'; | |
$slug = preg_replace('/[ \t\x{00A0}\-+_]+/u', $fallbackCharacter, $slug); | |
// Convert extended letters to ascii equivalents | |
// The specCharsToASCII() converts "€" to "EUR" | |
$slug = GeneralUtility::makeInstance(CharsetConverter::class) | |
->specCharsToASCII('utf-8', $slug); | |
// Get rid of all invalid characters, but allow slashes | |
$slug = preg_replace('/[^\p{L}0-9\/'.preg_quote($fallbackCharacter).']/u', '', $slug); | |
// Convert multiple fallback characters to a single one | |
if ($fallbackCharacter !== '') { | |
$slug = preg_replace('/'.preg_quote($fallbackCharacter).'{2,}/', $fallbackCharacter, $slug); | |
} | |
// Ensure slug is lower cased after all replacement was done | |
$slug = mb_strtolower($slug, 'utf-8'); | |
// Extract slug, thus it does not have wrapping fallback and slash characters | |
$extractedSlug = $this->extract($slug); | |
// Remove trailing and beginning slashes, except if the trailing slash was added, then we'll re-add it | |
$appendTrailingSlash = $extractedSlug !== '' && substr($slug, -1) === '/'; | |
$slug = $extractedSlug.($appendTrailingSlash ? '/' : ''); | |
//if ($this->prependSlashInSlug && ($slug{0} ?? '') !== '/') { | |
// $slug = '/' . $slug; | |
//} | |
return $slug; | |
} | |
/** | |
* code from: https://github.com/TYPO3/TYPO3.CMS/blob/v9.5.8/typo3/sysext/core/Classes/DataHandling/SlugHelper.php | |
* Extracts payload of slug and removes wrapping delimiters, | |
* e.g. `/hello/world/` will become `hello/world`. | |
* | |
* @param string $slug | |
* | |
* @return string | |
*/ | |
private function extract(string $slug): string { | |
// Convert some special tokens (space, "_" and "-") to the space character | |
$fallbackCharacter = '-'; | |
return trim($slug, $fallbackCharacter.'/'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace My\NamespaceExt\Tests\Unit\Controller; | |
use My\NamespaceExt\Task\GenerateValidSegmentPath; | |
use PHPUnit\Framework\TestCase; | |
class GenerateValidSegmentPathsTest extends TestCase { | |
/** | |
* @var GenerateValidSegmentPath | |
*/ | |
protected $generateValidSegmentPathsSUT; | |
protected function setUp() { | |
define("TYPO3_OS", "WIN"); | |
define("PATH_site", "http://my-dev.localhost/"); | |
parent::setUp(); | |
} | |
public function testExecute() { | |
$beraterStack = [ | |
"Hans Werner", | |
"Maraike Schmeißt", | |
"Önur Gundalfsson" | |
]; | |
$beraterStackAssertResult = [ | |
"hans-werner", | |
"maraike-schmeisst", | |
"oenur-gundalfsson" | |
]; | |
$beraterStackActualResult = []; | |
$pathGenerator = new GenerateValidSegmentPath(); | |
foreach ($beraterStack as $berater) { | |
$beraterStackActualResult[] = $pathGenerator->generate($berater); | |
} | |
$this->assertEquals($beraterStackAssertResult, $beraterStackActualResult); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment