<?php


class WizardTest extends WebTestCase
{

    public function testIndex()
    {
        $this->open('?r=wizard');

        $this->checkWelcomePage();  // page 1
        $this->checkGuiPage();      // page 2
        $this->checkNetworkPage();  // page 3
        $this->checkAdminPage();    // page 4
        $this->checkLicensePage();  // page 5

        // ensure local settings page is visible
        $this->waitForTextPresent(__('SYSTEM_GLOBAL_CONFIG_SECTION_TITLE_LOCAL_SETTINGS'));
    }



    public function checkWelcomePage()
    {
        // ensure first page of wizard is visible
        $this->assertTextPresent(__('WIZARD_WELCOME_HEADER_TEXT'));

        // ensure language selector is available
        $this->assertElementPresent('name=WizardForm[language]');

        // use language from dropdown
        Yii::app()->language = $this->getSelectedValue(
            "//select[@id='WizardForm_language']"
        );
        $this->clickAndWait("//button[@id='nextBtn']");
    }


    public function checkGuiPage()
    {
        // ensure second page of wizard is visible
        $this->assertTextPresent(__('WIZARD_GUI_HEADER_TEXT'));


        // check inputs
        $this->assertElementPresent('name=WizardForm[colorTheme]');

        $giuDataInputs = array(
            array(
                'selector' => 'name=WizardForm[installationName]',
                'rules' => array('required'),
                'translationKey' => 'WIZARD_INSTALLATION_NAME',
                'defaultValue' => 'TEST Installation'
            )
        );

        $this->checkInputValidation(
            $giuDataInputs,
            "//button[@id='nextBtn']",
            false // use no ajaxValidation
        );


        // set installation name
        $this->type('name=WizardForm[installationName]', 'TEST Installation');
        $this->clickAndWait("//button[@id='nextBtn']");
    }



    public function checkNetworkPage()
    {
        // ensure third page of wizard is visible
        $this->assertTextPresent(__('WIZARD_NETWORK_HEADER_TEXT'));


        $networkDataInputs = array(
            array(
                'selector' => 'name=WizardForm[ip]',
                'rules' => array('required', 'ip'),
                'translationKey' => 'WIZARD_IP',
                'defaultValue' => '192.168.24.1'
            ),
            array(
                'selector' => 'name=WizardForm[subnet]',
                'rules' => array('required', 'ip'),
                'translationKey' => 'WIZARD_SUBNET',
                'defaultValue' => '255.255.255.0'
            ),
            array(
                'selector' => 'name=WizardForm[httpProxyIp]',
                'translationKey' => 'SYSTEM_GLOBAL_CONFIG_KEY_HTTP_PROXY_IP_ADDRESS_SHORT',
            ),
            array(
                'selector' => 'name=WizardForm[httpProxyPort]',
                'translationKey' => 'SYSTEM_GLOBAL_CONFIG_KEY_HTTP_PROXY_PORT_SHORT',
            ),
            array(
                'selector' => 'name=WizardForm[httpProxyUser]',
                'translationKey' => 'SYSTEM_GLOBAL_CONFIG_KEY_HTTP_PROXY_USERNAME_SHORT',
            ),
            array(
                'selector' => 'name=WizardForm[httpProxyPassword]',
                'translationKey' => 'SYSTEM_GLOBAL_CONFIG_KEY_HTTP_PROXY_PASSWORD_SHORT',
            ),
            array(
                'selector' => 'name=WizardForm[httpsProxyIp]',
                'translationKey' => 'SYSTEM_GLOBAL_CONFIG_KEY_HTTPS_PROXY_IP_ADDRESS_SHORT',
            ),
            array(
                'selector' => 'name=WizardForm[httpsProxyPort]',
                'translationKey' => 'SYSTEM_GLOBAL_CONFIG_KEY_HTTPS_PROXY_PORT_SHORT',
            ),
            array(
                'selector' => 'name=WizardForm[httpsProxyUser]',
                'translationKey' => 'SYSTEM_GLOBAL_CONFIG_KEY_HTTPS_PROXY_USERNAME_SHORT',
            ),
            array(
                'selector' => 'name=WizardForm[httpsProxyPassword]',
                'translationKey' => 'SYSTEM_GLOBAL_CONFIG_KEY_HTTPS_PROXY_PASSWORD_SHORT',
            )
        );


        $this->checkInputValidation(
            $networkDataInputs,
            "//button[@id='nextBtn']",
            false // use no ajaxValidation
        );

        // go to next page
        $this->clickAndWait("//button[@id='nextBtn']");

    }


