Merge "Title: Title::getSubpage should not lose the interwiki prefix"
[lhc/web/wiklou.git] / tests / phpunit / includes / SampleTest.php
1 <?php
2
3 /**
4 * @coversNothing Just a sample
5 */
6 class SampleTest extends MediaWikiLangTestCase {
7
8 /**
9 * Anything that needs to happen before your tests should go here.
10 */
11 protected function setUp() {
12 // Be sure to call the parent setup and teardown functions.
13 // This makes sure that all the various cleanup and restorations
14 // happen as they should (including the restoration for setMwGlobals).
15 parent::setUp();
16
17 // This sets the globals and will restore them automatically
18 // after each test.
19 $this->setContentLang( 'en' );
20 $this->setMwGlobals( [
21 'wgCapitalLinks' => true,
22 ] );
23 }
24
25 /**
26 * Anything cleanup you need to do should go here.
27 */
28 protected function tearDown() {
29 parent::tearDown();
30 }
31
32 /**
33 * Name tests so that PHPUnit can turn them into sentences when
34 * they run. You are encouraged to use the naming described at:
35 * https://phpunit.de/manual/6.5/en/other-uses-for-tests.html
36 */
37 public function testTitleObjectStringConversion() {
38 $title = Title::newFromText( "text" );
39 $this->assertInstanceOf( Title::class, $title, "Title creation" );
40 $this->assertEquals( "Text", $title, "Automatic string conversion" );
41
42 $title = Title::newFromText( "text", NS_MEDIA );
43 $this->assertEquals( "Media:Text", $title, "Title creation with namespace" );
44 }
45
46 /**
47 * If you want to run the same test with a variety of data, use a data provider.
48 * See https://phpunit.de/manual/6.5/en/writing-tests-for-phpunit.html
49 */
50 public static function provideTitles() {
51 return [
52 [ 'Text', NS_MEDIA, 'Media:Text' ],
53 [ 'Text', null, 'Text' ],
54 [ 'text', null, 'Text' ],
55 [ 'Text', NS_USER, 'User:Text' ],
56 [ 'Photo.jpg', NS_FILE, 'File:Photo.jpg' ]
57 ];
58 }
59
60 /**
61 * phpcs:disable Generic.Files.LineLength
62 * @dataProvider provideTitles
63 * See https://phpunit.de/manual/6.5/en/appendixes.annotations.html#appendixes.annotations.dataProvider
64 * phpcs:enable
65 */
66 public function testCreateBasicListOfTitles( $titleName, $ns, $text ) {
67 $title = Title::newFromText( $titleName, $ns );
68 $this->assertEquals( $text, "$title", "see if '$titleName' matches '$text'" );
69 }
70
71 public function testSetUpMainPageTitleForNextTest() {
72 $title = Title::newMainPage();
73 $this->assertEquals( "Main Page", "$title", "Test initial creation of a title" );
74
75 return $title;
76 }
77
78 /**
79 * Instead of putting a bunch of tests in a single test method,
80 * you should put only one or two tests in each test method. This
81 * way, the test method names can remain descriptive.
82 *
83 * If you want to make tests depend on data created in another
84 * method, you can create dependencies feed whatever you return
85 * from the dependant method (e.g. testInitialCreation in this
86 * example) as arguments to the next method (e.g. $title in
87 * testTitleDepends is whatever testInitialCreatiion returned.)
88 */
89
90 /**
91 * @depends testSetUpMainPageTitleForNextTest
92 * See https://phpunit.de/manual/6.5/en/appendixes.annotations.html#appendixes.annotations.depends
93 */
94 public function testCheckMainPageTitleIsConsideredLocal( $title ) {
95 $this->assertTrue( $title->isLocal() );
96 }
97
98 /**
99 * @expectedException InvalidArgumentException
100 * See https://phpunit.de/manual/6.5/en/appendixes.annotations.html#appendixes.annotations.expectedException
101 */
102 public function testTitleObjectFromObject() {
103 $title = Title::newFromText( Title::newFromText( "test" ) );
104 $this->assertEquals( "Test", $title->isLocal() );
105 }
106 }