Skip to content

Commit

Permalink
fix bot responding to pings on non FGTC pages
Browse files Browse the repository at this point in the history
  • Loading branch information
NovemLinguae committed Dec 30, 2021
1 parent 149726c commit f49218e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Helper.php
@@ -1,9 +1,10 @@
<?php

class Helper {
/** Grabs template Wikicode of first instance encountered of that template. Case insensitive. Returns NULL if no template found. */
/** Grabs template Wikicode of first instance encountered of that template. Case insensitive. Returns null if no template found. */
function sliceFirstTemplateFound(string $wikicode, string $templateName) {
$starting_position = strpos(strtolower($wikicode), "{{" . strtolower($templateName));
if ( $starting_position === false ) return null;
$counter = 0;
$length = strlen($wikicode);
for ( $i = $starting_position + 2; $i < $length; $i++ ) {
Expand All @@ -20,7 +21,7 @@ function sliceFirstTemplateFound(string $wikicode, string $templateName) {
}
}
}
return NULL;
return null;
}

/** Used by echoAndFlush() */
Expand Down
72 changes: 72 additions & 0 deletions tests/HelperTest.php
Expand Up @@ -7,6 +7,8 @@ function setUp(): void {
$this->h = new Helper();
}

// TODO: add @group for grouping. this is equivlent to Jest's "describe"

function test_insertCodeAtEndOfFirstTemplate_TemplateWithParameters() {
$templateNameRegEx = 'Article ?history';
$codeToInsert = '[inserted code]';
Expand Down Expand Up @@ -184,4 +186,74 @@ function test_deleteArrayValuesBeginningWith_normal() {
];
$this->assertSame($expected, $result);
}

function test_sliceFirstTemplateFound_normal() {
$wikicode =
"Test
{{Good topic box
| algo = old(120d)
| archive = Wikipedia talk:Featured and good topic candidates/%(year)d
| archiveheader = {{Automatic archive navigator}}
| minthreadstoarchive = 1
| minthreadsleft = 4
}}
{{tmbox
|text= '''Questions about a topic you are working on or about the process in general should be asked at [[Wikipedia talk:Featured and good topic questions|Featured and good topic questions]].''' This page is primarily for discussion on proposals regarding the FTC process.
}}";
$templateName = 'good topic box';
$result = $this->h->sliceFirstTemplateFound($wikicode, $templateName);
$expected =
"{{Good topic box
| algo = old(120d)
| archive = Wikipedia talk:Featured and good topic candidates/%(year)d
| archiveheader = {{Automatic archive navigator}}
| minthreadstoarchive = 1
| minthreadsleft = 4
}}";
$this->assertSame($expected, $result);
}

function test_sliceFirstTemplateFound_secondTemplate() {
$wikicode =
"Test
{{tmbox
|text= '''Questions about a topic you are working on or about the process in general should be asked at [[Wikipedia talk:Featured and good topic questions|Featured and good topic questions]].''' This page is primarily for discussion on proposals regarding the FTC process.
}}
{{Good topic box
| algo = old(120d)
| archive = Wikipedia talk:Featured and good topic candidates/%(year)d
| archiveheader = {{Automatic archive navigator}}
| minthreadstoarchive = 1
| minthreadsleft = 4
}}";
$templateName = 'good topic box';
$result = $this->h->sliceFirstTemplateFound($wikicode, $templateName);
$expected =
"{{Good topic box
| algo = old(120d)
| archive = Wikipedia talk:Featured and good topic candidates/%(year)d
| archiveheader = {{Automatic archive navigator}}
| minthreadstoarchive = 1
| minthreadsleft = 4
}}";
$this->assertSame($expected, $result);
}

function test_sliceFirstTemplateFound_templateNotFound() {
$wikicode =
"{{User:MiszaBot/config
| algo = old(120d)
| archive = Wikipedia talk:Featured and good topic candidates/%(year)d
| archiveheader = {{Automatic archive navigator}}
| minthreadstoarchive = 1
| minthreadsleft = 4
}}
{{tmbox
|text= '''Questions about a topic you are working on or about the process in general should be asked at [[Wikipedia talk:Featured and good topic questions|Featured and good topic questions]].''' This page is primarily for discussion on proposals regarding the FTC process.
}}";
$templateName = 'good topic box';
$result = $this->h->sliceFirstTemplateFound($wikicode, $templateName);
$expected = null;
$this->assertSame($expected, $result);
}
}
2 changes: 2 additions & 0 deletions tests/PromoteTest.php
Expand Up @@ -11,6 +11,8 @@ function setUp(): void {
$this->p = new Promote($eh, $h);
}

// TODO: add @group for grouping. this is equivlent to Jest's "describe"

function test_getTopicWikipediaPageTitle_dontWriteToWikipediaGoodTopics() {
$mainArticleTitle = 'TestPage';
$goodOrFeatured = 'good';
Expand Down

0 comments on commit f49218e

Please sign in to comment.