    public function checkAdminPage()
    {
        // ensure page 4 of wizard is visible
        $this->assertTextPresent(__('WIZARD_ADMIN_USER_HEADER_TEXT'));

        $userDataInputs = array(
            array(
                'selector' => 'name=WizardForm[userName]',
                'rules' => array('required'),
                'translationKey' => 'USERS_MODEL_NAME',
                'defaultValue' => 'admin'
            ),
            array(
                'selector' => 'name=WizardForm[userPassword]',
                'rules' => array('required'),
                'translationKey' => 'USERS_MODEL_PASSWORD',
                'defaultValue' => 'demo'
            ),
            array(
                'selector' => 'name=WizardForm[userFirstname]',
                'translationKey' => 'USERS_MODEL_FIRSTNAME',
                'defaultValue' => 'Super'
            ),
            array(
                'selector' => 'name=WizardForm[userLastname]',
                'translationKey' => 'USERS_MODEL_LASTNAME',
                'defaultValue' => 'Admin'
            ),
            array(
                'selector' => 'name=WizardForm[userExtension]',
                'translationKey' => 'USERS_MODEL_EXTENSION',
                'rules' => array(
                    'required',
                    'numerical' => array(
                        'integerOnly' => true,
                    ),
                    'length'=>array(
                        'min' => 2,
                        'max' => 10,
                    )
                ),
                'defaultValue' => '777'
            ),
            array(
                'selector' => 'name=WizardForm[userEmail]',
                'rules' => array(
                    'required',
                    'email'
                ),
                'translationKey' => 'USERS_MODEL_EMAIL',
                'defaultValue' => 'anton.pauli@aerea.de'
            ),
            array(
                'selector' => 'name=WizardForm[userPin]',
                'rules' => array(
                    'required',
                    'numerical' => array(
                        'integerOnly' => true,
                    ),
                    "length"=>array(
                        'min' => 3,
                        'max' => 10,
                    ),
                ),
                'translationKey' => 'USERS_MODEL_PIN',
                'defaultValue' => '1234'
            )
        );


        $this->checkInputValidation(
            $userDataInputs,
            "//button[@id='nextBtn']",
            false // use no ajaxValidation
        );

        // go to next page
        $this->clickAndWait("//button[@id='nextBtn']");
    }


    public function checkLicensePage()
    {
        // ensure page 5 of wizard is visible
        $this->assertTextPresent(__('WIZARD_LICENSE_ACTIVATION_HEADER_TEXT'));

        $licenseDataInputs = array(
            array(
                'selector' => 'name=WizardForm[license_code]',
                'rules' => array('required'),
                'translationKey' => 'LICENCE_MODEL_LICENSE_CODE',
                'defaultValue' => 'INVALID-LICENSE_KEY'
            ),
            array(
                'selector' => 'name=WizardForm[customer_firstname]',
                'rules' => array('required'),
                'translationKey' => 'LICENCE_MODEL_CUSTOMER_FIRSTNAME',
                'defaultValue' => 'Anton'
            ),
            array(
                'selector' => 'name=WizardForm[customer_lastname]',
                'rules' => array('required'),
                'translationKey' => 'LICENCE_MODEL_CUSTOMER_LASTNAME',
                'defaultValue' => 'Pauli'
            ),
            array(
                'selector' => 'name=WizardForm[customer_company]',
                'rules' => array('required'),
                'translationKey' => 'LICENCE_MODEL_CUSTOMER_COMPANY',
                'defaultValue' => 'Phonalisa GmbH'
            ),
            array(
                'selector' => 'name=WizardForm[customer_adr_street]',
                'rules' => array('required'),
                'translationKey' => 'LICENCE_MODEL_CUSTOMER_ADR_STREET',
                'defaultValue' => 'Hauptstr.'
            ),
            array(
                'selector' => 'name=WizardForm[customer_adr_street2]',
                'translationKey' => 'LICENCE_MODEL_CUSTOMER_ADR_STREET2',
                'defaultValue' => ''
            ),
            array(
                'selector' => 'name=WizardForm[customer_adr_street_number]',
                'rules' => array('required'),
                'translationKey' => 'LICENCE_MODEL_CUSTOMER_ADR_STREET_NUMBER',
                'defaultValue' => '26'
            ),
            array(
                'selector' => 'name=WizardForm[customer_adr_zip]',
                'rules' => array('required'),
                'translationKey' => 'LICENCE_MODEL_CUSTOMER_ADR_ZIP',
                'defaultValue' => '10827'
            ),
            array(
                'selector' => 'name=WizardForm[customer_adr_region]',
                'rules' => array('required'),
                'translationKey' => 'LICENCE_MODEL_CUSTOMER_ADR_REGION',
                'defaultValue' => 'Berlin'
            ),
            array(
                'selector' => 'name=WizardForm[customer_adr_country]',
                'rules' => array('required'),
                'translationKey' => 'LICENCE_MODEL_CUSTOMER_ADR_COUNTRY',
                'defaultValue' => 'Germany'
            ),
            array(
                'selector' => 'name=WizardForm[customer_email]',
                'rules' => array('required', 'email'),
                'translationKey' => 'LICENCE_MODEL_CUSTOMER_EMAIL',
                'defaultValue' => 'anton.pauli@aerea.de'
            ),
            array(
                'selector' => 'name=WizardForm[customer_branch]',
                'translationKey' => 'LICENCE_MODEL_CUSTOMER_BRANCH',
                'defaultValue' => ''
            ),

        );


        $this->checkInputValidation(
            $licenseDataInputs,
            "//button[@id='nextBtn']",
            false // use no ajaxValidation
        );


        $this->clickAndWait("//button[@id='nextBtn']");
        $this->waitForTextPresent("Error 400: Invalid license key.");
        $this->clickAndWait("//button[@id='skipBtn']");
    }
}
