<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.test.ui;

import java.util.List;

import org.apache.commons.lang.math.RandomUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.xwiki.test.ui.framework.AbstractAdminAuthenticatedTest;
import org.xwiki.test.ui.framework.elements.AttachmentsPane;
import org.xwiki.test.ui.framework.elements.ViewPage;
import org.xwiki.test.ui.framework.elements.editor.WikiEditPage;

/**
 * Test saving and downloading of attachments.
 *
 * A number of these tests paste code into the document and expect an output. This is wrong IMO but the way we have
 * presently to test components which require the database to function.
 * 
 * @version $Id$
 * @since 2.5M1
 */
public class AttachmentTest extends AbstractAdminAuthenticatedTest
{
    /**
     * The object used to get the test name.
     */
    @Rule
    public TestName testName = new TestName();
    
    private final String testAttachment = "SmallAttachment.txt";

    private final String testAttachment2 = "SmallAttachment2.txt";

    private final String docName = "AttachmentTest";

    private final String docFullName = "Test.AttachmentTest";

    private final String smallAttachmentString = "This is content for a very small attachment.";

    @Before
    @Override
    public void setUp()
    {
        super.setUp();
        getUtil().deletePage("Test", docName);
    }

    @Test
    public void testUploadDownloadTwoAttachments()
    {
        WikiEditPage wep = new WikiEditPage();
        wep.switchToEdit("Test", docName);
        wep.setTitle("AttachmentTest#testUploadDownloadTwoAttachments()");
        ViewPage vp = wep.clickSaveAndView();

        AttachmentsPane ap = vp.openAttachmentsDocExtraPane();
        ap.setFileToUpload(this.getClass().getResource("/" + this.testAttachment).getPath());
        ap.addAnotherFile();
        ap.setFileToUpload(this.getClass().getResource("/" + this.testAttachment2).getPath());
        ap.clickAttachFiles();

        Assert.assertEquals("1.1", ap.getLatestVersionOfAttachment(this.testAttachment));

        // This is breaking because of a bug. TODO: fix.
        //Assert.assertEquals("1.1", ap.getLatestVersionOfAttachment(this.testAttachment2));

        List<WebElement> links = ap.getAttachmentLinks();
        links.get(0).click();
        // This test does not prove that the attachments will be shown in any particular order.
        String content = getDriver().findElement(By.tagName("html")).getText();
        int firstAttachNum = 0;
        if (content.equals("This is a small attachment.")) {
            firstAttachNum = 1;
        } else {
            Assert.assertEquals("This is another small attachment.", content);
            firstAttachNum = 2;
        }

        getDriver().navigate().back();
        links.get(1).click();
        content = getDriver().findElement(By.tagName("html")).getText();

        if (firstAttachNum == 2) {
            Assert.assertEquals("This is a small attachment.", content);
        } else {
            Assert.assertEquals("This is another small attachment.", content);
        }
    }

    /**
     * @see XWIKI-5896: The image handling in the WYSIWYG-editor with GIF images is buggy.
     */
    @Test
    public void testAttachAndViewGifImage()
    {
        WikiEditPage editPage = new WikiEditPage();
        // Prepare the page to display the GIF image. We explicitly set the width to a value greater than the actual
        // image width because we want the code that resizes the image on the server side to be executed (even if the
        // image is not actually resized).
        editPage.switchToEdit(getClass().getSimpleName(), testName.getMethodName());
        editPage.setContent(String.format("[[image:image.gif||width=%s]]", (20 + RandomUtils.nextInt(200))));
        ViewPage viewPage = editPage.clickSaveAndView();
        // Attach the GIF image.
        AttachmentsPane attachmentsPane = viewPage.openAttachmentsDocExtraPane();
        attachmentsPane.setFileToUpload(getClass().getResource("/image.gif").getPath());
        attachmentsPane.clickAttachFiles();
        // clickAttachFiles should wait for the page to load but it doesn't..
        viewPage.waitUntilPageIsLoaded();
        // XWIKI-5896 shows that the file name becomes image.png
        Assert.assertTrue(attachmentsPane.attachmentExistsByFileName("image.gif"));
    }
}<commit_msg>Tests should use getUtil().createPage() when they create pages for their needs since the goal of these tests is NOT to prove that clicking Edit>Wiki and clicking the save button works... That makes these tests smaller and faster to execute.<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.test.ui;

