summaryrefslogtreecommitdiff
blob: 4891a8051f8284369c0585d9e01d2b43bdae6441 (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
<?php
/**
 * @license GPL-2.0-or-later
 * @file
 */

/**
 * A utility trait containing reusable methods for use in tests
 * @since 2020.04
 */
trait TranslatablePageTestTrait {
	/**
	 * Creates a translatable page but does not mark it for translation.
	 *
	 * @param string $title
	 * @param string $content
	 * @param User $creator
	 * @return TranslatablePage
	 */
	public function createUnmarkedTranslatablePage(
		string $title, string $content, User $creator
	): TranslatablePage {
		return $this->createTranslatablePage( $title, $content, $creator, false );
	}

	/**
	 * Creates a translatable page and marks it for translation.
	 *
	 * @param string $title
	 * @param string $content
	 * @param User $creator
	 * @return TranslatablePage
	 */
	public function createMarkedTranslatablePage(
		string $title, string $content, User $creator
	): TranslatablePage {
		return $this->createTranslatablePage( $title, $content, $creator, true );
	}

	private function createTranslatablePage(
		string $title, string $content, User $creator, bool $markForTranslation
	): TranslatablePage {
		// Create new page
		$translatablePageTitle = Title::newFromText( $title );
		$page = WikiPage::factory( $translatablePageTitle );
		$text = "<translate>$content</translate>";
		$content = ContentHandler::makeContent( $text, $translatablePageTitle );
		$translatablePage = TranslatablePage::newFromTitle( $translatablePageTitle );

		// Create the page
		$editStatus = TranslateUtils::doPageEdit( $page, $content, $creator, __METHOD__ );

		if ( $markForTranslation ) {
			// Mark the page for translation
			$latestRevisionId = $editStatus->value['revision-record']->getId();
			$translatablePage->addMarkedTag( $latestRevisionId );
		}

		return $translatablePage;
	}
}