'use strict';

// #############################################################################
// Edit page content

var globals = require('./settings/globals');
var messages = require('./settings/messages').page.editContent;
var randomString = require('./helpers/randomString').randomString;
// random text string for filtering and content purposes
var randomText = randomString({ length: 50, withWhitespaces: false });
var casperjs = require('casper');
var cms = require('./helpers/cms')(casperjs);
var xPath = casperjs.selectXPath;

casper.test.setUp(function (done) {
    casper.start()
        .then(cms.login())
        .then(cms.addPage({ title: 'First page' }))
        .then(cms.addPlugin({
            type: 'TextPlugin',
            content: {
                id_body: 'Random text'
            }
        }))
        .run(done);
});

casper.test.tearDown(function (done) {
    casper.start()
        .then(cms.removePage())
        .then(cms.logout())
        .run(done);
});

casper.test.begin('Edit content', function (test) {
    var previousContentText;

    casper
        .start(globals.editUrl)
        // make sure we are in content mode
        .waitUntilVisible('.cms-toolbar-expanded', function () {
            this.click('.cms-toolbar-item-cms-mode-switcher .cms-btn[href="?edit"]');
        })
        // check edit modal window appearance after double click in content mode
        // double click on last added plugin content
        .waitUntilVisible('.cms-toolbar-expanded', function () {
            this.mouse.doubleclick(
                // pick a div with class cms-plugin that has a p that has text "Random text"
                xPath('//div[contains(@class, "cms-plugin ")][.//p[text()[contains(.,"Random text")]]][last()]')
            );
        })
        .waitUntilVisible('.cms-modal-open')
        // change content inside appeared modal window
        .withFrame(0, function () {
            casper
                .waitUntilVisible('#text_form', function () {
                    // explicitly put text to ckeditor
                    previousContentText = this.evaluate(function (contentData) {
                        var previousContent = CMS.CKEditor.editor.document.getBody().getText();

                        CMS.CKEditor.editor.setData(contentData);
                        return previousContent;
                    }, randomText);
                });
        })
        // submit changes in modal
        .then(function () {
            this.click('.cms-modal-buttons .cms-btn-action.default');
        })
        // check if content updated
        .waitWhileVisible('.cms-modal-open', function () {
            // ensure content updated with new one
            test.assertSelectorHasText(
                '.cms-plugin:last-child',
                randomText,
                messages.contentUpdatedDoubleClick
            );
        })
        // go to the Structure mode
        .then(function () {
            this.click('.cms-toolbar-item-cms-mode-switcher .cms-btn[href="?build"]');
        })

        // check edit modal window appearance after Edit button click in structure mode
        // click to Edit button for last plugin
        .waitUntilVisible('.cms-structure', function () {
            this.click('.cms-draggable:last-child .cms-submenu-edit');
        })
        // check if edit modal window appeared
        .waitUntilVisible('.cms-modal-open', function () {
            test.assertVisible('.cms-modal-open', messages.modalAppearsAfterEditButton);
        })
        // close edit modal
        .then(function () {
            this.click('.cms-modal-open .cms-modal-item-buttons:last-child > a');
        })
        .waitWhileVisible('.cms-modal-iframe')

        // check edit modal window appearance after double click in structure mode
        // double click on last plugin
        .then(function () {
            this.mouse.doubleclick('.cms-draggable:last-child .cms-dragitem');
        })
        // edit content inside opened editor modal
        .waitUntilVisible('.cms-modal-open')
        .withFrame(0, function () {
            casper
                .waitUntilVisible('#text_form', function () {
                    // explicitly put text to ckeditor
                    this.evaluate(function (contentData) {
                        CMS.CKEditor.editor.setData(contentData);
                    }, previousContentText);
                });
        })
        // submit changes
        .then(function () {
            this.click('.cms-modal-buttons .cms-btn-action.default');
        })
        // go to the Content mode
        .waitWhileVisible('.cms-modal-open', function () {
            this.click('.cms-toolbar-item-cms-mode-switcher .cms-btn[href="?edit"]');
        })
        // check for applied changes
        .waitUntilVisible('.cms-toolbar-expanded', function () {
            // ensure content updated with new one
            test.assertSelectorHasText(
                '.cms-plugin:last-child',
                previousContentText,
                messages.contentUpdatedDoubleClickStructure
            );
        })
        .run(function () {
            test.done();
        });
});

@@ -1934,33 +1934,16 @@
   casper
-%0A                
 .waitUnt
@@ -1988,36 +1988,32 @@
                 
-    
 // explicitly pu
@@ -2039,36 +2039,32 @@
                 
-  
-  
 previousContentT
@@ -2100,36 +2100,32 @@
 (contentData) %7B%0A
-    
                 
@@ -2213,36 +2213,32 @@
                 
-    
 CMS.CKEditor.edi
@@ -2283,20 +2283,16 @@
         
- 
-   
 return p
@@ -2323,20 +2323,16 @@
         
-    
 %7D, rando
@@ -2335,28 +2335,24 @@
 andomText);%0A
-    
             
@@ -4075,33 +4075,16 @@
   casper
-%0A                
 .waitUnt
@@ -4129,36 +4129,32 @@
                 
-  
-  
 // explicitly pu
@@ -4164,36 +4164,32 @@
 ext to ckeditor%0A
-    
                 
@@ -4239,36 +4239,32 @@
                 
-   
- 
 CMS.CKEditor.edi
@@ -4305,20 +4305,16 @@
         
-    
 %7D, previ
@@ -4326,28 +4326,24 @@
 ntentText);%0A
-    
             

revert indent [ci skip]