Passed
Push — master ( e0e7ca...df89c7 )
by Francis
01:42
created

BlogEngineTest::testGetRecentPosts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 38
nc 1
nop 0
dl 0
loc 43
rs 9.312
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
use PHPUnit\Framework\TestCase;
4
5
final class BlogEngineTest extends TestCase {
6
7
  /**
8
   * Code Igniter Instance.
9
   * @var object
10
   */
11
  private static $ci;
12
  /**
13
   * Package name for simplicity
14
   * @var string
15
   */
16
  private const PACKAGE = "francis94c/blog";
17
18
  /**
19
   * Prerquisites for the Unit Tests.
20
   */
21
  public static function setUpBeforeClass(): void {
22
    self::$ci =& get_instance();
23
    self::$ci->load->database('mysqli://root@localhost/test_db');
24
    $queries = [
25
      "CREATE TABLE IF NOT EXISTS admins (id INT(7) AUTO_INCREMENT PRIMARY KEY, name VARCHAR(20) NOT NULL, password TEXT NOT NULL) Engine=InnoDB;",
26
      "INSERT INTO admins (id, name, password) VALUES (1, \"Dev\", \"does_not_matter_for_this_test\");"
27
    ];
28
    foreach ($queries as $query) {
29
      self::assertTrue(self::$ci->db->query($query), "$query, Ran sucessfully.");
30
    }
31
    self::$ci->load->splint("francis94c/blog", "+Blogger", null, "blogger");
32
  }
33
  /**
34
   * Test Constructor
35
   *
36
   * @tesdox Test Constructor. √
37
   */
38
  public function testConstructor(): void {
39
    $params = [
40
      "name" => "ronaldo"
41
    ];
42
    self::$ci->load->splint(self::PACKAGE, "+Blogger", $params, "messi");
43
    $this->assertEquals(Blogger::TABLE_PREFIX . "_ronaldo", self::$ci->messi->getName());
44
  }
45
  /**
46
   * Test all functions relating to the installation of a blog. this is just the
47
   * creation of tables under the hood.
48
   *
49
   * @testdox Test Installation of Blog with and Without Admin Constraints. √
50
   */
51
  public function testInstallBlog(): void {
52
    $this->assertTrue(self::$ci->blogger->install("test_blog"), "Blog Installed Successfuly without admin ID constraint.");
53
    $this->assertTrue(self::$ci->db->table_exists(Blogger::TABLE_PREFIX . "_test_blog"));
54
    $fields = self::$ci->db->list_fields(Blogger::TABLE_PREFIX . "_test_blog");
55
    $this->assertContains("id", $fields);
56
    $this->assertContains("title", $fields);
57
    $this->assertContains("content", $fields);
58
    $this->assertContains("slug", $fields);
59
    $this->assertContains("date_created", $fields);
60
    $this->assertContains("date_published", $fields);
61
    $this->assertTrue(self::$ci->blogger->install("test_blog"), "Verify CREATE IF NOT EXISTS clause");
62
    $this->assertTrue(self::$ci->blogger->install("admin_test_blog", "admins", "id", 7), "Create Blog with existent admin constarint");
63
    $fields = self::$ci->db->list_fields(Blogger::TABLE_PREFIX . "_admin_test_blog");
64
    $this->assertContains("id", $fields);
65
    $this->assertContains("title", $fields);
66
    $this->assertContains("content", $fields);
67
    $this->assertContains("slug", $fields);
68
    $this->assertContains("date_created", $fields);
69
    $this->assertContains("date_published", $fields);
70
    $this->assertContains("poster_id", $fields);
71
  }
72
  /**
73
   * Test Empty Blog.
74
   *
75
   * @depends testInstallBlog
76
   *
77
   * @testdox Test HTML Output When no Post is Present. √
78
   */
79
  public function testEmptyBlog(): void {
80
    self::$ci->blogger->setBlog("test_blog");
81
    $this->setOutputCallback(function ($output) {
82
      $this->assertRegExp("/<h3 class=\"w3-center w3-margin\">No Posts.<\/h3>/", $output);
83
      $this->assertRegExp("/<div class=\"w3-padding\">/", $output);
84
    });
85
    $this->assertTrue(self::$ci->blogger->renderPostItems(null, null, null, 1, 0), "Test load empty posts set");
86
  }
87
  /**
88
   * Test UI functions. This just out pust  HTML for manual inspection. The optimal
89
   * inspection for this part is to use the Code Igniter Unit Testing system that
90
   * outputs to a browser. See https://splint.cynobit/wiki
91
   *
92
   * @depends testInstallBlog
93
   *
94
   * @testdox Test Editor HTML Output. √
95
   */
96
  public function testEditor(): void {
97
98
    // === Collect Output ===
99
    $this->setOutputCallback(function () {});
100
    $this->assertTrue(self::$ci->blogger->loadEditor("my_callback")); // Outputs Editor HTML.
101
    $o = $this->getActualOutput();
102
    // ==/ Collect Output ===
103
104
    $this->assertRegExp("/<link rel=\"stylesheet\" href=\"https:\/\/www\.w3schools\.com\/w3css\/4\/w3.css\">/", $o);
105
    $this->assertRegExp("/<link rel=\"stylesheet\" href=\"https:\/\/cdn.jsdelivr.net\/simplemde\/latest\/simplemde.min.css\">/", $o);
106
    $this->assertRegExp("/<script src=\"https:\/\/cdn.jsdelivr.net\/simplemde\/latest\/simplemde.min.js\"><\/script>/", $o);
107
    $this->assertRegExp("/id=\"publishModal\"/", $o);
108
    $this->assertRegExp("/<input type=\"hidden\" name=\"id\" value=\"\"\/>/", $o);
109
    $this->assertRegExp("/\/my_callback/", $o);
110
111
    // Reset Output Callback.
112
    $this->setOutputCallback(function ($o) { return $o;});
113
114
    $this->expectOutputRegex("/value=\"1\"\/>/");
115
    $this->assertTrue(self::$ci->blogger->loadEditor("my_callback", 1)); // Outputs Editor HTML.
116
  }
117
  /**
118
   * Test the blog post saving functionality of the library.
119
   * Create, Save, Publish, Create and Publish
120
   *
121
   * @testdox Blog Save Tested without Admin Constraint. √
122
   *
123
   * @depends testInstallBlog
124
   */
125
  public function testBlogSaveNoAdmin(): void {
126
    // No Admin.
127
    self::$ci->blogger->setBlog("test_blog");
128
    $_POST["action"] = "save";
129
    $_POST["title"] = "Hello Title";
130
    $_POST["editor"] = "The Quick Brown Fox Jumped over the Lazy Dog.";
131
    $this->assertEquals(self::$ci->blogger->savePost(), Blogger::CREATE);
132
    $_POST["editor"] = "The Quick Brown Fox Jumped over the Lazy Dog. Again.";
133
    $_POST["id"] = 1;
134
    $this->assertEquals(self::$ci->blogger->savePost(), Blogger::EDIT);
135
    $post = self::$ci->blogger->getPost("Hello-Title", false);
136
    $this->assertTrue(is_array($post));
137
    $this->assertArrayHasKey("id", $post);
138
    $this->assertArrayHasKey("title", $post);
139
    $this->assertArrayHasKey("content", $post);
140
    $this->assertArrayHasKey("published", $post);
141
    $this->assertArrayHasKey("date_published", $post);
142
    $this->assertArrayHasKey("slug", $post);
143
    $this->assertEquals(1, $post["id"], "Assert Post ID");
144
    $this->assertEquals("Hello Title", $post["title"], "Assert Post Title");
145
    $this->assertEquals("The Quick Brown Fox Jumped over the Lazy Dog. Again.", $post["content"]);
146
    $this->assertEquals("Hello-Title", $post["slug"]);
147
    $this->assertEquals(0, $post["published"]);
148
    $this->assertEquals(null, $post["date_published"]);
149
    $_POST["action"] = "publish";
150
    $this->assertEquals(self::$ci->blogger->savePost(), Blogger::PUBLISH);
151
    $post = self::$ci->blogger->getPost("Hello-Title", false);
152
    $this->assertTrue(is_array($post));
153
    $this->assertEquals(1, $post["published"]);
154
    $this->assertNotEquals(null, $post["date_published"]);
155
    $_POST["action"] = "createAndPublish";
156
    $_POST["title"] = "Hello Title 2";
157
    $_POST["editor"] = "Create and Published Post.";
158
    unset($_POST["id"]);
159
    $this->assertEquals(Blogger::CREATE_AND_PUBLISH, self::$ci->blogger->savePost());
160
    $post = self::$ci->blogger->getPost("Hello-Title-2", false);
161
    $this->assertTrue(is_array($post));
162
    $this->assertArrayHasKey("id", $post);
163
    $this->assertArrayHasKey("title", $post);
164
    $this->assertArrayHasKey("content", $post);
165
    $this->assertArrayHasKey("published", $post);
166
    $this->assertArrayHasKey("date_published", $post);
167
    $this->assertArrayHasKey("slug", $post);
168
    $this->assertEquals(2, $post["id"], "Assert Post ID");
169
    $this->assertEquals("Hello Title 2", $post["title"], "Assert Post Title");
170
    $this->assertEquals("Create and Published Post.", $post["content"]);
171
    $this->assertEquals("Hello-Title-2", $post["slug"]);
172
    $this->assertEquals(1, $post["published"]);
173
    $this->assertNotEquals(null, $post["date_published"]);
174
    $this->assertEquals(Blogger::ABORT, self::$ci->blogger->savePost(), "No 2 blog posts can have the same title.");
175
  }
176
  /**
177
   * Test blogSave with Admin.
178
   *
179
   * @depends testBlogSaveNoAdmin
180
   */
181
  public function testBlogSaveWithAdmin(): void {
182
    self::$ci->blogger->setBlog("admin_test_blog");
183
    $_POST["action"] = "save";
184
    $_POST["title"] = "Admin Hello Title";
185
    $_POST["editor"] = "The Quick Brown Fox Jumped over the Lazy Dog.";
186
    unset($_POST["id"]);
187
    $this->assertEquals(self::$ci->blogger->savePost(1), Blogger::CREATE);
188
    $_POST["editor"] = "The Quick Brown Fox Jumped over the Lazy Dog. Again.";
189
    $_POST["id"] = 1;
190
    $this->assertEquals(self::$ci->blogger->savePost(1), Blogger::EDIT);
191
    $post = self::$ci->blogger->getPost("Admin-Hello-Title", false);
192
    $this->assertTrue(is_array($post));
193
    $this->assertArrayHasKey("id", $post);
194
    $this->assertArrayHasKey("title", $post);
195
    $this->assertArrayHasKey("content", $post);
196
    $this->assertArrayHasKey("published", $post);
197
    $this->assertArrayHasKey("date_published", $post);
198
    $this->assertArrayHasKey("slug", $post);
199
    $this->assertEquals(1, $post["id"], "Assert Post ID");
200
    $this->assertEquals("Admin Hello Title", $post["title"], "Assert Post Title");
201
    $this->assertEquals("The Quick Brown Fox Jumped over the Lazy Dog. Again.", $post["content"]);
202
    $this->assertEquals("Admin-Hello-Title", $post["slug"]);
203
    $this->assertEquals(0, $post["published"]);
204
    $this->assertEquals(null, $post["date_published"]);
205
    $_POST["action"] = "publish";
206
    $this->assertEquals(self::$ci->blogger->savePost(1), Blogger::PUBLISH);
207
    $post = self::$ci->blogger->getPost("Admin-Hello-Title", false);
208
    $this->assertTrue(is_array($post));
209
    $this->assertEquals(1, $post["published"]);
210
    $this->assertNotEquals(null, $post["date_published"]);
211
    $_POST["action"] = "createAndPublish";
212
    $_POST["title"] = "Admin Hello Title 2";
213
    $_POST["editor"] = "Create and Published Post.";
214
    unset($_POST["id"]);
215
    $this->assertEquals(Blogger::CREATE_AND_PUBLISH, self::$ci->blogger->savePost(1));
216
    $post = self::$ci->blogger->getPost("Admin-Hello-Title-2", false);
217
    $this->assertTrue(is_array($post));
218
    $this->assertArrayHasKey("id", $post);
219
    $this->assertArrayHasKey("title", $post);
220
    $this->assertArrayHasKey("content", $post);
221
    $this->assertArrayHasKey("published", $post);
222
    $this->assertArrayHasKey("date_published", $post);
223
    $this->assertArrayHasKey("slug", $post);
224
    $this->assertArrayHasKey("poster_id", $post);
225
    $this->assertEquals(2, $post["id"], "Assert Post ID");
226
    $this->assertEquals("Admin Hello Title 2", $post["title"], "Assert Post Title");
227
    $this->assertEquals("Create and Published Post.", $post["content"]);
228
    $this->assertEquals("Admin-Hello-Title-2", $post["slug"]);
229
    $this->assertEquals(1, $post["published"]);
230
    $this->assertNotEquals(null, $post["date_published"]);
231
    $this->assertEquals(Blogger::ABORT, self::$ci->blogger->savePost(1), "No 2 blog posts can have the same title.");
232
  }
233
  /**
234
   * Test content of editor when editing post.
235
   *
236
   * @depends testBlogSaveWithAdmin
237
   */
238
  public function testEditPostUI(): void {
239
    // === Collect Output ===
240
    $this->setOutputCallback(function () {});
241
    $this->assertTrue(self::$ci->blogger->loadEditor("a_callback", 1)); // Outputs Editor HTML.
242
    $o = $this->getActualOutput();
243
    // ==/ Collect Output ===
244
    $this->assertRegExp("/<div id=\"content\" style=\"display:none;\">(\n|\r|\r\n)The Quick Brown Fox Jumped over the Lazy Dog. Again.<\/div>/", $o);
245
    $this->assertRegExp("/<div class=\"w3-padding w3-margin w3-border w3-round\" id=\"preview\">(\n|\r|\r\n)  <p>The Quick Brown Fox Jumped over the Lazy Dog. Again.<\/p><\/div>/", $o);
246
  }
247
  /**
248
   * Test for Recent Post.
249
   *
250
   * @depends testBlogSaveWithAdmin
251
   */
252
  public function testGetRecentPosts():void {
253
    // Check Blog Post Counts for different alues of limit.
254
    $this->assertCount(2, self::$ci->blogger->getRecentPosts(0));
255
    $this->assertCount(1, self::$ci->blogger->getRecentPosts(1));
256
    $this->assertCount(2, self::$ci->blogger->getRecentPosts(2));
257
    $this->assertCount(2, self::$ci->blogger->getRecentPosts(5));
258
    $this->assertCount(2, self::$ci->blogger->getRecentPosts());
259
    // Validate contents of first post.
260
    $posts = self::$ci->blogger->getRecentPosts(5);
261
    $this->assertArrayHasKey("id", $posts[0]);
262
    $this->assertArrayHasKey("title", $posts[0]);
263
    $this->assertArrayHasKey("content", $posts[0]);
264
    $this->assertArrayHasKey("published", $posts[0]);
265
    $this->assertArrayHasKey("date_published", $posts[0]);
266
    $this->assertArrayHasKey("slug", $posts[0]);
267
    $this->assertArrayHasKey("poster_id", $posts[0]);
268
    $this->assertEquals(2, $posts[0]["id"], "Assert Post ID");
269
    $this->assertEquals("Admin Hello Title 2", $posts[0]["title"], "Assert Post Title");
270
    $this->assertEquals("Create and Published Post.", $posts[0]["content"]);
271
    $this->assertEquals("Admin-Hello-Title-2", $posts[0]["slug"]);
272
    $this->assertEquals(1, $posts[0]["published"]);
273
    $this->assertNotEquals(null, $posts[0]["date_published"]);
274
    // Validate contents of second post
275
    $this->assertArrayHasKey("id", $posts[1]);
276
    $this->assertArrayHasKey("title", $posts[1]);
277
    $this->assertArrayHasKey("content", $posts[1]);
278
    $this->assertArrayHasKey("published", $posts[1]);
279
    $this->assertArrayHasKey("date_published", $posts[1]);
280
    $this->assertArrayHasKey("slug", $posts[1]);
281
    $this->assertArrayHasKey("poster_id", $posts[1]);
282
    $this->assertEquals(1, $posts[1]["id"], "Assert Post ID");
283
    $this->assertEquals("Admin Hello Title", $posts[1]["title"], "Assert Post Title");
284
    $this->assertEquals("The Quick Brown Fox Jumped over the Lazy Dog. Again.", $posts[1]["content"]);
285
    $this->assertEquals("Admin-Hello-Title", $posts[1]["slug"]);
286
    $this->assertEquals(1, $posts[1]["published"]);
287
    $this->assertNotEquals(null, $posts[0]["date_published"]);
288
    // Test Filter.
289
    $_POST["action"] = "save";
290
    $_POST["title"] = "Test Filter";
291
    $_POST["editor"] = "The Quick Brown Fox Jumped over the Lazy Dog.";
292
    unset($_POST["id"]);
293
    $this->assertEquals(self::$ci->blogger->savePost(1), Blogger::CREATE);
294
    $this->assertCount(2, self::$ci->blogger->getRecentPosts(5, true));
295
  }
296
  // TODO: Test Hits
297
  /**
298
   * Test Single Post Rendering
299
   *
300
   * @depends testGetRecentPosts
301
   */
302
  public function testRenderPost(): void {
303
    // Default Post View
304
    // Test Content.
305
    $post = self::$ci->blogger->getPost("Admin-Hello-Title", false);
306
    $this->expectOutputRegex("/<h1><b>Admin Hello Title<\/b><\/h1>/");
307
    self::$ci->blogger->renderPost($post);
308
    $this->expectOutputRegex("/<p>The Quick Brown Fox Jumped over the Lazy Dog. Again.<\/p>/");
309
    self::$ci->blogger->renderPost($post);
310
    $this->expectOutputRegex("/<div class=\"w3-padding\">/");
311
    self::$ci->blogger->renderPost($post);
312
    // Test MarkUp
313
    $this->expectOutputRegex("/<div class=\"w3-padding\">([\w(\r|\n|\r\n) <>\/.]+)<\/div>/");
314
    self::$ci->blogger->renderPost($post);
315
    // Test Custom View.
316
    $this->expectOutputRegex("/BLOGAdmin Hello TitleCONTENT<p>The Quick Brown Fox Jumped over the Lazy Dog. Again.<\/p>/");
317
    self::$ci->blogger->renderPost($post, "../splints/" . self::PACKAGE . "/unit_tests/views/test_post_item");
318
  }
319
  /**
320
   * Test Setters and Getters.
321
   *
322
   * @testdox Getters and Setters. √
323
   */
324
  public function testDynamicFunctions(): void {
325
    self::$ci->blogger->setBlog("rocket_blog");
326
    $this->assertEquals(Blogger::TABLE_PREFIX . "_rocket_blog", self::$ci->blogger->getName(), "Blogger setBlog works.");
327
  }
328
  /**
329
   * Load Scripts Test.
330
   *
331
   * @testdox Client Side Scripts Test. √
332
   */
333
  public function testClientSideScripts(): void {
334
    $this->assertRegExp("/<link rel=\"stylesheet\" href=\"https:\/\/www\.w3schools\.com\/w3css\/4\/w3.css\">/", self::$ci->blogger->w3css());
335
    $this->assertRegExp("/<link rel=\"stylesheet\" href=\"https:\/\/use.fontawesome.com\/releases\/v5.3.1\/css\/all.css\"\/>/", self::$ci->blogger->fontsAwesome());
336
  }
337
  /**
338
   * Clear and Free up persistent used resources for this test class.
339
   */
340
  public static function tearDownAfterClass(): void {
341
    self::$ci->db->empty_table("blogger_posts_test_blog");
342
    self::$ci->db->empty_table("blogger_posts_admin_test_blog");
343
    self::$ci->db->empty_table("admins");
344
    self::$ci->load->dbforge();
345
    self::$ci->dbforge->drop_table("blogger_posts_test_blog");
346
    self::$ci->dbforge->drop_table("blogger_posts_admin_test_blog");
347
    self::$ci->dbforge->drop_table("admins");
348
    self::$ci->db->close();
349
  }
350
}
351