parent
150c974040
commit
c5c774290d
|
@ -50,11 +50,27 @@ class Post extends Node implements ContentEntityBundleInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function toTweet(): string {
|
public function toTweet(): string {
|
||||||
// TODO: Add tags.
|
$parts = [
|
||||||
|
$this->label(),
|
||||||
$parts = [$this->label(), $this->url('canonical', ['absolute' => TRUE])];
|
$this->url('canonical', ['absolute' => TRUE]),
|
||||||
|
$this->convertTermsToHashtags(),
|
||||||
|
];
|
||||||
|
|
||||||
return implode(PHP_EOL . PHP_EOL, $parts);
|
return implode(PHP_EOL . PHP_EOL, $parts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function convertTermsToHashtags(): string {
|
||||||
|
return $this->getTags()
|
||||||
|
->map(fn(Term $term) => $this->convertTermToHashtag($term))
|
||||||
|
->implode(' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
private function convertTermToHashtag(Term $tag): string {
|
||||||
|
$tagName = strtolower($tag->label());
|
||||||
|
$tagName = "#{$tagName}";
|
||||||
|
$tagName = str_replace(' ', '-', $tagName);
|
||||||
|
|
||||||
|
return $tagName;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
|
||||||
use Drupal\node\Entity\Node;
|
use Drupal\node\Entity\Node;
|
||||||
use Drupal\node\NodeInterface;
|
use Drupal\node\NodeInterface;
|
||||||
use Drupal\opdavies_blog\Entity\Node\Post;
|
use Drupal\opdavies_blog\Entity\Node\Post;
|
||||||
|
use Drupal\taxonomy\Entity\Term;
|
||||||
|
|
||||||
final class PostTest extends EntityKernelTestBase {
|
final class PostTest extends EntityKernelTestBase {
|
||||||
|
|
||||||
|
@ -43,6 +44,12 @@ final class PostTest extends EntityKernelTestBase {
|
||||||
public function it_converts_a_post_to_a_tweet(): void {
|
public function it_converts_a_post_to_a_tweet(): void {
|
||||||
/** @var Post $post */
|
/** @var Post $post */
|
||||||
$post = Node::create([
|
$post = Node::create([
|
||||||
|
'field_tags' => [
|
||||||
|
Term::create(['vid' => 'tags', 'name' => 'Automated testing']),
|
||||||
|
Term::create(['vid' => 'tags', 'name' => 'DDEV']),
|
||||||
|
Term::create(['vid' => 'tags', 'name' => 'Drupal']),
|
||||||
|
Term::create(['vid' => 'tags', 'name' => 'PHP']),
|
||||||
|
],
|
||||||
'status' => NodeInterface::PUBLISHED,
|
'status' => NodeInterface::PUBLISHED,
|
||||||
'title' => 'Creating a custom PHPUnit command for DDEV',
|
'title' => 'Creating a custom PHPUnit command for DDEV',
|
||||||
'type' => 'post',
|
'type' => 'post',
|
||||||
|
@ -53,6 +60,8 @@ final class PostTest extends EntityKernelTestBase {
|
||||||
Creating a custom PHPUnit command for DDEV
|
Creating a custom PHPUnit command for DDEV
|
||||||
|
|
||||||
http://localhost/node/1
|
http://localhost/node/1
|
||||||
|
|
||||||
|
#automated-testing #ddev #drupal #php
|
||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
$this->assertSame($expected, $post->toTweet());
|
$this->assertSame($expected, $post->toTweet());
|
||||||
|
@ -61,6 +70,8 @@ final class PostTest extends EntityKernelTestBase {
|
||||||
protected function setUp() {
|
protected function setUp() {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->installEntitySchema('taxonomy_term');
|
||||||
|
|
||||||
$this->installConfig(['opdavies_blog_test']);
|
$this->installConfig(['opdavies_blog_test']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue