Completed
Push — master ( 24c5f5...cb85bb )
by Peter
21s queued 14s
created

tests/phpunit/Unit/SemanticTasksMailerTest.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace ST\Tests;
3
4
use MediaWiki\Diff\ComplexityException;
5
use MWException;
6
use ST\Assignees;
7
use ST\SemanticTasksMailer;
8
use ST\UserMailer;
9
use TextContent;
10
use Title;
11
use User;
12
use WikiPage;
13
14
/**
15
 * @covers SemanticTasksMailer
16
 * @group semantic-tasks
17
 *
18
 * @license GNU GPL v2+
19
 * @since 3.0
20
 */
21
class SemanticTasksMailerTest extends \MediaWikiTestCase {
22
23
	/**
24
	 * Only needed for MW 1.31
25
	 */
26
	public function run( \PHPUnit_Framework_TestResult $result = null ) {
27
		// MW 1.31
28
		$this->setCliArg( 'use-normal-tables', true );
29
		parent::run( $result );
30
	}
31
32
	protected function overrideMwServices( $configOverrides = null, array $services = [] ) {
33
		/**
34
		 * `MediaWikiTestCase` isolates the result with  `MediaWikiTestResult` which
35
		 * ecapsultes the commandline args and since we need to use "real" tables
36
		 * as part of "use-normal-tables" we otherwise end-up with the `CloneDatabase`
37
		 * to create TEMPORARY  TABLE by default as in:
38
		 *
39
		 * CREATE TEMPORARY  TABLE `unittest_smw_di_blob` (LIKE `smw_di_blob`) and
40
		 * because of the TEMPORARY TABLE, MySQL (not MariaDB) will complain
41
		 * about things like:
42
		 *
43
		 * SELECT p.smw_title AS prop, o_id AS id0, o0.smw_title AS v0, o0.smw_namespace
44
		 * AS v1, o0.smw_iw AS v2, o0.smw_sortkey AS v3, o0.smw_subobject AS v4,
45
		 * o0.smw_sort AS v5 FROM `unittest_smw_di_wikipage` INNER JOIN
46
		 * `unittest_smw_object_ids` AS p ON p_id=p.smw_id INNER JOIN
47
		 * `unittest_smw_object_ids` AS o0 ON o_id=o0.smw_id WHERE (s_id='29') AND
48
		 * (p.smw_iw!=':smw') AND (p.smw_iw!=':smw-delete')
49
		 *
50
		 * Function: SMW\SQLStore\EntityStore\SemanticDataLookup::fetchSemanticDataFromTable
51
		 * Error: 1137 Can't reopen table: 'p' ()
52
		 *
53
		 * The reason is that `unittest_smw_object_ids` was created as TEMPORARY TABLE
54
		 * and p is referencing to a TEMPORARY TABLE as well which isn't allowed in
55
		 * MySQL.
56
		 *
57
		 * "You cannot refer to a TEMPORARY table more than once in the same query" [0]
58
		 *
59
		 * [0] https://dev.mysql.com/doc/refman/8.0/en/temporary-table-problems.html
60
		 */
61
		// MW 1.32+
62
		$this->setCliArg( 'use-normal-tables', true );
63
		parent::overrideMwServices( $configOverrides, $services );
64
	}
65
66
	/** @todo: expand tests */
67
	/**
68
	 * @covers SemanticTasksMailer::mailNotification
69
	 * @throws ComplexityException
70
	 * @throws MWException
71
	 */
72
	public function testMailNotification() {
73
		$userMailerMock = $this->createMock( UserMailer::class );
74
75
		$assignees = [ 'someone' ];
76
		$text = '';
77
		$title = new Title();
78
		$user = new \User();
79
		$status = 0; //ST_NEWTASK
80
81
		$userMailerMock->expects( $this->once() )
82
			->method( 'send' )
83
			->with( $assignees, $this->anything(), $this->anything(), $this->anything(), $this->anything() )
84
			->willReturn( \Status::newGood() );
85
86
		SemanticTasksMailer::setUserMailer( $userMailerMock );
87
		SemanticTasksMailer::mailNotification( $assignees, $text, $title, $user, $status );
88
	}
89
90
	/**
91
	 * @covers SemanticTasksMailer::generateDiffBodyTxt
92
	 * @throws ComplexityException
93
	 * @throws MWException
94
	 */
95
	public function testGenerateDiffBodyTxt() {
96
		$namespace = $this->getDefaultWikitextNS();
97
		$title = Title::newFromText( 'Kitten', $namespace );
98
99
		$context = new \RequestContext();
100
		$context->setTitle( $title );
101
102
		$page = WikiPage::factory( $title );
103
		$strings = [ "it is a kitten", "two kittens", "three kittens", "four kittens" ];
104
		$revisions = [];
105
		foreach ( $strings as $string ) {
106
			$content = \ContentHandler::makeContent( $string, $title );
107
			$page->doEditContent( $content, 'edit page' );
108
			$revisions[] = $page->getLatest();
109
		}
110
111
		$returnText = SemanticTasksMailer::generateDiffBodyTxt( $title, $context );
112
		$this->assertNotEquals( '', $returnText, 'Diff should not be empty string.' );
113
	}
114
115
	/**
116
	 * @covers Assignees::saveAssignees
117
	 */
118
	public function testSaveAssignees() {
119
		$title = new Title();
120
		$article = new WikiPage( $title );
121
		$assignees = new Assignees();
122
		$assignees->saveAssignees( $article );
123
	}
124
125
	/** @todo: add more tests or asserts */
126
	/**
127
	 * @covers SemanticTasksMailer::mailAssigneesUpdatedTask
128
	 * @throws MWException
129
	 */
130
	public function testMailAssigneesUpdatedTaskTrueOnMinorEdit() {
131
		$assignees = new Assignees();
132
		$title = new Title();
133
		$article = new WikiPage($title);
134
		$current_user = new User();
135
		$text = new TextContent('test TextContent');
136
		$summary = ''; // unused
137
		$minoredit = true; // or true;
138
		$watchthis = null; // unused
139
		$sectionanchor = null; // unused
140
		$flags = EDIT_NEW; // or other..
141
		$revision = null;
142
		try {
143
			$returnValue = SemanticTasksMailer::mailAssigneesUpdatedTask( $assignees, $article, $current_user, $text,
144
				$summary, $minoredit, $watchthis, $sectionanchor, $flags, $revision );
145
		} catch ( MWException $e ) {
146
147
		} catch ( ComplexityException $e ) {
148
149
		}
150
151
		$this->assertTrue($returnValue);
152
	}
153
154
	public function testGetAssignedUsersFromParserOutput() {
155
		$namespace = $this->getDefaultWikitextNS();
156
		$title = Title::newFromText( 'Some Random Page', $namespace );
157
		$article = WikiPage::factory( $title );
158
		$content = \ContentHandler::makeContent( 'this is some edit', $title );
159
		$article->doEditContent( $content, 'edit page' );
160
		$revision = $article->getRevision();
161
		$current_user = new User();
0 ignored issues
show
$current_user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
162
		$assignees = new Assignees();
163
		$assignendUsers = $assignees->getCurrentAssignees( $article, $revision );
164
165
		$this->assertEmpty( $assignendUsers );
166
	}
167
168
	/** @todo: fix covers annotation and remove this. */
169
	public function testValidCovers() {
170
		$this->assertTrue( true );
171
	}
172
}
173