import os
from unittest import mock
import pytest
import nikola.plugins.command.import_wordpress
def test_create_import_work_without_argument(import_command):
"""
Running import command without an argument must not fail.
It should show the proper usage of the command.
"""
import_command.execute()
@pytest.mark.parametrize(
"key, expected_value",
[
("DEFAULT_LANG", "de"),
("BLOG_TITLE", "Wordpress blog title"),
("BLOG_DESCRIPTION", "Nikola test blog ;) - with moré Ümläüts"),
("SITE_URL", "http://some.blog/"),
("BLOG_EMAIL", "mail@some.blog"),
("BLOG_AUTHOR", "Niko"),
],
)
def test_populate_context(import_command, import_filename, key, expected_value):
channel = import_command.get_channel_from_file(import_filename)
import_command.html2text = False
import_command.transform_to_markdown = False
import_command.transform_to_html = False
import_command.use_wordpress_compiler = False
import_command.translations_pattern = "{path}.{lang}.{ext}"
context = import_command.populate_context(channel)
for required_key in ("POSTS", "PAGES", "COMPILERS"):
assert required_key in context
assert expected_value == context[key]
def test_importing_posts_and_attachments(module, import_command, import_filename):
channel = import_command.get_channel_from_file(import_filename)
import_command.base_dir = ""
import_command.output_folder = "new_site"
import_command.squash_newlines = True
import_command.no_downloads = False
import_command.export_categories_as_categories = False
import_command.export_comments = False
import_command.html2text = False
import_command.transform_to_markdown = False
import_command.transform_to_html = False
import_command.use_wordpress_compiler = False
import_command.tag_saniziting_strategy = "first"
import_command.separate_qtranslate_content = False
import_command.translations_pattern = "{path}.{lang}.{ext}"
import_command.context = import_command.populate_context(channel)
# Ensuring clean results
# assert not import_command.url_map
assert not module.links
import_command.url_map = {}
write_metadata = mock.MagicMock()
write_content = mock.MagicMock()
write_attachments_info = mock.MagicMock()
download_mock = mock.MagicMock()
with mock.patch(
"nikola.plugins.command.import_wordpress.CommandImportWordpress.write_content",
write_content,
), mock.patch(
"nikola.plugins.command.import_wordpress.CommandImportWordpress.write_metadata",
write_metadata,
), mock.patch(
"nikola.plugins.command.import_wordpress.CommandImportWordpress.download_url_content_to_file",
download_mock,
), mock.patch(
"nikola.plugins.command.import_wordpress.CommandImportWordpress.write_attachments_info",
write_attachments_info,
), mock.patch(
"nikola.plugins.command.import_wordpress.os.makedirs"
):
import_command.import_posts(channel)
assert download_mock.called
qpath = "new_site/files/wp-content/uploads/2008/07/arzt_und_pfusch-sick-cover.png"
download_mock.assert_any_call(
"http://some.blog/wp-content/uploads/2008/07/arzt_und_pfusch-sick-cover.png",
qpath.replace("/", os.sep),
)
assert write_metadata.called
write_metadata.assert_any_call(
"new_site/pages/kontakt.meta".replace("/", os.sep),
"Kontakt",
"kontakt",
"2009-07-16 20:20:32",
"",
[],
**{"wp-status": "publish"}
)
assert write_content.called
write_content.assert_any_call(
"new_site/posts/2007/04/hoert.md".replace("/", os.sep),
"""An image.
Some source code.
```Python
import sys
print sys.version
```
The end.
""",
True,
)
assert write_attachments_info.called
write_attachments_info.assert_any_call(
"new_site/posts/2008/07/arzt-und-pfusch-s-i-c-k.attachments.json".replace(
"/", os.sep
),
{
10: {
"wordpress_user_name": "Niko",
"files_meta": [
{"width": 300, "height": 299},
{"width": 150, "size": "thumbnail", "height": 150},
],
"excerpt": "Arzt+Pfusch - S.I.C.K.",
"date_utc": "2009-07-16 19:40:37",
"content": "Das Cover von Arzt+Pfusch - S.I.C.K.",
"files": [
"/wp-content/uploads/2008/07/arzt_und_pfusch-sick-cover.png",
"/wp-content/uploads/2008/07/arzt_und_pfusch-sick-cover-150x150.png",
],
"title": "Arzt+Pfusch - S.I.C.K.",
}
},
)
write_content.assert_any_call(
"new_site/posts/2008/07/arzt-und-pfusch-s-i-c-k.md".replace("/", os.sep),
"""
Arzt+Pfusch - S.I.C.K.Gerade bin ich \xfcber das Album S.I.C.K von Arzt+Pfusch gestolpert, welches Arzt+Pfusch zum Download f\xfcr lau anbieten. Das Album steht unter einer Creative Commons BY-NC-ND-Lizenz.
Die Ladung noisebmstupidevildustrial gibts als MP3s mit 64kbps und VBR, als Ogg Vorbis und als FLAC (letztere hier). Artwork und Lyrics gibts nochmal einzeln zum Download.""",
True,
)
write_content.assert_any_call(
"new_site/pages/kontakt.md".replace("/", os.sep),
"""
[/caption]'
transformed_content = import_command.transform_caption(caption)
expected_content = '
'
assert transformed_content == expected_content
def test_transform_multiple_captions_in_a_post(import_command):
content = """asdasdas
[caption id="attachment_16" align="alignnone" width="739" caption="beautiful picture"]
[/caption]
asdasdas
asdasdas
[caption id="attachment_16" align="alignnone" width="739" caption="beautiful picture"]
[/caption]
asdasdas"""
expected_content = """asdasdas
asdasdas
asdasdas
asdasdas"""
assert expected_content == import_command.transform_caption(content)
def test_transform_multiple_newlines(import_command):
content = """This
has
way to many
newlines.
"""
expected_content = """This
has
way to many
newlines.
"""
import_command.squash_newlines = False
assert content == import_command.transform_multiple_newlines(content)
import_command.squash_newlines = True
assert expected_content == import_command.transform_multiple_newlines(content)
def test_transform_caption_with_link_inside(import_command):
content = """[caption caption="Fehlermeldung"]
[/caption]"""
transformed_content = import_command.transform_caption(content)
expected_content = """
"""
assert expected_content == transformed_content
def test_get_configuration_output_path(import_command):
import_command.output_folder = "new_site"
default_config_path = os.path.join("new_site", "conf.py")
import_command.import_into_existing_site = False
assert default_config_path == import_command.get_configuration_output_path()
import_command.import_into_existing_site = True
config_path_with_timestamp = import_command.get_configuration_output_path()
assert default_config_path != config_path_with_timestamp
assert import_command.name in config_path_with_timestamp
def test_write_content_does_not_detroy_text(import_command):
content = b"""FOO"""
open_mock = mock.mock_open()
with mock.patch("nikola.plugins.basic_import.open", open_mock, create=True):
import_command.write_content("some_file", content)
open_mock.assert_has_calls(
[
mock.call(u"some_file", u"wb+"),
mock.call().__enter__(),
mock.call().write(b"FOO
"), mock.call().__exit__(None, None, None), ] ) def test_configure_redirections(import_command): """ Testing the configuration of the redirections. We need to make sure that we have valid sources and target links. """ url_map = {"/somewhere/else": "http://foo.bar/posts/somewhereelse.html"} redirections = import_command.configure_redirections(url_map) assert 1 == len(redirections) assert ("somewhere/else/index.html", "/posts/somewhereelse.html") in redirections @pytest.mark.parametrize( "options, additional_args", [ pytest.param(None, None, id="only import filename"), ({"output_folder": "some_folder"}, None), (None, ["folder_argument"]), ], ) def test_create_import( patched_import_command, import_filename, mocks, options, additional_args ): arguments = {"args": [import_filename]} if options: arguments["options"] = options if additional_args: arguments["args"].extend(additional_args) patched_import_command.execute(**arguments) for applied_mock in mocks: assert applied_mock.called assert patched_import_command.exclude_drafts is False @pytest.mark.parametrize( "options", [ {"exclude_drafts": True}, {"exclude_drafts": True, "output_folder": "some_folder"}, ], ) def test_ignoring_drafts_during_import( patched_import_command, import_filename, options ): arguments = {"options": options, "args": [import_filename]} patched_import_command.execute(**arguments) assert patched_import_command.exclude_drafts is True @pytest.fixture def import_command(module): command = module.CommandImportWordpress() command.onefile = False return command @pytest.fixture def module(): return nikola.plugins.command.import_wordpress @pytest.fixture def import_filename(test_dir): return os.path.abspath( os.path.join( test_dir, "data", "wordpress_import", "wordpress_export_example.xml" ) ) @pytest.fixture def patched_import_command(import_command, testsite, mocks): """ Import command with disabled site generation and various functions mocked. """ data_import, site_generation, write_urlmap, write_configuration = mocks import_command.site = testsite with mock.patch("os.system", site_generation), mock.patch( "nikola.plugins.command.import_wordpress.CommandImportWordpress.import_posts", data_import, ), mock.patch( "nikola.plugins.command.import_wordpress.CommandImportWordpress.write_urlmap_csv", write_urlmap, ), mock.patch( "nikola.plugins.command.import_wordpress.CommandImportWordpress.write_configuration", write_configuration, ): yield import_command @pytest.fixture def testsite(): return FakeSite() class FakeSite: def link(self, *args, **kwargs): # We need a link function. # Stubbed because there is nothing done with the results. pass @pytest.fixture def mocks(): "Mocks to be used in `patched_import_command`" return [ mock.MagicMock(name="data_import"), mock.MagicMock(name="site_generation"), mock.MagicMock(name="write_urlmap"), mock.MagicMock(name="write_configuration"), ]