Skip to content

Commit

Permalink
Add anon edit warning link parameters
Browse files Browse the repository at this point in the history
Bug: T330550
  • Loading branch information
micgro42 committed Sep 13, 2023
1 parent d1d0044 commit e6a014c
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/components/AnonymousEditWarning.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ import WarningMessage from '@/components/WarningMessage.vue';
import { useConfig } from '@/plugins/ConfigPlugin/Config';
import { useMessages } from '@/plugins/MessagesPlugin/Messages';
import { computed } from 'vue';
import { useAccountLinker } from '@/plugins/AccountLinkerPlugin/AccountLinker';
const accountLinker = useAccountLinker();
const messages = useMessages();
const warning = computed(
() => messages.get( 'wikibase-anonymouseditwarning' ) );
() => messages.get(
'wikibase-anonymouseditwarning',
accountLinker.getLoginLink(),
accountLinker.getCreateAccountLink(),
) );
const config = useConfig();
</script>
Expand Down
6 changes: 6 additions & 0 deletions src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ComponentPublicInstance } from 'vue';
import MediaWikiRouter from './plugins/WikiRouterPlugin/MediaWikiRouter';
import MwApiLangCodeRetriever from './data-access/MwApiLangCodeRetriever';
import LanguageItemSearcher from '@/data-access/LanguageItemSearcher';
import MediaWikiAccountLinker from '@/plugins/AccountLinkerPlugin/MediaWikiAccountLinker';

interface InitConfig extends CreateAndMountConfig {
tags: string[];
Expand Down Expand Up @@ -39,6 +40,10 @@ export default function init( config: InitConfig, mw: MediaWiki ): ComponentPubl
mw.util.getUrl,
( mw.config.get( 'wgNamespaceIds' ) as Record<string, number> ).lexeme,
);
const accountLinker = new MediaWikiAccountLinker(
mw.util.getUrl,
mw.config.get( 'wgPageName' ) as string,
);
const tracker = new MwTracker( mw.track );
const wikiRouter = new MediaWikiRouter( mw.util.getUrl );

