Re: [Wikitech-l] Setting logged in user context on unit tests

2017-12-27 Thread Tony Thomas
Wow.


$page = WikiPage::factory( $title );
$page->doEditContent( ContentHandler::makeContent( $text, $title ),
$comment, 0, false, $user );

Thank you Addshore, and I say - close enough :P

--
Tony Thomas
https://mediawiki.org/wiki/User:01tonythomas
--

On Wed, Dec 27, 2017 at 1:59 PM, Addshore  wrote:

> Take a look at MediaWikitestCase::insertPage
>
> https://phabricator.wikimedia.org/source/mediawiki/browse/
> master/tests/phpunit/MediaWikiTestCase.php;8eaee6fd06d9089ef90032530af9a9
> d25a52a1fc$997
>
> On 26 December 2017 at 09:58, Tony Thomas <01tonytho...@gmail.com> wrote:
>
> > Great. This would really help. I just posted the same in our tasks (which
> > are GCI tasks now) so that people would use it. One more thing, in a unit
> > test - is it the only way to create a Wikipage and save it to the db ?
> >
> > $title = Title::newFromText( 'TestPage' );
> > $wikiPage = WikiPage::factory( $title );
> > $content = new WikitextContent( $text='this is a test' );
> > $wikiPage->doEditContent( $content, $summary='Test commit' );
> >
> > or are there some other simpler ways ?
> >
> >
> > --
> > Tony Thomas
> > https://mediawiki.org/wiki/User:01tonythomas
> > --
> >
> > On Tue, Dec 26, 2017 at 1:27 AM, Gergo Tisza 
> wrote:
> >
> > > On Mon, Dec 25, 2017 at 12:00 PM, Tony Thomas <01tonytho...@gmail.com>
> > > wrote:
> > >
> > > > Came across a situation similar to [1] where I had to call submit()
> > > > function of a FormSpecialPage during a unit test. We have something
> > going
> > > > on in the background, and $this->getUser() needs to return a valid
> > user.
> > > >
> > > > Is there a way to mimic this context inside the unit test, so that I
> > can
> > > > manually set a $user in the unit test, and this would be used while
> > > > performing inner operations ?
> > > >
> > >
> > > No need to mimic, you can just inject it (as long as the special page
> > > correctly uses $this->getUser() & co instead of using globals, which is
> > > usually not a problem with special pages) :
> > >
> > > $specialPage = $this->newSpecialPage();
> > > $context = new DerivativeContext( RequestContext::getMain() );
> > > $context->setUser( $user );
> > > $context->setRequest( ... );
> > > $specialPage->setContext( $context );
> > > $res = $specialPage->onSubmit( $input );
> > > ___
> > > Wikitech-l mailing list
> > > Wikitech-l@lists.wikimedia.org
> > > https://lists.wikimedia.org/mailman/listinfo/wikitech-l
> > >
> > ___
> > Wikitech-l mailing list
> > Wikitech-l@lists.wikimedia.org
> > https://lists.wikimedia.org/mailman/listinfo/wikitech-l
> >
> ___
> Wikitech-l mailing list
> Wikitech-l@lists.wikimedia.org
> https://lists.wikimedia.org/mailman/listinfo/wikitech-l
>
___
Wikitech-l mailing list
Wikitech-l@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/wikitech-l

Re: [Wikitech-l] Setting logged in user context on unit tests

2017-12-27 Thread Addshore
Take a look at MediaWikitestCase::insertPage

https://phabricator.wikimedia.org/source/mediawiki/browse/master/tests/phpunit/MediaWikiTestCase.php;8eaee6fd06d9089ef90032530af9a9d25a52a1fc$997

On 26 December 2017 at 09:58, Tony Thomas <01tonytho...@gmail.com> wrote:

> Great. This would really help. I just posted the same in our tasks (which
> are GCI tasks now) so that people would use it. One more thing, in a unit
> test - is it the only way to create a Wikipage and save it to the db ?
>
> $title = Title::newFromText( 'TestPage' );
> $wikiPage = WikiPage::factory( $title );
> $content = new WikitextContent( $text='this is a test' );
> $wikiPage->doEditContent( $content, $summary='Test commit' );
>
> or are there some other simpler ways ?
>
>
> --
> Tony Thomas
> https://mediawiki.org/wiki/User:01tonythomas
> --
>
> On Tue, Dec 26, 2017 at 1:27 AM, Gergo Tisza  wrote:
>
> > On Mon, Dec 25, 2017 at 12:00 PM, Tony Thomas <01tonytho...@gmail.com>
> > wrote:
> >
> > > Came across a situation similar to [1] where I had to call submit()
> > > function of a FormSpecialPage during a unit test. We have something
> going
> > > on in the background, and $this->getUser() needs to return a valid
> user.
> > >
> > > Is there a way to mimic this context inside the unit test, so that I
> can
> > > manually set a $user in the unit test, and this would be used while
> > > performing inner operations ?
> > >
> >
> > No need to mimic, you can just inject it (as long as the special page
> > correctly uses $this->getUser() & co instead of using globals, which is
> > usually not a problem with special pages) :
> >
> > $specialPage = $this->newSpecialPage();
> > $context = new DerivativeContext( RequestContext::getMain() );
> > $context->setUser( $user );
> > $context->setRequest( ... );
> > $specialPage->setContext( $context );
> > $res = $specialPage->onSubmit( $input );
> > ___
> > Wikitech-l mailing list
> > Wikitech-l@lists.wikimedia.org
> > https://lists.wikimedia.org/mailman/listinfo/wikitech-l
> >
> ___
> Wikitech-l mailing list
> Wikitech-l@lists.wikimedia.org
> https://lists.wikimedia.org/mailman/listinfo/wikitech-l
>
___
Wikitech-l mailing list
Wikitech-l@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/wikitech-l

