Skip to content

Commit 6a9a144

Browse files
authored
Add integration and unit tests (#54)
* Add more tests Add an integration test for a geotag. * Update and rename general.test.php to spatialhelper.test.php Update spatialhelper.test.php * Update CI.yml Update requirements.txt * update * update * update 2 * fix indexing test * test update
1 parent 7ed64e8 commit 6a9a144

File tree

10 files changed

+157
-28
lines changed

10 files changed

+157
-28
lines changed

.github/workflows/CI.yml

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
name: 'CI'
22

3-
on:
4-
push:
5-
pull_request:
6-
branches:
7-
- master
8-
workflow_dispatch:
3+
on: [ push, pull_request, workflow_dispatch ]
4+
5+
env:
6+
PRESERVE_TMP: true
97

108
jobs:
11-
action:
12-
name: "CI (${{ github.event_name }}) for ${{ matrix.php-version }} ${{ matrix.dokuwiki-branch }}"
9+
test:
1310
strategy:
1411
fail-fast: false
1512
matrix:
16-
php-version: [ '8.1', '8.2' ]
13+
php-version: [ '8.2' ]
1714
dokuwiki-branch: [ 'master', 'stable' ]
1815
include:
19-
- php-version: '8.0'
20-
dokuwiki-branch: 'stable'
16+
- php-version: '8.4'
17+
dokuwiki-branch: 'master'
2118
- php-version: '8.3'
2219
dokuwiki-branch: 'master'
2320

2421
uses: mprins/.github/.github/workflows/test.yml@main
2522
with:
26-
php-version: ${{ matrix.php-version }}
27-
dokuwiki-branch: ${{ matrix.dokuwiki-branch }}
23+
php-version: "${{ matrix.php-version }}"
24+
dokuwiki-branch: "${{ matrix.dokuwiki-branch }}"
25+

_test/data/pages/geotag.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
A geotagged page
2+
3+
{{geotag>lat=52.132633, lon=5.291266, alt=9, placename:Sint-Oedenrode, region:NL-NB, country:NL, hide}}

_test/index.test.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace dokuwiki\plugin\spatialhelper\test;
44

55
use DokuWikiTest;
6-
use helper_plugin_spatialhelper_index;
76
use TestUtils;
87

98
/**
@@ -30,9 +29,11 @@ public static function setUpBeforeClass(): void
3029
}
3130

3231
/**
33-
* Testdata for @return array
32+
* Test data provider.
33+
* @return array
3434
* @see index_test::test_convertDMStoD
3535
*
36+
* @see index_test::test_convertDMStoD
3637
*/
3738
final public static function convertDMStoDTestdata(): array
3839
{
@@ -75,7 +76,7 @@ final public function setUp(): void
7576
final public function test_convertDMStoD(array $input, float $expected_output, string $msg): void
7677
{
7778
$index = plugin_load('helper', 'spatialhelper_index');
78-
assert($index instanceof helper_plugin_spatialhelper_index);
79+
self::assertInstanceOf('helper_plugin_spatialhelper_index', $index);
7980

8081
$actual_output = $index->convertDMStoD($input);
8182

@@ -85,7 +86,7 @@ final public function test_convertDMStoD(array $input, float $expected_output, s
8586
final public function test_ImageWithoutGeotag(): void
8687
{
8788
$index = plugin_load('helper', 'spatialhelper_index');
88-
assert($index instanceof helper_plugin_spatialhelper_index);
89+
self::assertInstanceOf('helper_plugin_spatialhelper_index', $index);
8990

9091
$actual_output = $index->getCoordsFromExif(':vesder_eupen_no_gps.jpg');
9192
self::assertFalse($actual_output, 'Expected no geotag to be found');
@@ -94,7 +95,7 @@ final public function test_ImageWithoutGeotag(): void
9495
final public function test_ImageWithGeotag(): void
9596
{
9697
$index = plugin_load('helper', 'spatialhelper_index');
97-
assert($index instanceof helper_plugin_spatialhelper_index);
98+
self::assertInstanceOf('helper_plugin_spatialhelper_index', $index);
9899

99100
// lat/lon: 37°4'36.12",31°39'21.96" or x/y: 31.6561,37.0767
100101
$actual_output = $index->getCoordsFromExif(':manavgat_restaurant_handost_with_gps.jpg');

_test/indexing.test.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
/*
3+
* Copyright (c) 2024 Mark C. Prins <mprins@users.sf.net>
4+
*
5+
* Permission to use, copy, modify, and distribute this software for any
6+
* purpose with or without fee is hereby granted, provided that the above
7+
* copyright notice and this permission notice appear in all copies.
8+
*
9+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16+
*/
17+
18+
/**
19+
* Tests for the spatialhelper plugin.
20+
*
21+
* @group plugin_spatialhelper
22+
* @group plugins
23+
*
24+
* @noinspection AutoloadingIssuesInspection
25+
* @phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps
26+
*/
27+
class indexing_test extends DokuWikiTest
28+
{
29+
/**
30+
* copy data and add pages to the index.
31+
*/
32+
public static function setUpBeforeClass(): void
33+
{
34+
parent::setUpBeforeClass();
35+
TestUtils::rcopy(TMP_DIR, __DIR__ . '/data/');
36+
}
37+
38+
final public function setUp(): void
39+
{
40+
$this->pluginsEnabled = array(
41+
'geophp',
42+
'geotag',
43+
'spatialhelper'
44+
);
45+
46+
global $conf;
47+
$conf['allowdebug'] = 1;
48+
$conf['dontlog'] = [];
49+
$conf['cachetime'] = -1;
50+
51+
parent::setUp();
52+
53+
$indexer = plugin_load('helper', 'spatialhelper_index');
54+
self::assertInstanceOf('helper_plugin_spatialhelper_index', $indexer);
55+
56+
$data = [];
57+
search($data, $conf['datadir'], 'search_allpages', array('skipacl' => true));
58+
59+
foreach ($data as $val) {
60+
idx_addPage($val['id']);
61+
$indexer->updateSpatialIndex($val['id']);
62+
}
63+
}
64+
65+
/**
66+
* @throws Exception if anything goes wrong
67+
*/
68+
final public function testIndexed(): void
69+
{
70+
// render the page
71+
$request = new TestRequest();
72+
$response = $request->get(array('id' => 'geotag'));
73+
74+
// test metadata
75+
self::assertEquals(
76+
'52.132633;5.291266;9',
77+
$response->queryHTML('meta[name="geo.position"]')->attr('content')
78+
);
79+
self::assertEquals(
80+
'52.132633, 5.291266',
81+
$response->queryHTML('meta[name="ICBM"]')->attr('content')
82+
);
83+
84+
// test the geohash and index values
85+
self::assertStringStartsWith(
86+
'u17b86kyx7jv',
87+
$response->queryHTML('meta[name="geo.geohash"]')->attr('content')
88+
);
89+
}
90+
91+
92+
final public function testIndexFileExists(): void
93+
{
94+
self::assertFileExists(TMP_DIR . '/data/index/spatial.idx');
95+
}
96+
97+
final public function testIndexFileNotEmpty(): void
98+
{
99+
self::assertGreaterThan(0, filesize(TMP_DIR . '/data/index/spatial.idx'));
100+
}
101+
102+
final public function testSearchNearby(): void
103+
{
104+
$search = plugin_load('helper', 'spatialhelper_search');
105+
self::assertInstanceOf('helper_plugin_spatialhelper_search', $search);
106+
107+
$result = $search->findNearby('u17b86kyx7');
108+
self::assertIsArray($result);
109+
self::assertNotEmpty($result);
110+
self::assertEmpty($result['media']);
111+
self::assertEquals('geotag', $result['pages'][0]['id']);
112+
self::assertEquals('u17b86kyx7', $result['geohash']);
113+
self::assertEquals(0.6, $result['precision']);
114+
self::assertEqualsWithDelta(52.1326, $result['lat'], 0.001);
115+
self::assertEqualsWithDelta(5.2912, $result['lon'], 0.001);
116+
}
117+
}

_test/general.test.php renamed to _test/spatialhelper.test.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
* @group plugin_spatialhelper
2424
* @group plugins
2525
*/
26-
class general_plugin_spatialhelper_test extends DokuWikiTest
26+
class spatialhelper_plugin_test extends DokuWikiTest
2727
{
2828

2929
protected $pluginsEnabled = array('spatialhelper');
3030

3131
/**
3232
* Simple test to make sure the plugin.info.txt is in correct format.
3333
*/
34-
final public function test_plugininfo(): void
34+
final public function test_plugininfo(): void
3535
{
3636
$file = __DIR__ . '/../plugin.info.txt';
3737
self::assertFileExists($file);
@@ -56,7 +56,7 @@ final public function test_plugininfo(): void
5656
/**
5757
* test if plugin is loaded.
5858
*/
59-
final public function test_plugin_spatialhelper_isloaded(): void
59+
final public function test_plugin_spatialhelper_isloaded(): void
6060
{
6161
global $plugin_controller;
6262
self::assertContains(

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "mprins/dokuwiki-plugin-spatialhelper",
33
"description": "Provides spatial indexing and spatial search facilities.",
4-
"homepage": " https://www.dokuwiki.org/plugin:spatialhelper",
4+
"homepage": "https://www.dokuwiki.org/plugin:spatialhelper",
55
"minimum-stability": "stable",
66
"license": "proprietary",
77
"type": "plugin",

helper/index.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,19 @@ final public function generateSpatialIndex(): bool
100100
*
101101
* @param string $id
102102
* the document ID
103+
* @param bool $verbose
104+
* if true, echo debug info
103105
* @throws Exception
104106
*/
105-
final public function updateSpatialIndex(string $id): bool
107+
final public function updateSpatialIndex(string $id, bool $verbose = false): bool
106108
{
107109
$geotags = p_get_metadata($id, 'geo');
108110
if (empty($geotags)) {
111+
if ($verbose) echo "No geo metadata found for page $id" . DOKU_LF;
109112
return false;
110113
}
111114
if (empty($geotags ['lon']) || empty($geotags ['lat'])) {
115+
if ($verbose) echo "No valid geo metadata found for page $id" . DOKU_LF;
112116
return false;
113117
}
114118
Logger::debug("Geo metadata found for page $id", $geotags);
@@ -143,7 +147,7 @@ private function addToIndex(string $geohash, string $id): bool
143147
$pageIds [] = $id;
144148
}
145149
// TODO shortcut, need to make sure there is only one element, if not the index is corrupt
146-
$knownHash = $knownHashes [0];
150+
$knownHash = $knownHashes [0] ?? '';
147151

148152
if ($knownHash === $geohash) {
149153
Logger::debug("Document $id was found in index and has the same geohash, nothing to do.");

helper/search.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ final public function findNearbyLatLon(float $lat, float $lon): array
8383

8484
/**
8585
* finds nearby elements in the index based on the geohash.
86-
* returns a list of documents and the bunding box.
86+
* returns a list of documents and the bounding box.
8787
*
8888
* @param string $geohash
8989
* @param Point|null $p
@@ -172,13 +172,18 @@ final public function findNearby(string $geohash, Point $p = null): array
172172
static fn($a, $b) => strnatcmp($a ['distance'], $b ['distance'])
173173
);
174174

175+
if (strlen($geohash) < 10) {
176+
$precision = $this->precision[strlen($geohash)];
177+
} else {
178+
$precision = $this->precision[9];
179+
}
175180
return [
176181
'pages' => $pages,
177182
'media' => $media,
178183
'lat' => $decodedPoint->y(),
179184
'lon' => $decodedPoint->x(),
180185
'geohash' => $geohash,
181-
'precision' => $this->precision [strlen($geohash)]
186+
'precision' => $precision
182187
];
183188
}
184189
}

plugin.info.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
base spatialhelper
22
author Mark C. Prins
33
email mprins@users.sf.net
4-
date 2025-01-31
4+
date 2025-02-08
55
name Spatial Helper plugin for DokuWiki
66
desc Provides spatial indexing and spatial search facilities.
77
url https://www.dokuwiki.org/plugin:spatialhelper

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
https://github.com/mprins/dokuwiki-plugin-geophp.git lib/plugins/geophp
1+
https://github.com/mprins/dokuwiki-plugin-geophp.git lib/plugins/geophp
2+
https://github.com/mprins/dokuwiki-plugin-geotag.git lib/plugins/geotag

0 commit comments

Comments
 (0)