Hi, I'm trying to do a test unit for my PR but I have a problem

Hi,

I’m trying to do a test unit for my PR but I have a problem, I don’t know how to add a tag in this test

In the image below I created a pseudocode, so you can understand what I want to test through this test
image

Thanks!!!

@CatalinStratu Please don’t paste images, they take a lot of space and we cannot copy paste the content and try it or help you with modified content easily. Just paste the text.

@Test
void displayTagsWhenNoEditRightsAndTagPluginAvailableWithTags() throws Exception
{
    /* Pseudocode
    String[] mytags = {"Testtag0", "Testtag1"};
     add.tags(mytags)
     */
    TemplateManager templateManager = oldcore.getMocker().getInstance(TemplateManager.class);
    String result = templateManager.render("documentTags.vm").trim().replaceAll("\\s+", " ");

    // Verify that the generated HTML matches the expectation:
    // - The tag label is displayed
    // - No "+" link is displayed since the user doesn't have edit rights
    assertThat(result, matchesPattern("\\Q<div class=\"doc-tags \" id=\"xdocTags\"> core.tags.list.label"+
            "<span class=\"tag\"><a href=\"/xwiki/bin/view/Main/Tags?do=viewTag&amp;tag=Testtag0\">Testtag0</a></span>"
            +"<span class=\"tag\"><a href=\"/xwiki/bin/view/Main/Tags?do=viewTag&amp;tag=Testtag1\">Testtag1</a></span>"
            + " </div>"));
}

To display code please use the 3 backticks (markdown syntax)

I edited my last post

grrr… let me fix it, you could have researched the markdown syntax a bit…

You need to check documentTags.vm and see how tags are displayed to know from where they are gathered.

#macro(addTag $tag)
#if($xwiki.tag)
    #set($oldTags = $xwiki.tag.getTagsFromDocument($doc.fullName))
    #set($result = $xwiki.tag.addTagsToDocument($tag, $doc.fullName))
    #if($result == 'OK' && "$!{request.ajax}" != '')
        #set($newTags = $xwiki.tag.getTagsFromDocument($doc.fullName))
        #set($discard = $newTags.removeAll($oldTags))
        #foreach($t in $newTags)
            #if($t != '' && !$oldTags.contains($t))
                #displayTag($t)
            #end
        #end
    #elseif($result == 'NO_EFFECT')
        $response.setStatus(409)
        #set($tagErrorMessage = $services.localization.render('core.tags.add.error.alreadySet', [$tag]))
    #elseif($result == 'NOT_ALLOWED')
        $response.setStatus(403)
        #set($tagErrorMessage = $services.localization.render('core.tags.add.error.notAllowed', [$tag]))
    #elseif($result == 'FAILED')
        $response.setStatus(500)
        #set($tagErrorMessage = $services.localization.render('core.tags.add.error.failed', [$tag]))
    #end
    #if("$!{request.ajax}" != '')
        $tagErrorMessage
    #elseif("$!{request.xredirect}" != '')
        $response.sendRedirect($request.xredirect)
    #end
#else
    ## TODO
#end

This part is responsible for creating the tag, I don’t understand how I could add it to my test

So, as you can see the tags are retrieved with $xwiki.tag.getTagsFromDocument...) so you need to provide a mockito expectation for the tag plugin mock in the test.

More specifically, replace this part:

        // Enable the Tag plugin
        oldcore.getSpyXWiki().getPluginManager().addPlugin("tag", TagPlugin.class.getName(), oldcore.getXWikiContext());

with a mock or a spy instead. Since that API takes a plugin class you can’t use that. You simply need to provide an expectation for oldcore.getSpyXWiki() when get("tag") is called and return the mock/spy :slight_smile:

Or don’t mock the tag plugin and instead mock what it does to return the tags. That’s more in the spirit of the PageTest which are supposed to be integration tests.

I’ll add a test since I believe you don’t have the necessary knowledge.

What is the GSOC you’re applying for? If it’s a java project I think you’re going to lack knowledge for it.

1 Like

Test now added: [Misc] Add missing test for documentTags.vm · xwiki/xwiki-platform@a8cfdf6 · GitHub

1 Like

Okay, I want to apply for "Create a integrable solution Two-factor authentication (2FA) for XWiki " project. In case I will not be accepted this year because I do not know Java well or I will not be accepted for other reasons I will continue to do PR and I will apply for GSoC 2022 also to this project.

That’s a good attitude! :slight_smile:

Hello, I have some “empty spaces” in Java, but I try to learn as soon as possible. Can you recommend what I could learn for testing?

The standard testing books in java. JUnit 5, mockito, selenium/webdriver for UI testing, etc.

Also https://dev.xwiki.org/xwiki/bin/view/Community/Testing/

Thanks a lot for the test. I ran the test and my program was run successfully. Please check ! Thank you very much once again. A nice day!