import java.util.List;

import org.apache.commons.lang.math.RandomUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.xwiki.test.ui.framework.AbstractAdminAuthenticatedTest;
import org.xwiki.test.ui.framework.elements.AttachmentsPane;
import org.xwiki.test.ui.framework.elements.ViewPage;
import org.xwiki.test.ui.framework.elements.editor.WikiEditPage;

/**
 * Test saving and downloading of attachments.
 *
 * A number of these tests paste code into the document and expect an output. This is wrong IMO but the way we have
 * presently to test components which require the database to function.
 * 
 * @version $Id$
 * @since 2.5M1
 */
public class AttachmentTest extends AbstractAdminAuthenticatedTest
{
    /**
     * The object used to get the test name.
     */
    @Rule
    public TestName testName = new TestName();
    
    private final String testAttachment = "SmallAttachment.txt";

    private final String testAttachment2 = "SmallAttachment2.txt";

    private final String docName = "AttachmentTest";

    @Before
    @Override
    public void setUp()
    {
        super.setUp();
        getUtil().deletePage("Test", docName);
    }

    @Test
    public void testUploadDownloadTwoAttachments()
    {
        getUtil().createPage("Test", docName, "", "AttachmentTest#testUploadDownloadTwoAttachments()");
        ViewPage vp = new ViewPage();

        AttachmentsPane ap = vp.openAttachmentsDocExtraPane();
        ap.setFileToUpload(this.getClass().getResource("/" + this.testAttachment).getPath());
        ap.addAnotherFile();
        ap.setFileToUpload(this.getClass().getResource("/" + this.testAttachment2).getPath());
        ap.clickAttachFiles();

        Assert.assertEquals("1.1", ap.getLatestVersionOfAttachment(this.testAttachment));

        // This is breaking because of a bug. TODO: fix.
        //Assert.assertEquals("1.1", ap.getLatestVersionOfAttachment(this.testAttachment2));

        List<WebElement> links = ap.getAttachmentLinks();
        links.get(0).click();
        // This test does not prove that the attachments will be shown in any particular order.
        String content = getDriver().findElement(By.tagName("html")).getText();
        int firstAttachNum = 0;
        if (content.equals("This is a small attachment.")) {
            firstAttachNum = 1;
        } else {
            Assert.assertEquals("This is another small attachment.", content);
            firstAttachNum = 2;
        }

        getDriver().navigate().back();
        links.get(1).click();
        content = getDriver().findElement(By.tagName("html")).getText();

        if (firstAttachNum == 2) {
            Assert.assertEquals("This is a small attachment.", content);
        } else {
            Assert.assertEquals("This is another small attachment.", content);
        }
    }

    /**
     * See XWIKI-5896: The image handling in the WYSIWYG-editor with GIF images is buggy.
     */
    @Test
    public void testAttachAndViewGifImage()
    {
        // Prepare the page to display the GIF image. We explicitly set the width to a value greater than the actual
        // image width because we want the code that resizes the image on the server side to be executed (even if the
        // image is not actually resized).
        getUtil().createPage(getClass().getSimpleName(), testName.getMethodName(),
            String.format("[[image:image.gif||width=%s]]", (20 + RandomUtils.nextInt(200))), testName.getMethodName());
        ViewPage viewPage = new ViewPage();

        // Attach the GIF image.
        AttachmentsPane attachmentsPane = viewPage.openAttachmentsDocExtraPane();
        attachmentsPane.setFileToUpload(getClass().getResource("/image.gif").getPath());
        attachmentsPane.clickAttachFiles();
        // clickAttachFiles should wait for the page to load but it doesn't..
        viewPage.waitUntilPageIsLoaded();
        // XWIKI-5896 shows that the file name becomes image.png
        Assert.assertTrue(attachmentsPane.attachmentExistsByFileName("image.gif"));
    }
}
{'fraction_non_alphanumeric': 0.10027803521779426, 'fraction_numerical': 0.007970342910101947, 'mean_word_length': 3.216881594372802, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4745432', 'n_tokens_mistral': 2953, 'n_tokens_neox': 2898, 'n_words': 954}