From 9b26d772a835411ee8bf9a66364216d32f3e92a2 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Fri, 4 Sep 2020 19:45:30 +0100 Subject: [PATCH] Remove dashes in hashtags, and don't lowercase References #24 --- web/modules/custom/opdavies_blog/src/Entity/Node/Post.php | 8 +++----- .../tests/src/Kernel/Entity/Node/PostTest.php | 8 +++----- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/web/modules/custom/opdavies_blog/src/Entity/Node/Post.php b/web/modules/custom/opdavies_blog/src/Entity/Node/Post.php index 78c93e8..be2f561 100644 --- a/web/modules/custom/opdavies_blog/src/Entity/Node/Post.php +++ b/web/modules/custom/opdavies_blog/src/Entity/Node/Post.php @@ -75,11 +75,9 @@ class Post extends Node implements ContentEntityBundleInterface { } private function convertTermToHashtag(Term $tag): string { - $tagName = strtolower($tag->label()); - $tagName = "#{$tagName}"; - $tagName = str_replace(' ', '-', $tagName); - - return $tagName; + return '#' . (new Collection(explode(' ', $tag->label()))) + ->map(fn(string $word): string => ucfirst($word)) + ->implode(''); } } diff --git a/web/modules/custom/opdavies_blog/tests/src/Kernel/Entity/Node/PostTest.php b/web/modules/custom/opdavies_blog/tests/src/Kernel/Entity/Node/PostTest.php index 8c87c05..ad00379 100644 --- a/web/modules/custom/opdavies_blog/tests/src/Kernel/Entity/Node/PostTest.php +++ b/web/modules/custom/opdavies_blog/tests/src/Kernel/Entity/Node/PostTest.php @@ -5,8 +5,6 @@ declare(strict_types=1); namespace Drupal\Tests\custom\Kernel\Entity\Node; use Drupal\KernelTests\Core\Entity\EntityKernelTestBase; -use Drupal\node\Entity\Node; -use Drupal\opdavies_blog\Entity\Node\Post; use Drupal\opdavies_blog_test\Factory\PostFactory; final class PostTest extends EntityKernelTestBase { @@ -42,7 +40,7 @@ final class PostTest extends EntityKernelTestBase { public function it_converts_a_post_to_a_tweet(): void { $post = (new PostFactory()) ->setTitle('Creating a custom PHPUnit command for DDEV') - ->withTags(['Automated testing', 'DDEV', 'Drupal', 'PHP']) + ->withTags(['Automated testing', 'DDEV', 'Drupal', 'Drupal 8', 'PHP']) ->create(); $post->save(); @@ -52,7 +50,7 @@ final class PostTest extends EntityKernelTestBase { http://localhost/node/1 - #automated-testing #ddev #drupal #php + #AutomatedTesting #DDEV #Drupal #Drupal8 #PHP EOF; $this->assertSame($expected, $post->toTweet()); @@ -72,7 +70,7 @@ final class PostTest extends EntityKernelTestBase { http://localhost/node/1 - #drupal #php + #Drupal #PHP EOF; $this->assertSame($expected, $post->toTweet());