Page MenuHomePhabricator

BlueSpice BSApiTasksTestBase causes Undefined index:
Open, NormalPublic

Description

BlueSpiceDashboards and BlueSpiceRSSFeeder fail some tests with an Undefined index: when testing a task:

1) BSApiDashboardWidgetsTasksTest::testWikipage
Undefined index: 

tests/phpunit/includes/api/ApiTestCase.php:129
tests/phpunit/includes/api/ApiTestCase.php:173
extensions/BlueSpiceFoundation/tests/phpunit/BSApiTasksTestBase.php:22
extensions/BlueSpiceDashboards/tests/phpunit/BSApiDashboardWidgetsTasksTest.php:43
tests/phpunit/MediaWikiTestCase.php:469
maintenance/doMaintenance.php:94
1) BSApiTasksRSSFeederTest::testInvalidUrl
Undefined index: 

tests/phpunit/includes/api/ApiTestCase.php:129
tests/phpunit/includes/api/ApiTestCase.php:173
extensions/BlueSpiceFoundation/tests/phpunit/BSApiTasksTestBase.php:22
extensions/BlueSpiceRSSFeeder/tests/phpunit/BSApiTasksRSSFeederTest.php:28
tests/phpunit/MediaWikiTestCase.php:469
maintenance/doMaintenance.php:94

2) BSApiTasksRSSFeederTest::testValidNonWhitelistedUrl
Undefined index: 

tests/phpunit/includes/api/ApiTestCase.php:129
tests/phpunit/includes/api/ApiTestCase.php:173
extensions/BlueSpiceFoundation/tests/phpunit/BSApiTasksTestBase.php:22
extensions/BlueSpiceRSSFeeder/tests/phpunit/BSApiTasksRSSFeederTest.php:41
tests/phpunit/MediaWikiTestCase.php:469
maintenance/doMaintenance.php:94

3) BSApiTasksRSSFeederTest::testValidWhitelistedUnreachableUrl
Undefined index: 

tests/phpunit/includes/api/ApiTestCase.php:129
tests/phpunit/includes/api/ApiTestCase.php:173
extensions/BlueSpiceFoundation/tests/phpunit/BSApiTasksTestBase.php:22
extensions/BlueSpiceRSSFeeder/tests/phpunit/BSApiTasksRSSFeederTest.php:55
tests/phpunit/MediaWikiTestCase.php:469
maintenance/doMaintenance.php:94

4) BSApiTasksRSSFeederTest::testActualUrl
Undefined index: 

tests/phpunit/includes/api/ApiTestCase.php:129
tests/phpunit/includes/api/ApiTestCase.php:173
extensions/BlueSpiceFoundation/tests/phpunit/BSApiTasksTestBase.php:22
extensions/BlueSpiceRSSFeeder/tests/phpunit/BSApiTasksRSSFeederTest.php:69
tests/phpunit/MediaWikiTestCase.php:469
maintenance/doMaintenance.php:94

Event Timeline

hashar triaged this task as Normal priority.Jul 16 2018, 3:42 PM
hashar created this task.
hashar renamed this task from [BlueSpiceDashboards] BSApiDashboardWidgetsTasksTest::testWikipage: Undefined index: to BlueSpice BSApiTasksTestBase causes Undefined index: .Jul 17 2018, 7:50 AM
hashar updated the task description. (Show Details)

Looking at BSApiTaskRSSFeeder.php it has:

public function needsToken() {
    return false;
}

The test case tests/phpunit/BSApiTasksRSSFeederTest.php invokes BSApiTasksTestBase->executeTask() which does a request with a token:

protected function executeTask( $taskName, $taskData ) {
    $results = $this->doApiRequestWithToken([
        'action' => $this->getModuleName(),
        'task' => $taskName,
        'taskData' => \FormatJson::encode( $taskData )
    ]);

    return (object)$results[0];
}

That then invokes ApiTestCase->doApiRequest() with $tokenType = 'auto', but the API module says it does not need a token and doApiRequest mismanage that corner case. Summarized method:

protected function doApiRequest(...) {
        ...
        if ( $tokenType !== null ) {   // 'auto'
            if ( $tokenType === 'auto' ) {  // true
                $tokenType = ( new ApiMain() )->getModuleManager()
                    ->getModule( $params['action'], 'action' )->needsToken();  // false
            }
            $params['token'] = ApiQueryTokens::getToken(
                $wgUser, $sessionObj, ApiQueryTokens::getTokenTypeSalts()[$tokenType]
            )->toString();
        }

}

$tokenType is false, ApiQueryTokens::getTokenTypeSalts() returns an array such as:

[
    "createaccount" => [ "", "createaccount", ],
    "csrf" => "",
    "login" => [ "", "login", ],
    "patrol" => "patrol",
    "rollback" => "rollback",
    "userrights" => "userrights",
    "watch" => "watch",
  ]

There is obviously no entry for a (bool)False token type hence the undefined index.

Either the affected modules require a token, and their needsToken should be adjusted. Or they indeed do not need a token and BlueSpiceFoundation would need to switch between doApiRequestWithToken() and doApiRequest() depending on needsToken().

Thank you very much for this deep analysis. In general all out "TaskAPI"s require tokens. The "RSS Feeder" extension is a special case (for "Dashboards" I actually don't know). I will either change the affected tests (as they are special cases) or implement a dynamic switch in the test base class as you suggtest.