Add PHPUnit tests for VectorComponentMainMenu

Adds tests for VectorComponentMainMenu in Vector skin.
The test suite includes the following validations:
- Constructor initialization with expected parameters and behaviors.
- Correct identification of 'is-pinned' state
- Inclusion of data for first portlet and rest portlets
- Handling of pin/unpin actions and language alert considerations
- Proper integration of menu elements based on user and feature flags

Change-Id: Ie408ff668893d1de2cf9dd1211a9c82c0b882f99
This commit is contained in:
Doğu Abaris 2024-02-07 14:05:13 +01:00 committed by Abaris
parent 64218333de
commit fab1b342e0

View file

@ -0,0 +1,157 @@
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @since 1.42
*/
namespace MediaWiki\Skins\Vector\Tests\Unit\Components;
use MediaWiki\Skins\Vector\Components\VectorComponent;
use MediaWiki\Skins\Vector\Components\VectorComponentMainMenu;
use MediaWiki\Skins\Vector\Constants;
use MediaWiki\Skins\Vector\FeatureManagement\FeatureManager;
use MediaWiki\User\User;
use MediaWikiUnitTestCase;
use MessageLocalizer;
use Skin;
/**
* @group Vector
* @group Components
* @coversDefaultClass \MediaWiki\Skins\Vector\Components\VectorComponentMainMenu
*/
class VectorComponentMainMenuTest extends MediaWikiUnitTestCase {
/**
* This test checks if the VectorComponentMainMenu class can be instantiated
* @covers ::__construct
*/
public function testConstruct() {
// Mock the sidebar data, number of languages, and language data
$sidebarData = [];
$numLanguages = 5;
$languageData = [];
// Mock the MessageLocalizer, User, FeatureManager, and Skin classes
$localizerMock = $this->createMock( MessageLocalizer::class );
$userMock = $this->createMock( User::class );
$featureManagerMock = $this->createMock( FeatureManager::class );
$skinMock = $this->createMock( Skin::class );
// Create a new VectorComponentMainMenu object
$mainMenu = new VectorComponentMainMenu(
$sidebarData,
true,
$numLanguages,
$languageData,
$localizerMock,
$userMock,
$featureManagerMock,
$skinMock
);
// Assert that the object is an instance of VectorComponent
$this->assertInstanceOf( VectorComponent::class, $mainMenu );
}
/**
* @return array[]
*/
public function provideMainMenuScenarios(): array {
return [
'Main Menu Pinned' => [
'sidebarData' => [
'data-portlets-first' => [],
'array-portlets-rest' => [],
],
'shouldLanguageAlertBeInSidebar' => false,
'numLanguages' => 2,
'languageData' => [],
'isPinned' => true,
],
'Main Menu Not Pinned' => [
'sidebarData' => [
'data-portlets-first' => [],
'array-portlets-rest' => [],
],
'shouldLanguageAlertBeInSidebar' => false,
'numLanguages' => 2,
'languageData' => [],
'isPinned' => false,
],
];
}
/**
* @covers ::getTemplateData
* @dataProvider provideMainMenuScenarios
*/
public function testGetTemplateData( array $sidebarData, bool $shouldLanguageAlertBeInSidebar, int $numLanguages,
array $languageData, bool $isPinned ) {
// Mock the MessageLocalizer, User, FeatureManager, and Skin classes
$localizerMock = $this->createMock( MessageLocalizer::class );
$userMock = $this->createMock( User::class );
$featureManagerMock = $this->createMock( FeatureManager::class );
// Mock the isFeatureEnabled method
$featureManagerMock->expects( $this->once() )
->method( 'isFeatureEnabled' )
->with( Constants::FEATURE_MAIN_MENU_PINNED )
->willReturn( $isPinned );
// Mock the Skin class
$skinMock = $this->createMock( Skin::class );
// Create a new VectorComponentMainMenu object
$mainMenu = new VectorComponentMainMenu(
$sidebarData,
$shouldLanguageAlertBeInSidebar,
$numLanguages,
$languageData,
$localizerMock,
$userMock,
$featureManagerMock,
$skinMock
);
// Call the getTemplateData method
$templateData = $mainMenu->getTemplateData();
// Assert main menu id and pin status
$this->assertSame( 'vector-main-menu', $templateData['id'] );
$this->assertSame( $isPinned, $templateData['is-pinned'] );
// Assert the structure and types of expected keys
$this->assertIsArray( $templateData['data-portlets-first'] );
$this->assertIsArray( $templateData['array-portlets-rest'] );
$this->assertNull( $templateData['data-main-menu-action'] );
$this->assertNull( $templateData['data-vector-language-switch-alert'] );
// Assert data-pinnable-header
$this->assertIsArray( $templateData['data-pinnable-header'] );
$this->assertIsArray( $templateData['data-languages'] );
// Assert the structure and types of expected keys
$this->assertArrayHasKey( 'data-portlets-first', $templateData );
$this->assertArrayHasKey( 'array-portlets-rest', $templateData );
$this->assertArrayHasKey( 'data-main-menu-action', $templateData );
$this->assertArrayHasKey( 'data-vector-language-switch-alert', $templateData );
$this->assertArrayHasKey( 'data-pinnable-header', $templateData );
$this->assertArrayHasKey( 'data-languages', $templateData );
}
}