Passed
Branch master (ef5451)
by Caen
03:29
created

HasArticleMetadataTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 3
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Hyde\Framework\Testing\Feature\Concerns;
4
5
use Hyde\Framework\Models\Pages\MarkdownPost;
6
use Hyde\Testing\TestCase;
7
use Illuminate\Support\Facades\Config;
8
9
/**
10
 * @covers \Hyde\Framework\Concerns\HasArticleMetadata
11
 *
12
 * @see \Hyde\Framework\Models\Metadata
13
 */
14
class HasArticleMetadataTest extends TestCase
15
{
16
    protected function setUp(): void
17
    {
18
        parent::setUp();
19
20
        Config::set('site.url', null);
21
    }
22
23
    public function test_get_metadata_returns_valid_array_when_initialized()
24
    {
25
        $page = MarkdownPost::make(matter: [
26
            'description' => 'foo',
27
            'author' => 'bar',
28
            'category' => 'cat',
29
        ]);
30
        $this->assertEquals([
31
            'description' => 'foo',
32
            'author' => 'bar',
33
            'keywords' => 'cat',
34
        ], $page->getMetadata());
35
    }
36
37
    public function test_get_meta_properties_returns_base_array_when_initialized_with_empty_front_matter()
38
    {
39
        $page = MarkdownPost::make();
40
        $this->assertEquals(['og:type' => 'article'], $page->getMetaProperties());
41
    }
42
43
    // Note that this currently assumes that the object using it is a Blog Post.
44
    public function test_get_meta_properties_contains_og_url_when_uri_path_set()
45
    {
46
        Config::set('site.url', 'https://example.com/foo');
47
        $page = MarkdownPost::make('bar');
48
49
        $this->assertEquals([
50
            'og:type' => 'article',
51
            'og:url' => 'https://example.com/foo/posts/bar.html',
52
        ], $page->getMetaProperties());
53
    }
54
55
    public function test_get_meta_properties_contains_og_title_when_title_set()
56
    {
57
        $page = MarkdownPost::make(matter: [
58
            'title' => 'foo',
59
        ]);
60
61
        $this->assertEquals([
62
            'og:type' => 'article',
63
            'og:title' => 'foo',
64
        ], $page->getMetaProperties());
65
    }
66
67
    public function test_get_meta_properties_contains_og_article_date_published_when_date_set()
68
    {
69
        $page = MarkdownPost::make(matter: [
70
            'date' => '2022-01-01 12:00',
71
        ]);
72
73
        $this->assertEquals([
74
            'og:type' => 'article',
75
            'og:article:published_time' => '2022-01-01T12:00:00+00:00',
76
        ], $page->getMetaProperties());
77
    }
78
79
    public function test_get_meta_properties_contains_image_metadata_when_featured_image_set_to_string()
80
    {
81
        $page = MarkdownPost::make(matter: [
82
            'image' => 'foo.jpg',
83
        ]);
84
85
        $this->assertEquals([
86
            'og:type' => 'article',
87
            'og:image' => 'foo.jpg',
88
        ], $page->getMetaProperties());
89
    }
90
91
    public function test_get_meta_properties_contains_image_metadata_when_featured_image_set_to_array_with_path()
92
    {
93
        $page = MarkdownPost::make(matter: [
94
            'image' => [
95
                'path' => 'foo.jpg',
96
            ],
97
        ]);
98
99
        $this->assertEquals([
100
            'og:type' => 'article',
101
            'og:image' => 'foo.jpg',
102
        ], $page->getMetaProperties());
103
    }
104
105
    public function test_get_meta_properties_contains_image_metadata_when_featured_image_set_to_array_with_uri()
106
    {
107
        $page = MarkdownPost::make(matter: [
108
            'image' => [
109
                'uri' => 'foo.jpg',
110
            ],
111
        ]);
112
113
        $this->assertEquals([
114
            'og:type' => 'article',
115
            'og:image' => 'foo.jpg',
116
        ], $page->getMetaProperties());
117
    }
118
119
    public function test_get_author_returns_author_name_when_author_set_to_array_using_username()
120
    {
121
        $page = MarkdownPost::make(matter: [
122
            'author' => [
123
                'username' => 'foo',
124
            ],
125
        ]);
126
127
        $this->assertEquals([
128
            'author' => 'foo',
129
        ], $page->getMetadata());
130
    }
131
132
    public function test_get_author_returns_author_name_when_author_set_to_array_using_name()
133
    {
134
        $page = MarkdownPost::make(matter: [
135
            'author' => [
136
                'name' => 'foo',
137
            ],
138
        ]);
139
140
        $this->assertEquals([
141
            'author' => 'foo',
142
        ], $page->getMetadata());
143
    }
144
145
    public function test_get_author_returns_guest_when_author_set_to_array_without_name_or_username()
146
    {
147
        $page = MarkdownPost::make(matter: [
148
            'author' => [],
149
        ]);
150
151
        $this->assertEquals([
152
            'author' => 'Guest',
153
        ], $page->getMetadata());
154
    }
155
}
156