Expand All @@ -49,6 +54,7 @@ export default function init( config: InitConfig, mw: MediaWiki ): ComponentPubl
messagesRepository,
lexemeCreator,
searchLinker,
accountLinker,
tracker,
wikiRouter,
};
Expand Down
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import LexemeCreator from '@/data-access/LexemeCreator';
import { ItemSearchKey } from '@/plugins/ItemSearchPlugin/ItemSearch';
import { Config, ConfigKey } from '@/plugins/ConfigPlugin/Config';
import SearchLinker, { SearchLinkerKey } from '@/plugins/SearchLinkerPlugin/SearchLinker';
import AccountLinker, { AccountLinkerKey } from '@/plugins/AccountLinkerPlugin/AccountLinker';
import Tracker from '@/data-access/tracking/Tracker';
import {
HANDLE_INIT_PARAMS,
Expand Down Expand Up @@ -35,6 +36,7 @@ export interface Services {
langCodeRetriever: LangCodeRetriever;
lexemeCreator: LexemeCreator;
searchLinker: SearchLinker;
accountLinker: AccountLinker;
tracker: Tracker;
wikiRouter: WikiRouter;
}
Expand All @@ -59,6 +61,7 @@ export default function createAndMount(
app.provide( ItemSearchKey, services.itemSearcher );
app.provide( LanguageItemSearchKey, services.languageItemSearcher );
app.provide( SearchLinkerKey, services.searchLinker );
app.provide( AccountLinkerKey, services.accountLinker );
app.provide( WikiRouterKey, services.wikiRouter );
app.provide(
LanguageCodesProviderKey,
Expand Down
21 changes: 21 additions & 0 deletions src/plugins/AccountLinkerPlugin/AccountLinker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { inject, InjectionKey } from 'vue';

export default interface AccountLinker {
/**
* FIXME
*/
getLoginLink(): string;

/**
* FIXME
*/
getCreateAccountLink(): string;
}

export const AccountLinkerKey: InjectionKey<AccountLinker> = Symbol( 'AccountLinker' );

export function useAccountLinker(): AccountLinker {
return inject( AccountLinkerKey, () => {
throw new Error( 'No AccountLinker provided!' );
}, true );
}
19 changes: 19 additions & 0 deletions src/plugins/AccountLinkerPlugin/MediaWikiAccountLinker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { MwUtilGetUrl } from '@/@types/mediawiki';
import AccountLinker from '@/plugins/AccountLinkerPlugin/AccountLinker';

export default class MediaWikiAccountLinker implements AccountLinker {

public constructor(
private readonly getUrl: MwUtilGetUrl,
private readonly currentPage: string,
) {}

public getCreateAccountLink(): string {
return this.getUrl( 'Special:CreateAccount', { returnto: this.currentPage } );
}

public getLoginLink(): string {
return this.getUrl( 'Special:UserLogin', { returnto: this.currentPage } );
}

}
2 changes: 2 additions & 0 deletions tests/integration/App.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import DevItemSearcher from '@/data-access/DevItemSearcher';
import DevMessagesRepository from '@/plugins/MessagesPlugin/DevMessagesRepository';
import MediaWikiSearchLinker from '@/plugins/SearchLinkerPlugin/MediaWikiSearchLinker';
import { LanguageItemSearchKey } from '@/plugins/ItemSearchPlugin/LanguageItemSearch';
import { AccountLinkerKey } from '@/plugins/AccountLinkerPlugin/AccountLinker';

describe( 'App.vue', () => {

Expand Down Expand Up @@ -64,6 +65,7 @@ describe( 'App.vue', () => {
mwUtilGetUrl,
lexemeNS,
),
[ AccountLinkerKey as symbol ]: null,
[ WikiRouterKey as symbol ]: null,
[ MessagesKey as symbol ]: new Messages( new DevMessagesRepository() ),
[ ItemSearchKey as symbol ]: new DevItemSearcher(),
Expand Down
19 changes: 17 additions & 2 deletions tests/unit/components/AnonymousEditWarning.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,52 @@ import AnonymousEditWarning from '@/components/AnonymousEditWarning.vue';
import { ConfigKey } from '@/plugins/ConfigPlugin/Config';
import { MessagesKey } from '@/plugins/MessagesPlugin/Messages';
import { mount } from '@vue/test-utils';
import { AccountLinkerKey } from '@/plugins/AccountLinkerPlugin/AccountLinker';

describe( 'AnonymousEditWarning', () => {

it( 'does nothing if not anonymous', () => {
const messages = { get: jest.fn() };
const accountLinker = {
getLoginLink: jest.fn(),
getCreateAccountLink: jest.fn(),
};
const warning = mount( AnonymousEditWarning, {
global: {
provide: {
[ ConfigKey as symbol ]: { isAnonymous: false },
[ MessagesKey as symbol ]: messages,
[ AccountLinkerKey as symbol ]: accountLinker,
},
},
} );

expect( warning.isVisible() ).toBe( false );
expect( messages.get ).not.toHaveBeenCalled();
expect( accountLinker.getLoginLink ).not.toHaveBeenCalled();
expect( accountLinker.getCreateAccountLink ).not.toHaveBeenCalled();
} );

it( 'matches snapshot if anonymous', () => {
const messages = { get: jest.fn().mockImplementation( ( key ) => key ) };
const messages = { get: jest.fn().mockImplementation( ( ...params ) =>
`(${params.join( ', ' )})`,
) };
const accountLinker = {
getLoginLink: jest.fn().mockReturnValue( 'loginLink' ),
getCreateAccountLink: jest.fn().mockReturnValue( 'createAccountLink' ),
};
const warning = mount( AnonymousEditWarning, {
global: {
provide: {
[ ConfigKey as symbol ]: { isAnonymous: true },
[ MessagesKey as symbol ]: messages,
[ AccountLinkerKey as symbol ]: accountLinker,
},
},
} );

expect( warning.html() ).toMatchSnapshot();
expect( messages.get ).toHaveBeenCalledWith( 'wikibase-anonymouseditwarning' );
expect( messages.get ).toHaveBeenCalledWith( 'wikibase-anonymouseditwarning', 'loginLink', 'createAccountLink' );
} );

} );
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AnonymousEditWarning matches snapshot if anonymous 1`] = `"<div class="wikit wikit-Message wikit-Message--warning wbl-snl-anonymous-edit-warning"><span class="wikit-Message__content"><span class="wikit wikit-Icon wikit-Icon--large wikit-Icon--warning wikit-Message__icon"><svg class="wikit-Icon__svg" xmlns="http://www.w3.org/2000/svg" fill="none" aria-hidden="true" focusable="false" viewBox="0 0 16 16"><path fill="currentColor" d="M9.163 1.68234C9.06078 1.4381 8.89901 1.22746 8.69449 1.07231C8.48997 0.917151 8.25017 0.823144 7.99999 0.800049C7.75116 0.82453 7.51294 0.919178 7.30987 1.07425C7.10679 1.22933 6.94619 1.43922 6.84459 1.68234L0.672272 13.0631C0.0337565 14.2368 0.558251 15.2 1.82768 15.2H14.1723C15.4417 15.2 15.9662 14.2368 15.3277 13.0631L9.163 1.68234ZM8.76013 12.7717H7.23986V11.1528H8.76013V12.7717ZM8.76013 9.53394H7.23986V4.67728H8.76013V9.53394Z"></path></svg></span><span><!-- eslint-disable-next-line vue/no-v-html --><span>wikibase-anonymouseditwarning</span></span></span></div>"`;
exports[`AnonymousEditWarning matches snapshot if anonymous 1`] = `"<div class="wikit wikit-Message wikit-Message--warning wbl-snl-anonymous-edit-warning"><span class="wikit-Message__content"><span class="wikit wikit-Icon wikit-Icon--large wikit-Icon--warning wikit-Message__icon"><svg class="wikit-Icon__svg" xmlns="http://www.w3.org/2000/svg" fill="none" aria-hidden="true" focusable="false" viewBox="0 0 16 16"><path fill="currentColor" d="M9.163 1.68234C9.06078 1.4381 8.89901 1.22746 8.69449 1.07231C8.48997 0.917151 8.25017 0.823144 7.99999 0.800049C7.75116 0.82453 7.51294 0.919178 7.30987 1.07425C7.10679 1.22933 6.94619 1.43922 6.84459 1.68234L0.672272 13.0631C0.0337565 14.2368 0.558251 15.2 1.82768 15.2H14.1723C15.4417 15.2 15.9662 14.2368 15.3277 13.0631L9.163 1.68234ZM8.76013 12.7717H7.23986V11.1528H8.76013V12.7717ZM8.76013 9.53394H7.23986V4.67728H8.76013V9.53394Z"></path></svg></span><span><!-- eslint-disable-next-line vue/no-v-html --><span>(wikibase-anonymouseditwarning, loginLink, createAccountLink)</span></span></span></div>"`;
4 changes: 4 additions & 0 deletions tests/unit/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ describe( 'createAndMount', () => {
searchLinker: {
getSearchUrlForLexeme: jest.fn().mockReturnValue( 'https://example.com' ),
},
accountLinker: {
getLoginLink: jest.fn(),
getCreateAccountLink: jest.fn(),
},
tracker: unusedTracker,
wikiRouter: unusedWikiRouter,
langCodeRetriever: unusedLangCodeRetriever,
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/plugins/MediaWikiAccountLinker.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import MediaWikiAccountLinker from '@/plugins/AccountLinkerPlugin/MediaWikiAccountLinker';

describe( 'MediaWikiAccountLinker', () => {
it( 'delegates to mw.util.getUrl for getting the login url', () => {
const url = '/w/index.php?title=Special:UserLogin&returnto=Special%3ANewLexeme';
const getUrlMock = jest.fn().mockReturnValue( url );
const accountLinker = new MediaWikiAccountLinker( getUrlMock, 'Special:NewLexeme' );

expect( accountLinker.getLoginLink() ).toBe( url );
expect( getUrlMock ).toHaveBeenCalledWith( 'Special:UserLogin', {
returnto: 'Special:NewLexeme',
} );
} );

it( 'delegates to mw.util.getUrl for getting the create account url', () => {
const url = '/w/index.php?title=Special:CreateAccount&returnto=Special%3ANewLexeme';
const getUrlMock = jest.fn().mockReturnValue( url );
const accountLinker = new MediaWikiAccountLinker( getUrlMock, 'Special:NewLexeme' );

expect( accountLinker.getCreateAccountLink() ).toBe( url );
expect( getUrlMock ).toHaveBeenCalledWith( 'Special:CreateAccount', {
returnto: 'Special:NewLexeme',
} );
} );
} );

0 comments on commit e6a014c

Please sign in to comment.