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 18use dokuwiki\Search\Indexer; 19 20/** 21 * Tests for the spatialhelper plugin. 22 * 23 * @group plugin_spatialhelper 24 * @group plugins 25 * 26 * @noinspection AutoloadingIssuesInspection 27 * @phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps 28 */ 29class indexing_test extends DokuWikiTest 30{ 31 /** 32 * copy data and add pages to the index. 33 */ 34 public static function setUpBeforeClass(): void 35 { 36 parent::setUpBeforeClass(); 37 TestUtils::rcopy(TMP_DIR, __DIR__ . '/data/'); 38 } 39 40 final public function setUp(): void 41 { 42 $this->pluginsEnabled = array( 43 'geophp', 44 'geotag', 45 'spatialhelper' 46 ); 47 48 global $conf; 49 $conf['allowdebug'] = 1; 50 $conf['dontlog'] = []; 51 $conf['cachetime'] = -1; 52 53 parent::setUp(); 54 55 $indexer = plugin_load('helper', 'spatialhelper_index'); 56 self::assertInstanceOf('helper_plugin_spatialhelper_index', $indexer); 57 58 $data = []; 59 search($data, $conf['datadir'], 'search_allpages', array('skipacl' => true)); 60 61 foreach ($data as $val) { 62 (new Indexer())->addPage($val['id']); 63 $indexer->updateSpatialIndex($val['id']); 64 } 65 } 66 67 /** 68 * @throws Exception if anything goes wrong 69 */ 70 final public function testIndexed(): void 71 { 72 // render the page 73 $request = new TestRequest(); 74 $response = $request->get(array('id' => 'geotag')); 75 76 // test metadata 77 self::assertEquals( 78 '52.132633;5.291266;9', 79 $response->queryHTML('meta[name="geo.position"]')->attr('content') 80 ); 81 self::assertEquals( 82 '52.132633, 5.291266', 83 $response->queryHTML('meta[name="ICBM"]')->attr('content') 84 ); 85 86 // test the geohash and index values 87 self::assertStringStartsWith( 88 'u17b86kyx7jv', 89 $response->queryHTML('meta[name="geo.geohash"]')->attr('content') 90 ); 91 } 92 93 94 final public function testIndexFileExists(): void 95 { 96 self::assertFileExists(TMP_DIR . '/data/index/spatial.idx'); 97 } 98 99 final public function testIndexFileNotEmpty(): void 100 { 101 self::assertGreaterThan(0, filesize(TMP_DIR . '/data/index/spatial.idx')); 102 } 103 104 /** 105 * @throws Exception 106 */ 107 final public function testSearchNearby(): void 108 { 109 $search = plugin_load('helper', 'spatialhelper_search'); 110 self::assertInstanceOf('helper_plugin_spatialhelper_search', $search); 111 112 $result = $search->findNearby('u17b86kyx7'); 113 self::assertIsArray($result); 114 self::assertNotEmpty($result); 115 self::assertEmpty($result['media']); 116 self::assertEquals('geotag', $result['pages'][0]['id']); 117 self::assertEquals('u17b86kyx7', $result['geohash']); 118 self::assertEquals(0.6, $result['precision']); 119 self::assertEqualsWithDelta(52.1326, $result['lat'], 0.001); 120 self::assertEqualsWithDelta(5.2912, $result['lon'], 0.001); 121 } 122} 123