Пример #1
0
    def test_restore_specific_config(self, wp_generator_generic, wp_plugin_list):

        # First, uninstall from WP installation
        plugin_config = wp_plugin_list.plugins(TEST_SITE)['add-to-any']
        wp_plugin_config = WPPluginConfig(wp_generator_generic.wp_site, 'add-to-any', plugin_config)
        wp_plugin_config.uninstall()

        # Then, reinstall and configure it
        wp_plugin_config.install()
        wp_plugin_config.configure(force=True)

        # Check plugin options
        wp_config = WPConfig(wp_generator_generic.wp_site)
        assert wp_config.run_wp_cli("option get addtoany_options") == 'test_overload'
        assert wp_config.run_wp_cli("option get addtoany_dummy") == 'dummy'
Пример #2
0
    def test_restore_generic_config(self, wp_generator_generic,
                                    wp_plugin_list):

        # First, uninstall from WP installation
        plugin_config = wp_plugin_list.plugins()['add-to-any']
        wp_plugin_config = WPPluginConfig(wp_generator_generic.wp_site,
                                          'add-to-any', plugin_config)
        wp_plugin_config.uninstall()

        # Then, reinstall plugin and configure it
        wp_plugin_config.install()
        wp_plugin_config.configure()

        # Check plugin options
        wp_config = WPConfig(wp_generator_generic.wp_site)
        assert wp_config.run_wp_cli("option get addtoany_options") == 'test'
Пример #3
0
    def fix_site(self, openshift_env, wp_site_url, shortcode_name=None):
        """
        Fix shortocdes in WP site
        :param openshift_env: openshift environment name
        :param wp_site_url: URL to website to fix.
        :param shortcode_name: fix site for this shortcode only
        :return: dictionnary with report.
        """

        content_filename = Utils.generate_name(15, '/tmp/')

        wp_site = WPSite(openshift_env, wp_site_url)
        wp_config = WPConfig(wp_site)

        if not wp_config.is_installed:
            logging.info("No WP site found at given URL (%s)", wp_site_url)
            return self.report

        logging.info("Fixing %s...", wp_site.path)

        # Getting site posts
        post_ids = wp_config.run_wp_cli("post list --post_type=page --skip-plugins --skip-themes "
                                        "--format=csv --fields=ID")

        # Nothing to fix
        if not post_ids:
            logging.info("No page found, nothing to do!")
            return

        post_ids = post_ids.split('\n')[1:]

        # Looping through posts
        for post_id in post_ids:
            logging.info("Fixing page ID %s...", post_id)
            content = wp_config.run_wp_cli("post get {} --skip-plugins --skip-themes "
                                           "--field=post_content".format(post_id))
            original_content = content

            # Step 1 - Fixing shortcodes
            # Looking for all shortcodes in current post
            for shortcode in list(set(re.findall(self.regex, content))):

                if shortcode_name is None or shortcode_name.startswith(shortcode):

                    fix_func_name = "_fix_{}".format(shortcode.replace("-", "_"))

                    if shortcode_name is not None and shortcode_name.endswith("_new_version"):
                        fix_func_name += "_new_version"

                    try:
                        # Trying to get function to fix current shortcode
                        fix_func = getattr(self, fix_func_name)
                    except Exception as e:
                        # "Fix" function not found, skipping to next shortcode
                        continue

                    logging.debug("Fixing shortcode %s...", shortcode)
                    content = fix_func(content)

            # Step 2: Removing <div class="textbox"> to avoid display issues on 2018 theme
            soup = BeautifulSoup(content, 'html5lib')
            soup.body.hidden = True

            # Looking for all DIVs with "textBox" as class
            for div in soup.find_all('div', {'class': 'textBox'}):
                # Remove DIV but keep its content
                div.unwrap()

            content = str(soup.body)

            # If content changed for current page,
            if content != original_content:

                logging.debug("Content fixed, updating page...")

                for try_no in range(settings.WP_CLI_AND_API_NB_TRIES):
                    try:
                        # We use a temporary file to store page content to avoid to have problems with simple/double
                        # quotes and content size
                        with open(content_filename, 'wb') as content_file:
                            content_file.write(content.encode())
                        wp_config.run_wp_cli("post update {} --skip-plugins --skip-themes {} ".format(
                            post_id, content_filename))

                    except Exception as e:
                        if try_no < settings.WP_CLI_AND_API_NB_TRIES - 1:
                            logging.error("fix_site() error. Retry %s in %s sec...",
                                          try_no + 1,
                                          settings.WP_CLI_AND_API_NB_SEC_BETWEEN_TRIES)
                            time.sleep(settings.WP_CLI_AND_API_NB_SEC_BETWEEN_TRIES)
                            pass

        # Cleaning
        if os.path.exists(content_filename):
            os.remove(content_filename)

        return self.report