1<?php 2 3 4/** 5 * Test the endpoint page of the api plugin 6 * 7 * @group plugin_api 8 * @group plugins 9 * @uses \PHPUnit\Framework\TestCase 10 */ 11include_once(__DIR__ . '/utils.php'); 12 13class dokuwiki_plugin_api_page_test extends DokuWikiTest 14{ 15 16 const ENDPOINT_NAME = 'page'; 17 18 protected $pluginsEnabled = array(action_plugin_api::PLUGIN_NAME); 19 /** 20 * @var JSON 21 */ 22 private static $JSON; 23 24 static function setUpBeforeClass(): void 25 { 26 self::$JSON = new JSON(JSON_LOOSE_TYPE); 27 $file = __DIR__ . '/../plugin.info.txt'; 28 self::$PLUGIN_INFO = confToHash($file); 29 30 } 31 32 /** 33 * Test the pages function 34 */ 35 public function test_plugin_page() 36 { 37 38 // $conf must not be in a static method 39 global $conf; 40 // Use heading as title 41 $conf['useheading'] = 1; 42 43 /** 44 * Set things up 45 */ 46 // Create a page, the endpoint namespace is important to avoid race condition with other test 47 $homePageId = self::ENDPOINT_NAME."home"; 48 $summaryDefault = 'Summary'; 49 saveWikiText($homePageId, 'Home Page', $summaryDefault); 50 idx_addPage($homePageId); 51 52 // Backlinks 53 // The endpoint namespace is important to avoid race condition with other test 54 $backlinkHomePageId = self::ENDPOINT_NAME."PageWithLinkToHome"; 55 $externalLink = 'https://gerardnico.com'; 56 $backlinkHomePageTitle = 'Backlink Page Heading 1'; 57 saveWikiText($backlinkHomePageId, '====== ' . $backlinkHomePageTitle . '======' . DOKU_LF . 58 '[['.$homePageId.']] - [[' . $externalLink . ']]', $summaryDefault); 59 idx_addPage($backlinkHomePageId); 60 61 62 /** 63 * Query Home page 64 */ 65 $queryParameters = array( 66 'fn' => self::ENDPOINT_NAME, 67 'id' => $homePageId 68 ); 69 $response = dokuwiki_plugin_api_util::getRequest($queryParameters); 70 $data = self::$JSON->decode($response->getContent()); 71 72 /** 73 * Test 74 */ 75 76 77 // Same Title 78 $actualPageTitle = $data['title']; 79 $expectedTitle = $homePageId; 80 $this->assertEquals($expectedTitle, $actualPageTitle, "A page title without header must be the page id"); 81 82 // Html 83 $expectedHtml = DOKU_LF . '<p>' . DOKU_LF . 'Home Page' . DOKU_LF . '</p>' . DOKU_LF; 84 $actualHtml = $data['html']; 85 $this->assertEquals($expectedHtml, $actualHtml, "The Html must be the same"); 86 87 // backlinks 88 $actualBacklinks = $data['backlinks']; 89 $expectedBacklinks = array( 90 0 => $backlinkHomePageId 91 ); 92 $this->assertEquals($expectedBacklinks, $actualBacklinks, "The Backlinks must be the same"); 93 94 /** 95 * Query Second page 96 */ 97 $queryParameters = array( 98 'fn' => 'page', 99 'id' => $backlinkHomePageId 100 ); 101 $response = dokuwiki_plugin_api_util::getRequest($queryParameters); 102 $data = self::$JSON->decode($response->getContent()); 103 104 // Title 105 $actualPageTitle = $data['title']; 106 $expectedTitle = $backlinkHomePageTitle; 107 $this->assertEquals($expectedTitle, $actualPageTitle, "A page title with header must be the first heading"); 108 109 // internal links 110 $actualLinks = $data['links']; 111 $expectedLinks = array( 112 0 => $homePageId 113 ); 114 $this->assertEquals($expectedLinks, $actualLinks, "The links must be the same"); 115 116 // external links 117 $actualExternalLinks = $data['external_links']; 118 $expectedExternalLinks = array( 119 0 => $externalLink 120 ); 121 $this->assertEquals($expectedExternalLinks, $actualExternalLinks, "The externals links must be the same"); 122 123 124 } 125 126} 127