summaryrefslogtreecommitdiffstats
path: root/common/tests
diff options
context:
space:
mode:
Diffstat (limited to 'common/tests')
-rw-r--r--common/tests/GuzzleHarness.php20
-rw-r--r--common/tests/HelpersTest.php15
-rw-r--r--common/tests/InstallTest.php61
-rw-r--r--common/tests/PlanetConfigTest.php73
-rw-r--r--common/tests/PlanetErrorTest.php12
-rw-r--r--common/tests/PlanetTest.php84
6 files changed, 265 insertions, 0 deletions
diff --git a/common/tests/GuzzleHarness.php b/common/tests/GuzzleHarness.php
new file mode 100644
index 0000000..a3f2ac4
--- /dev/null
+++ b/common/tests/GuzzleHarness.php
@@ -0,0 +1,20 @@
+<?php
+
+use \PHPUnit\Framework\TestCase;
+use \GuzzleHttp\Client;
+
+class GuzzleHarness extends TestCase
+{
+
+ /** @var GuzzleHttp\Client */
+ protected $client = null;
+
+ public function setUp()
+ {
+ $this->client = new Client([
+ 'base_uri' => 'http://127.0.0.1:8081',
+ 'timeout' => 1,
+ ]);
+ }
+
+} \ No newline at end of file
diff --git a/common/tests/HelpersTest.php b/common/tests/HelpersTest.php
new file mode 100644
index 0000000..141e604
--- /dev/null
+++ b/common/tests/HelpersTest.php
@@ -0,0 +1,15 @@
+<?php
+
+use PHPUnit\Framework\TestCase;
+
+class HelpersTest extends TestCase
+{
+ function test_constant_time_compare()
+ {
+ $this->assertTrue(_hash_equals('abc', 'abc'));
+ $this->assertFalse(_hash_equals('abc', 'ab'));
+ $this->assertFalse(_hash_equals('ab', 'abc'));
+ $this->assertFalse(_hash_equals('abcd', 'adbc'));
+ $this->assertFalse(_hash_equals(0, 0));
+ }
+}
diff --git a/common/tests/InstallTest.php b/common/tests/InstallTest.php
new file mode 100644
index 0000000..7615f18
--- /dev/null
+++ b/common/tests/InstallTest.php
@@ -0,0 +1,61 @@
+<?php
+
+require_once 'GuzzleHarness.php';
+
+class InstallTest extends GuzzleHarness {
+
+ public function setUp()
+ {
+ parent::setUp();
+ removeCustomFiles();
+ }
+
+ public function tearDown()
+ {
+ parent::tearDown();
+ removeCustomFiles();
+ }
+
+ public function test_index_page_tells_moonmoon_is_not_installed()
+ {
+ $res = $this->client->get('/index.php');
+ $this->assertEquals(200, $res->getStatusCode());
+ $this->assertContains('install moonmoon', (string) $res->getBody());
+ }
+
+ public function test_install_page_loads_without_error()
+ {
+ $res = $this->client->get('/install.php');
+ $this->assertEquals(200, $res->getStatusCode());
+ $this->assertContains('Administrator password', (string) $res->getBody());
+ }
+
+ /**
+ * Regression test, `people.opml` was created by requesting `/install.php`
+ * even if the site was not installed: `touch()` was called to see if
+ * the path was writable but the file was not removed.
+ */
+ public function test_get_install_page_should_not_create_custom_files()
+ {
+ $this->client->get('/install.php');
+ $this->assertFalse(file_exists(custom_path('people.opml')));
+ $this->assertFalse(file_exists(custom_path('config.yml')));
+ $this->assertFalse(file_exists(custom_path('inc/pwc.inc.php')));
+ }
+
+ public function test_install_button()
+ {
+ $data = [
+ 'url' => 'http://127.0.0.1:8081/',
+ 'title' => 'My website',
+ 'password' => 'admin',
+ 'locale' => 'en',
+ ];
+
+ $res = $this->client->request('POST', '/install.php', [
+ 'form_params' => $data
+ ]);
+ $this->assertEquals(200, $res->getStatusCode());
+ $this->assertContains('Your moonmoon is ready.', (string) $res->getBody());
+ }
+} \ No newline at end of file
diff --git a/common/tests/PlanetConfigTest.php b/common/tests/PlanetConfigTest.php
new file mode 100644
index 0000000..4db6e90
--- /dev/null
+++ b/common/tests/PlanetConfigTest.php
@@ -0,0 +1,73 @@
+<?php
+
+use PHPUnit\Framework\TestCase;
+
+class PlanetConfigTest extends TestCase
+{
+ public function test_default_configuration_values()
+ {
+ $conf = new PlanetConfig();
+ $this->assertEquals('http://www.example.com/', $conf->getUrl());
+ }
+
+ public function test_merge_user_configuration_with_default_one()
+ {
+ $conf = new PlanetConfig(['url' => 'http://foobar.tld']);
+ $this->assertEquals('http://foobar.tld', $conf->getUrl());
+ }
+
+ public function test_generic_getter()
+ {
+ $conf = new PlanetConfig(['foo' => 'bar']);
+ $this->assertEquals('bar', $conf->foo);
+ }
+
+ public function test_generic_setter()
+ {
+ $conf = new PlanetConfig();
+ $conf->foo = 'bar';
+ $this->assertEquals('bar', $conf->foo);
+ }
+
+ public function test_normalize_key_name_on_merge()
+ {
+ $conf = new PlanetConfig(['FOO' => 'bar']);
+ $this->assertEquals('bar', $conf->foo);
+ }
+
+ public function test_normalize_key_name_on_generic_getter()
+ {
+ $conf = new PlanetConfig(['foo' => 'bar']);
+ $this->assertEquals('bar', $conf->FOO);
+ }
+
+ public function test_normalize_key_name_on_generic_setter()
+ {
+ $conf = new PlanetConfig();
+ $conf->FOO = 'bar';
+ $this->assertEquals('bar', $conf->foo);
+ }
+
+ public function test_to_array()
+ {
+ $conf = new PlanetConfig(['foo' => 'bar']);
+ $this->assertEquals('bar', $conf->toArray()['foo']);
+ $this->assertEquals('http://www.example.com/', $conf->toArray()['url']);
+ }
+
+ public function test_constructor_without_default_config()
+ {
+ $conf = new PlanetConfig(['foo' => 'bar'], false);
+ $this->assertEquals('bar', $conf->foo);
+ $this->assertEquals(1, sizeof($conf->toArray()));
+ }
+
+ public function test_to_yaml()
+ {
+ $conf = new PlanetConfig([], false);
+ $this->assertEquals("---\n", $conf->toYaml());
+
+ $conf = new PlanetConfig(['foo' => 'bar'], false);
+ $this->assertEquals("---\nfoo: bar\n", $conf->toYaml());
+ }
+}
diff --git a/common/tests/PlanetErrorTest.php b/common/tests/PlanetErrorTest.php
new file mode 100644
index 0000000..d2f4599
--- /dev/null
+++ b/common/tests/PlanetErrorTest.php
@@ -0,0 +1,12 @@
+<?php
+
+use PHPUnit\Framework\TestCase;
+
+class PlanetErrorTest extends TestCase
+{
+ public function test_to_string()
+ {
+ $error = new PlanetError(1, 'foo');
+ $this->assertEquals('notice: foo', $error->toString());
+ }
+} \ No newline at end of file
diff --git a/common/tests/PlanetTest.php b/common/tests/PlanetTest.php
new file mode 100644
index 0000000..5cec1ce
--- /dev/null
+++ b/common/tests/PlanetTest.php
@@ -0,0 +1,84 @@
+<?php
+
+use PHPUnit\Framework\TestCase;
+
+class FoolCategory {
+
+ protected $name;
+
+ function __construct($name)
+ {
+ $this->name = $name;
+ }
+
+ function get_label()
+ {
+ return $this->name;
+ }
+}
+
+class FoolItem
+{
+ protected $categories;
+
+ function __construct($categories)
+ {
+ foreach ($categories as $c)
+ $this->categories[] = new FoolCategory($c);
+ }
+
+ function get_categories() {
+ return $this->categories;
+ }
+}
+
+class PlanetTest extends TestCase
+{
+
+ protected $planet;
+ protected $items;
+
+ public function setUp()
+ {
+ $this->planet = new Planet();
+
+ $this->items = array(
+ new FoolItem(array('catA', 'catB', 'catC')),
+ new FoolItem(array('catB')),
+ new FoolItem(array('catA')),
+ new FoolItem(array('catC'))
+ );
+ }
+
+ protected function _after()
+ {
+ unset($this->planet);
+ }
+
+ public function testFilterItemsByCategoryWithInvalidCategory()
+ {
+ $this->assertEquals(count($this->planet->_filterItemsByCategory($this->items, null)), count($this->items));
+ $this->assertEquals(count($this->planet->_filterItemsByCategory($this->items, ' ')), count($this->items));
+ }
+
+ public function testFilterItemsByCategoryWithNonUsedCategory()
+ {
+ $this->assertEquals(count($this->planet->_filterItemsByCategory($this->items, 'catD')), 0);
+ }
+
+ public function testFilterItemsByCategoryWithValidCategory()
+ {
+ $this->assertEquals(count($this->planet->_filterItemsByCategory($this->items, 'catA')), 2);
+ $this->assertEquals(count($this->planet->_filterItemsByCategory($this->items, 'catB')), 2);
+ $this->assertEquals(count($this->planet->_filterItemsByCategory($this->items, 'catC')), 2);
+ $this->assertEquals(count($this->planet->_filterItemsByCategory($this->items, 'CATA')), 2);
+ }
+
+ public function testFilterItemsByCategoryWithMultipleCategory()
+ {
+ $this->assertEquals(count($this->planet->_filterItemsByCategory($this->items, 'catA,catB')), 3);
+ $this->assertEquals(count($this->planet->_filterItemsByCategory($this->items, 'catA,catB,catC')), 4);
+ $this->assertEquals(count($this->planet->_filterItemsByCategory($this->items, 'catA, catB')), 3);
+ }
+
+}