Completed
Push — master ( 7714d3...c690da )
by Francis
01:35
created

BlogEngineTest::testClientSideScripts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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