Skip to content
This repository has been archived by the owner on Jun 30, 2024. It is now read-only.

Commit

Permalink
Update extension test workflow on REL1_39 branch (#386) (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
lens0021 authored Feb 19, 2023
1 parent 4f41d8b commit 6638b6a
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 5 deletions.
3 changes: 2 additions & 1 deletion extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@
},
"RecursiveCategory": {
"class": "MediaWiki\\Extension\\FacetedCategory\\Hooks\\RecursiveCategory",
"services": []
"services": ["DBLoadBalancer", "JobQueueGroup"]
}
},
"Hooks": {
"BeforePageDisplay": "Main",
"CategoryAfterPageAdded": "RecursiveCategory",
"ContentAlterParserOutput": "RecursiveCategory",
"MakeGlobalVariablesScript": "RecursiveCategory",
"OutputPageParserOutput": "RecursiveCategory",
Expand Down
45 changes: 43 additions & 2 deletions includes/Hooks/RecursiveCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,59 @@
namespace MediaWiki\Extension\FacetedCategory\Hooks;

use Category;
use JobQueueGroup;
use RefreshLinksJob;
use Title;
use Wikimedia\Rdbms\ILoadBalancer;

class RecursiveCategory implements
\MediaWiki\Page\Hook\CategoryAfterPageAddedHook,
\MediaWiki\Content\Hook\ContentAlterParserOutputHook,
\MediaWiki\Hook\OutputPageParserOutputHook,
\MediaWiki\Hook\MakeGlobalVariablesScriptHook
{

/** @const string direct-categories key. */
public const DIRECT_CATEGORIES_PROPERTY_NAME = 'direct-categories';

/** @var ILoadBalancer */
private $loadBalancer;

/** @var JobQueueGroup */
private $jobQueueGroup;

/**
* direct-categories key.
* @param ILoadBalancer $loadBalancer
* @param JobQueueGroup $jobQueueGroup
*/
public const DIRECT_CATEGORIES_PROPERTY_NAME = 'direct-categories';
public function __construct( ILoadBalancer $loadBalancer, JobQueueGroup $jobQueueGroup ) {
$this->loadBalancer = $loadBalancer;
$this->jobQueueGroup = $jobQueueGroup;
}

/**
* @inheritDoc
*/
public function onCategoryAfterPageAdded( $category, $wikiPage ) {
// return true;
$title = $wikiPage->getTitle();
if ( $title->getNamespace() !== NS_CATEGORY || !str_contains( $title->getText(), '/' ) ) {
return true;
}

$dbr = $this->loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA );
$pages = $dbr->selectFieldValues(
'categorylinks',
'cl_from',
[ 'cl_to' => $title->getDBKey() ],
__METHOD__
);
foreach ( $pages as $id ) {
$title = Title::newFromId( $id );
$job = new RefreshLinksJob( $title, [ 'parseThreshold' => 0 ] );
$this->jobQueueGroup->push( $job );
}
}

/**
* When adding categories to a page, include the parent of the category also.
Expand Down
86 changes: 86 additions & 0 deletions tests/phpunit/integration/Hooks/RecursiveCategoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace MediaWiki\Extension\FacetedCategory\Tests\Integration\Hooks;

use CommentStoreComment;
use MediaWikiIntegrationTestCase;
use Title;
use WikitextContent;

/**
* @group Database
* @covers MediaWiki\Extension\FacetedCategory\Hooks\RecursiveCategory
*/
class RecursiveCategoryTest extends MediaWikiIntegrationTestCase {
protected function assertCategory( $title, $expected, string $message = '' ) {
$actual = $this->db->selectFieldValues(
'categorylinks',
'cl_to',
[
'cl_from' => $title->getId(),
],
);
$this->assertEqualsCanonicalizing( $expected, $actual, $message );
}

public function testCategorizedUsingParent() {
$parent = $this->createTitle( 'Category:Facet/Parent1', '[[Category:Facet/Cat1]][[Category:Facet/Cat2]]' );
$child = $this->createTitle( 'Child1', '[[Category:Facet/Parent1]]' );
$this->assertCategory( $child, [
'Facet/Cat1',
'Facet/Cat2',
'Facet/Parent1',
],
'Should be registered to the specified category including the grandparent categories.'
);
}

public function testLateAddingCategoryToParent() {
$parentText = 'Facet/Parent-' . mt_rand();
$child = $this->createTitle( 'Child-' . mt_rand(), "[[Category:$parentText]]" );
$parent = $this->createTitle( "Category:$parentText", '[[Category:Facet/Cat1]]' );
$this->assertCategory( $child, [ $parentText ] );

$this->runJobs();

$this->assertCategory( $child, [
'Facet/Cat1',
$parentText,
],
'Should be registered to the specified category including the grandparent categories'
);
}

public function testGrandParentCategoryChange() {
$suffix = mt_rand();
$child = $this->createTitle( "Child-$suffix", "[[Category:Facet/Parent-$suffix]]" );
$parent = $this->createTitle( "Category:Facet/Parent-$suffix", "[[Category:Facet/GrandParent-$suffix]]" );
$grandParent = $this->createTitle( "Category:Facet/GrandParent-$suffix", "[[Category:Facet/Cat1-$suffix]]" );

$this->runJobs();

$this->assertCategory( $child, [
"Facet/Cat1-$suffix",
"Facet/Parent-$suffix",
"Facet/GrandParent-$suffix",
] );
}

/**
* @param string $name
* @param string $content
* @return Title
*/
protected function createTitle( $name, $content ) {
$content = new WikitextContent( $content );

$title = Title::newFromText( $name );
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );

$updater = $page->newPageUpdater( $this->getTestUser()->getUser() );
$updater->setContent( 'main', $content );
$updater->saveRevision( CommentStoreComment::newUnsavedComment( 'Test' ) );

return $page->getTitle();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace MediaWiki\Extension\FacetedCategory\Tests\Units;
namespace MediaWiki\Extension\FacetedCategory\Tests\Integration;

use MediaWiki\Extension\FacetedCategory\Special\SpecialCategoryIntersectionSearch;
use ReflectionMethod;
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/unit/Hooks/MainTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace MediaWiki\Extension\FacetedCategory\Tests\Integration;
namespace MediaWiki\Extension\FacetedCategory\Tests\Integration\Hooks;

use MediaWiki\Extension\FacetedCategory\Hooks\Main;
use MediaWikiUnitTestCase;
Expand Down

0 comments on commit 6638b6a

Please sign in to comment.