summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phpunit/maintenance/categoriesRdfTest.php
blob: 5068e7011f749814244e8ad047047699ce519d08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php

namespace MediaWiki\Tests\Maintenance;

use DumpCategoriesAsRdf;
use MediaWikiLangTestCase;

/**
 * @covers CategoriesRdf
 * @covers DumpCategoriesAsRdf
 */
class CategoriesRdfTest extends MediaWikiLangTestCase {
	public function getCategoryIterator() {
		return [
			// batch 1
			[
				(object)[
					'page_title' => 'Category One',
					'page_id' => 1,
					'pp_propname' => null,
					'cat_pages' => '20',
					'cat_subcats' => '10',
					'cat_files' => '3'
				],
				(object)[
					'page_title' => '2 Category Two',
					'page_id' => 2,
					'pp_propname' => 'hiddencat',
					'cat_pages' => 20,
					'cat_subcats' => 0,
					'cat_files' => 3
				],
			],
			// batch 2
			[
				(object)[
					'page_title' => 'Третья категория',
					'page_id' => 3,
					'pp_propname' => null,
					'cat_pages' => '0',
					'cat_subcats' => '0',
					'cat_files' => '0'
				],
			]
		];
	}

	public function getCategoryLinksIterator( $dbr, array $ids ) {
		$res = [];
		foreach ( $ids as $pageid ) {
			$res[] = (object)[ 'cl_from' => $pageid, 'cl_to' => "Parent of $pageid" ];
		}
		return $res;
	}

	public function testCategoriesDump() {
		$this->setMwGlobals( [
			'wgServer' => 'http://acme.test',
			'wgCanonicalServer' => 'http://acme.test',
			'wgArticlePath' => '/wiki/$1',
			'wgRightsUrl' => '//creativecommons.org/licenses/by-sa/3.0/',
		] );

		$dumpScript =
			$this->getMockBuilder( DumpCategoriesAsRdf::class )
				->setMethods( [ 'getCategoryIterator', 'getCategoryLinksIterator' ] )
				->getMock();

		$dumpScript->expects( $this->once() )
			->method( 'getCategoryIterator' )
			->willReturn( $this->getCategoryIterator() );

		$dumpScript->expects( $this->any() )
			->method( 'getCategoryLinksIterator' )
			->willReturnCallback( [ $this, 'getCategoryLinksIterator' ] );

		/** @var DumpCategoriesAsRdf  $dumpScript */
		$logFileName = tempnam( sys_get_temp_dir(), "Categories-DumpRdfTest" );
		$outFileName = tempnam( sys_get_temp_dir(), "Categories-DumpRdfTest" );

		$dumpScript->loadParamsAndArgs(
			null,
			[
				'log' => $logFileName,
				'output' => $outFileName,
				'format' => 'nt',
			]
		);

		$dumpScript->execute();
		$actualOut = file_get_contents( $outFileName );
		$actualOut = preg_replace(
			'|<http://acme.test/wiki/Special:CategoryDump> <http://schema.org/dateModified> "[^"]+?"|',
			'<http://acme.test/wiki/Special:CategoryDump> <http://schema.org/dateModified> "{DATE}"',
			$actualOut
		);

		$outFile = __DIR__ . '/../data/categoriesrdf/categoriesRdf-out.nt';
		$this->assertFileContains( $outFile, $actualOut );
	}

}