1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
use PHPUnit\Framework\TestCase; |
4
|
|
|
|
5
|
|
|
final class BlogTest extends TestCase { |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Code Igniter Instance. |
9
|
|
|
* @var object |
10
|
|
|
*/ |
11
|
|
|
private static $ci; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Prerquisites for the Unit Tests. |
15
|
|
|
*/ |
16
|
|
|
public static function setUpBeforeClass(): void { |
17
|
|
|
self::$ci =& get_instance(); |
18
|
|
|
self::$ci->load->database('mysqli://root@localhost/test_db'); |
19
|
|
|
$queries = [ |
20
|
|
|
"CREATE TABLE IF NOT EXISTS admins (id INT(7) AUTO_INCREMENT PRIMARY KEY, name VARCHAR(20) NOT NULL, password TEXT NOT NULL) Engine=InnoDB;", |
21
|
|
|
"INSERT INTO admins (id, name, password) VALUES (1, \"Dev\", \"does_not_matter_for_this_test\");" |
22
|
|
|
]; |
23
|
|
|
foreach ($queries as $query) { |
24
|
|
|
self::assertTrue(self::$ci->db->query($query), "$query, Ran sucessfully."); |
25
|
|
|
} |
26
|
|
|
self::$ci->load->splint("francis94c/blog", "+Blogger", null, "blogger"); |
27
|
|
|
} |
28
|
|
|
/** |
29
|
|
|
* Test all functions relating to the installation of a blog. this is just the |
30
|
|
|
* creation of tables under the hood. |
31
|
|
|
*/ |
32
|
|
|
public function testInstallBlog(): void { |
33
|
|
|
$this->assertTrue(self::$ci->blogger->install("test_blog"), "Blog Installed Successfuly without admin ID constraint."); |
34
|
|
|
$this->assertTrue(self::$ci->db->table_exists(Blogger::TABLE_PREFIX . "_test_blog")); |
35
|
|
|
$fields = self::$ci->db->list_fields(Blogger::TABLE_PREFIX . "_test_blog"); |
36
|
|
|
$this->assertContains("id", $fields); |
37
|
|
|
$this->assertContains("title", $fields); |
38
|
|
|
$this->assertContains("content", $fields); |
39
|
|
|
$this->assertContains("slug", $fields); |
40
|
|
|
$this->assertContains("date_created", $fields); |
41
|
|
|
$this->assertContains("date_published", $fields); |
42
|
|
|
$this->assertTrue(self::$ci->blogger->install("test_blog"), "Verify CREATE IF NOT EXISTS clause"); |
43
|
|
|
$this->assertTrue(self::$ci->blogger->install("admin_test_blog", "admins", "id", 7), "Create Blog with existent admin constarint"); |
44
|
|
|
$fields = self::$ci->db->list_fields(Blogger::TABLE_PREFIX . "_admin_test_blog"); |
45
|
|
|
$this->assertContains("id", $fields); |
46
|
|
|
$this->assertContains("title", $fields); |
47
|
|
|
$this->assertContains("content", $fields); |
48
|
|
|
$this->assertContains("slug", $fields); |
49
|
|
|
$this->assertContains("date_created", $fields); |
50
|
|
|
$this->assertContains("date_published", $fields); |
51
|
|
|
$this->assertContains("poster_id", $fields); |
52
|
|
|
} |
53
|
|
|
/** |
54
|
|
|
* Test UI functions. This just out pust HTML for manual inspection. The optimal |
55
|
|
|
* inspection for this part is to use the Code Igniter Unit Testing system that |
56
|
|
|
* outputs to a browser. See https://splint.cynobit/wiki |
57
|
|
|
* |
58
|
|
|
* @depends testInstallBlog |
59
|
|
|
*/ |
60
|
|
|
public function testUI(): void { |
61
|
|
|
$this->assertTrue(self::$ci->blogger->loadEditor("callback"), "Load Editor"); |
62
|
|
|
self::$ci->blogger->setBlog("test_blog"); |
63
|
|
|
$this->assertTrue(self::$ci->blogger->renderPostItems(null, null, null, 1, 0), "Test load empty posts set"); |
64
|
|
|
} |
65
|
|
|
/** |
66
|
|
|
* Test the blog post saving functionality of the library. |
67
|
|
|
* Create, Save, Publish, Create and Publish |
68
|
|
|
* |
69
|
|
|
* @depends testInstallBlog |
70
|
|
|
*/ |
71
|
|
|
public function testBlogSaveNoAdmin(): void { |
72
|
|
|
// No Admin. |
73
|
|
|
self::$ci->blogger->setBlog("test_blog"); |
74
|
|
|
$_POST["action"] = "save"; |
75
|
|
|
$_POST["title"] = "Hello Title"; |
76
|
|
|
$_POST["editor"] = "The Quick Brown Fox Jumped over the Lazy Dog."; |
77
|
|
|
$this->assertEquals(self::$ci->blogger->savePost(), Blogger::CREATE); |
78
|
|
|
$_POST["editor"] = "The Quick Brown Fox Jumped over the Lazy Dog. Again."; |
79
|
|
|
$_POST["id"] = 1; |
80
|
|
|
$this->assertEquals(self::$ci->blogger->savePost(), Blogger::EDIT); |
81
|
|
|
$this->assertTrue(self::$ci->blogger->renderPost("Hello-Title", null)); |
82
|
|
|
$post = self::$ci->blogger->getPost("Hello-Title", false); |
83
|
|
|
$this->assertTrue(is_array($post)); |
84
|
|
|
$this->assertArrayHasKey("id", $post); |
85
|
|
|
$this->assertArrayHasKey("title", $post); |
86
|
|
|
$this->assertArrayHasKey("content", $post); |
87
|
|
|
$this->assertArrayHasKey("published", $post); |
88
|
|
|
$this->assertArrayHasKey("date_published", $post); |
89
|
|
|
$this->assertArrayHasKey("slug", $post); |
90
|
|
|
$this->assertEquals(1, $post["id"], "Assert Post ID"); |
91
|
|
|
$this->assertEquals("Hello Title", $post["title"], "Assert Post Title"); |
92
|
|
|
$this->assertEquals("The Quick Brown Fox Jumped over the Lazy Dog. Again.", $post["content"]); |
93
|
|
|
$this->assertEquals("Hello-Title", $post["slug"]); |
94
|
|
|
$this->assertEquals(0, $post["published"]); |
95
|
|
|
$this->assertEquals(null, $post["date_published"]); |
96
|
|
|
$_POST["action"] = "publish"; |
97
|
|
|
$this->assertEquals(self::$ci->blogger->savePost(), Blogger::PUBLISH); |
98
|
|
|
$post = self::$ci->blogger->getPost("Hello-Title", false); |
99
|
|
|
$this->assertTrue(is_array($post)); |
100
|
|
|
$this->assertEquals(1, $post["published"]); |
101
|
|
|
$this->assertNotEquals(null, $post["date_published"]); |
102
|
|
|
$_POST["action"] = "createAndPublish"; |
103
|
|
|
$_POST["title"] = "Hello Title 2"; |
104
|
|
|
$_POST["editor"] = "Create and Published Post."; |
105
|
|
|
unset($_POST["id"]); |
106
|
|
|
$this->assertEquals(Blogger::CREATE_AND_PUBLISH, self::$ci->blogger->savePost()); |
107
|
|
|
$post = self::$ci->blogger->getPost("Hello-Title-2", false); |
108
|
|
|
$this->assertTrue(is_array($post)); |
109
|
|
|
$this->assertArrayHasKey("id", $post); |
110
|
|
|
$this->assertArrayHasKey("title", $post); |
111
|
|
|
$this->assertArrayHasKey("content", $post); |
112
|
|
|
$this->assertArrayHasKey("published", $post); |
113
|
|
|
$this->assertArrayHasKey("date_published", $post); |
114
|
|
|
$this->assertArrayHasKey("slug", $post); |
115
|
|
|
$this->assertEquals(2, $post["id"], "Assert Post ID"); |
116
|
|
|
$this->assertEquals("Hello Title 2", $post["title"], "Assert Post Title"); |
117
|
|
|
$this->assertEquals("Create and Published Post.", $post["content"]); |
118
|
|
|
$this->assertEquals("Hello-Title-2", $post["slug"]); |
119
|
|
|
$this->assertEquals(1, $post["published"]); |
120
|
|
|
$this->assertNotEquals(null, $post["date_published"]); |
121
|
|
|
$this->assertEquals(Blogger::ABORT, self::$ci->blogger->savePost(), "No 2 blog posts can have the same title."); |
122
|
|
|
} |
123
|
|
|
/** |
124
|
|
|
* Test blogSave with Admin. |
125
|
|
|
* |
126
|
|
|
* @depends testBlogSaveNoAdmin |
127
|
|
|
*/ |
128
|
|
|
public function testBlogSaveWithAdmin(): void { |
129
|
|
|
self::$ci->blogger->setBlog("admin_test_blog"); |
130
|
|
|
$_POST["action"] = "save"; |
131
|
|
|
$_POST["title"] = "Admin Hello Title"; |
132
|
|
|
$_POST["editor"] = "The Quick Brown Fox Jumped over the Lazy Dog."; |
133
|
|
|
unset($_POST["id"]); |
134
|
|
|
$this->assertEquals(self::$ci->blogger->savePost(1), Blogger::CREATE); |
135
|
|
|
$_POST["editor"] = "The Quick Brown Fox Jumped over the Lazy Dog. Again."; |
136
|
|
|
$_POST["id"] = 1; |
137
|
|
|
$this->assertEquals(self::$ci->blogger->savePost(1), Blogger::EDIT); |
138
|
|
|
$this->assertTrue(self::$ci->blogger->renderPost("Admin-Hello-Title", null)); |
139
|
|
|
$post = self::$ci->blogger->getPost("Admin-Hello-Title", false); |
140
|
|
|
$this->assertTrue(is_array($post)); |
141
|
|
|
$this->assertArrayHasKey("id", $post); |
142
|
|
|
$this->assertArrayHasKey("title", $post); |
143
|
|
|
$this->assertArrayHasKey("content", $post); |
144
|
|
|
$this->assertArrayHasKey("published", $post); |
145
|
|
|
$this->assertArrayHasKey("date_published", $post); |
146
|
|
|
$this->assertArrayHasKey("slug", $post); |
147
|
|
|
$this->assertEquals(1, $post["id"], "Assert Post ID"); |
148
|
|
|
$this->assertEquals("Admin Hello Title", $post["title"], "Assert Post Title"); |
149
|
|
|
$this->assertEquals("The Quick Brown Fox Jumped over the Lazy Dog. Again.", $post["content"]); |
150
|
|
|
$this->assertEquals("Admin-Hello-Title", $post["slug"]); |
151
|
|
|
$this->assertEquals(0, $post["published"]); |
152
|
|
|
$this->assertEquals(null, $post["date_published"]); |
153
|
|
|
$_POST["action"] = "publish"; |
154
|
|
|
$this->assertEquals(self::$ci->blogger->savePost(1), Blogger::PUBLISH); |
155
|
|
|
$post = self::$ci->blogger->getPost("Admin-Hello-Title", false); |
156
|
|
|
$this->assertTrue(is_array($post)); |
157
|
|
|
$this->assertEquals(1, $post["published"]); |
158
|
|
|
$this->assertNotEquals(null, $post["date_published"]); |
159
|
|
|
$_POST["action"] = "createAndPublish"; |
160
|
|
|
$_POST["title"] = "Admin Hello Title 2"; |
161
|
|
|
$_POST["editor"] = "Create and Published Post."; |
162
|
|
|
unset($_POST["id"]); |
163
|
|
|
$this->assertEquals(Blogger::CREATE_AND_PUBLISH, self::$ci->blogger->savePost(1)); |
164
|
|
|
$post = self::$ci->blogger->getPost("Admin-Hello-Title-2", false); |
165
|
|
|
$this->assertTrue(is_array($post)); |
166
|
|
|
$this->assertArrayHasKey("id", $post); |
167
|
|
|
$this->assertArrayHasKey("title", $post); |
168
|
|
|
$this->assertArrayHasKey("content", $post); |
169
|
|
|
$this->assertArrayHasKey("published", $post); |
170
|
|
|
$this->assertArrayHasKey("date_published", $post); |
171
|
|
|
$this->assertArrayHasKey("slug", $post); |
172
|
|
|
$this->assertArrayHasKey("poster_id", $post); |
173
|
|
|
$this->assertEquals(2, $post["id"], "Assert Post ID"); |
174
|
|
|
$this->assertEquals("Admin Hello Title 2", $post["title"], "Assert Post Title"); |
175
|
|
|
$this->assertEquals("Create and Published Post.", $post["content"]); |
176
|
|
|
$this->assertEquals("Admin-Hello-Title-2", $post["slug"]); |
177
|
|
|
$this->assertEquals(1, $post["published"]); |
178
|
|
|
$this->assertNotEquals(null, $post["date_published"]); |
179
|
|
|
$this->assertEquals(Blogger::ABORT, self::$ci->blogger->savePost(1), "No 2 blog posts can have the same title."); |
180
|
|
|
} |
181
|
|
|
/** |
182
|
|
|
* Test for Recent Post. |
183
|
|
|
* |
184
|
|
|
* @depends testBlogSaveWithAdmin |
185
|
|
|
*/ |
186
|
|
|
public function testGetRecentPosts():void { |
187
|
|
|
// Check Blog Post Counts for different alues of limit. |
188
|
|
|
$this->assertCount(2, self::$ci->blogger->getRecentPosts(0)); |
189
|
|
|
$this->assertCount(1, self::$ci->blogger->getRecentPosts(1)); |
190
|
|
|
$this->assertCount(2, self::$ci->blogger->getRecentPosts(2)); |
191
|
|
|
$this->assertCount(2, self::$ci->blogger->getRecentPosts(5)); |
192
|
|
|
$this->assertCount(2, self::$ci->blogger->getRecentPosts()); |
193
|
|
|
// Validate contents of first post. |
194
|
|
|
$posts = self::$ci->blogger->getRecentPosts(5); |
195
|
|
|
$this->assertArrayHasKey("id", $posts[0]); |
196
|
|
|
$this->assertArrayHasKey("title", $posts[0]); |
197
|
|
|
$this->assertArrayHasKey("content", $posts[0]); |
198
|
|
|
$this->assertArrayHasKey("published", $posts[0]); |
199
|
|
|
$this->assertArrayHasKey("date_published", $posts[0]); |
200
|
|
|
$this->assertArrayHasKey("slug", $posts[0]); |
201
|
|
|
$this->assertArrayHasKey("poster_id", $posts[0]); |
202
|
|
|
$this->assertEquals(2, $posts[0]["id"], "Assert Post ID"); |
203
|
|
|
$this->assertEquals("Admin Hello Title 2", $posts[0]["title"], "Assert Post Title"); |
204
|
|
|
$this->assertEquals("Create and Published Post.", $posts[0]["content"]); |
205
|
|
|
$this->assertEquals("Admin-Hello-Title-2", $posts[0]["slug"]); |
206
|
|
|
$this->assertEquals(1, $posts[0]["published"]); |
207
|
|
|
$this->assertNotEquals(null, $posts[0]["date_published"]); |
208
|
|
|
// Validate contents of second post |
209
|
|
|
$this->assertArrayHasKey("id", $posts[1]); |
210
|
|
|
$this->assertArrayHasKey("title", $posts[1]); |
211
|
|
|
$this->assertArrayHasKey("content", $posts[1]); |
212
|
|
|
$this->assertArrayHasKey("published", $posts[1]); |
213
|
|
|
$this->assertArrayHasKey("date_published", $posts[1]); |
214
|
|
|
$this->assertArrayHasKey("slug", $posts[1]); |
215
|
|
|
$this->assertArrayHasKey("poster_id", $posts[1]); |
216
|
|
|
$this->assertEquals(1, $posts[1]["id"], "Assert Post ID"); |
217
|
|
|
$this->assertEquals("Admin Hello Title", $posts[1]["title"], "Assert Post Title"); |
218
|
|
|
$this->assertEquals("The Quick Brown Fox Jumped over the Lazy Dog. Again.", $posts[1]["content"]); |
219
|
|
|
$this->assertEquals("Admin-Hello-Title", $posts[1]["slug"]); |
220
|
|
|
$this->assertEquals(1, $posts[1]["published"]); |
221
|
|
|
$this->assertNotEquals(null, $posts[0]["date_published"]); |
222
|
|
|
// Test Filter. |
223
|
|
|
$_POST["action"] = "save"; |
224
|
|
|
$_POST["title"] = "Test Filter"; |
225
|
|
|
$_POST["editor"] = "The Quick Brown Fox Jumped over the Lazy Dog."; |
226
|
|
|
unset($_POST["id"]); |
227
|
|
|
$this->assertEquals(self::$ci->blogger->savePost(1), Blogger::CREATE); |
228
|
|
|
$this->assertCount(2, self::$ci->blogger->getRecentPosts(5, true)); |
229
|
|
|
} |
230
|
|
|
// TODO: Test Hits |
231
|
|
|
/** |
232
|
|
|
* Test Single Post Rendering |
233
|
|
|
* |
234
|
|
|
* @depends testGetRecentPosts |
235
|
|
|
*/ |
236
|
|
|
public function testRenderPost(): void { |
237
|
|
|
// Default Post View |
238
|
|
|
// Test Content. |
239
|
|
|
$post = self::$ci->blogger->getPost("Admin-Hello-Title", false); |
240
|
|
|
$this->expectOutputRegex("/<h1><b>Admin Hello Title<\/b><\/h1>/"); |
241
|
|
|
self::$ci->blogger->renderPost($post); |
242
|
|
|
$this->expectOutputRegex("/<p>The Quick Brown Fox Jumped over the Lazy Dog. Again.<\/p>/"); |
243
|
|
|
self::$ci->blogger->renderPost($post); |
244
|
|
|
$this->expectOutputRegex("/<div class=\"w3-padding\">/"); |
245
|
|
|
self::$ci->blogger->renderPost($post); |
246
|
|
|
// Test MarkUp |
247
|
|
|
$this->expectOutputRegex("/<div class=\"w3-padding\">([\w(\r|\n|\r\n) <>\/.]+)<\/div>/"); |
248
|
|
|
self::$ci->blogger->renderPost($post); |
249
|
|
|
} |
250
|
|
|
/** |
251
|
|
|
* Test Setters and Getters. |
252
|
|
|
*/ |
253
|
|
|
public function testDynamicFunctions(): void { |
254
|
|
|
self::$ci->blogger->setBlog("rocket_blog"); |
255
|
|
|
$this->assertEquals(Blogger::TABLE_PREFIX . "_rocket_blog", self::$ci->blogger->getName(), "Blogger setBlog works."); |
256
|
|
|
} |
257
|
|
|
/** |
258
|
|
|
* Clear and Free up persistent used resources for this test class. |
259
|
|
|
*/ |
260
|
|
|
public static function tearDownAfterClass(): void { |
261
|
|
|
self::$ci->db->empty_table("blogger_posts_test_blog"); |
262
|
|
|
self::$ci->db->empty_table("blogger_posts_admin_test_blog"); |
263
|
|
|
self::$ci->db->empty_table("admins"); |
264
|
|
|
self::$ci->load->dbforge(); |
265
|
|
|
self::$ci->dbforge->drop_table("blogger_posts_test_blog"); |
266
|
|
|
self::$ci->dbforge->drop_table("blogger_posts_admin_test_blog"); |
267
|
|
|
self::$ci->dbforge->drop_table("admins"); |
268
|
|
|
self::$ci->db->close(); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|