Re: [Wikitech-l] Setting logged in user context on unit tests

2017-12-26 Thread Tony Thomas
Great. This would really help. I just posted the same in our tasks (which
are GCI tasks now) so that people would use it. One more thing, in a unit
test - is it the only way to create a Wikipage and save it to the db ?

$title = Title::newFromText( 'TestPage' );
$wikiPage = WikiPage::factory( $title );
$content = new WikitextContent( $text='this is a test' );
$wikiPage->doEditContent( $content, $summary='Test commit' );

or are there some other simpler ways ?


--
Tony Thomas
https://mediawiki.org/wiki/User:01tonythomas
--

On Tue, Dec 26, 2017 at 1:27 AM, Gergo Tisza  wrote:

> On Mon, Dec 25, 2017 at 12:00 PM, Tony Thomas <01tonytho...@gmail.com>
> wrote:
>
> > Came across a situation similar to [1] where I had to call submit()
> > function of a FormSpecialPage during a unit test. We have something going
> > on in the background, and $this->getUser() needs to return a valid user.
> >
> > Is there a way to mimic this context inside the unit test, so that I can
> > manually set a $user in the unit test, and this would be used while
> > performing inner operations ?
> >
>
> No need to mimic, you can just inject it (as long as the special page
> correctly uses $this->getUser() & co instead of using globals, which is
> usually not a problem with special pages) :
>
> $specialPage = $this->newSpecialPage();
> $context = new DerivativeContext( RequestContext::getMain() );
> $context->setUser( $user );
> $context->setRequest( ... );
> $specialPage->setContext( $context );
> $res = $specialPage->onSubmit( $input );
> ___
> Wikitech-l mailing list
> Wikitech-l@lists.wikimedia.org
> https://lists.wikimedia.org/mailman/listinfo/wikitech-l
>
___
Wikitech-l mailing list
Wikitech-l@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/wikitech-l

Re: [Wikitech-l] Setting logged in user context on unit tests

2017-12-25 Thread Gergo Tisza
On Mon, Dec 25, 2017 at 12:00 PM, Tony Thomas <01tonytho...@gmail.com>
wrote:

> Came across a situation similar to [1] where I had to call submit()
> function of a FormSpecialPage during a unit test. We have something going
> on in the background, and $this->getUser() needs to return a valid user.
>
> Is there a way to mimic this context inside the unit test, so that I can
> manually set a $user in the unit test, and this would be used while
> performing inner operations ?
>

No need to mimic, you can just inject it (as long as the special page
correctly uses $this->getUser() & co instead of using globals, which is
usually not a problem with special pages) :

$specialPage = $this->newSpecialPage();
$context = new DerivativeContext( RequestContext::getMain() );
$context->setUser( $user );
$context->setRequest( ... );
$specialPage->setContext( $context );
$res = $specialPage->onSubmit( $input );
___
Wikitech-l mailing list
Wikitech-l@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/wikitech-l

Re: [Wikitech-l] Setting logged in user context on unit tests

2017-12-25 Thread Amir Ladsgroup
I recently stumbled across a great test suite called
ApiQueryWatchlistIntegrationTest
https://github.com/wikimedia/mediawiki/blob/master/tests/phpunit/includes/api/ApiQueryWatchlistIntegrationTest.php

I think that answers your question. Feel free to ask question if it's
unclear.

Best

On Mon, Dec 25, 2017 at 9:00 PM Tony Thomas <01tonytho...@gmail.com> wrote:

> Dear Team,
>
> Came across a situation similar to [1] where I had to call submit()
> function of a FormSpecialPage during a unit test. We have something going
> on in the background, and $this->getUser() needs to return a valid user.
>
> Is there a way to mimic this context inside the unit test, so that I can
> manually set a $user in the unit test, and this would be used while
> performing inner operations ?
>
>
> [1]
>
> https://gerrit.wikimedia.org/r/#/c/35/4/tests/specials/SpecialNewsletterCreateTest.php
>
>
> --
> Tony Thomas
> https://mediawiki.org/wiki/User:01tonythomas
> --
> ___
> Wikitech-l mailing list
> Wikitech-l@lists.wikimedia.org
> https://lists.wikimedia.org/mailman/listinfo/wikitech-l
___
Wikitech-l mailing list
Wikitech-l@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/wikitech-l

[Wikitech-l] Setting logged in user context on unit tests

2017-12-25 Thread Tony Thomas
Dear Team,

Came across a situation similar to [1] where I had to call submit()
function of a FormSpecialPage during a unit test. We have something going
on in the background, and $this->getUser() needs to return a valid user.

Is there a way to mimic this context inside the unit test, so that I can
manually set a $user in the unit test, and this would be used while
performing inner operations ?


[1]
https://gerrit.wikimedia.org/r/#/c/35/4/tests/specials/SpecialNewsletterCreateTest.php


--
Tony Thomas
https://mediawiki.org/wiki/User:01tonythomas
--
___
Wikitech-l mailing list
Wikitech-l@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/wikitech-l