# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from context import nikola
import os
import unittest
import mock
class CommandImportWordpressTest(unittest.TestCase):
def setUp(self):
self.import_command = nikola.plugins.command_import_wordpress.CommandImportWordpress()
self.import_filename = os.path.abspath(
os.path.join(os.path.dirname(__file__),
'wordpress_export_example.xml'))
def tearDown(self):
del self.import_command
del self.import_filename
def test_create_import_work_without_argument(self):
# Running this without an argument must not fail.
# It should show the proper usage of the command.
self.import_command.run()
def test_create_import(self):
data_import = mock.MagicMock()
site_generation = mock.MagicMock()
write_urlmap = mock.MagicMock()
write_configuration = mock.MagicMock()
with mock.patch('nikola.plugins.command_import_wordpress.CommandImportWordpress.generate_base_site', site_generation):
with mock.patch('nikola.plugins.command_import_wordpress.CommandImportWordpress.import_posts', data_import):
with mock.patch('nikola.plugins.command_import_wordpress.CommandImportWordpress.write_urlmap_csv', write_urlmap):
with mock.patch('nikola.plugins.command_import_wordpress.CommandImportWordpress.write_configuration', write_configuration):
self.import_command.run(self.import_filename)
self.assertTrue(site_generation.called)
self.assertTrue(data_import.called)
def test_populate_context(self):
channel = self.import_command.get_channel_from_file(
self.import_filename)
context = self.import_command.populate_context(channel)
for required_key in ('POST_PAGES', 'POST_COMPILERS'):
self.assertTrue(required_key in context)
self.assertEqual('de', context['DEFAULT_LANG'])
self.assertEqual('Wordpress blog title', context['BLOG_TITLE'])
self.assertEqual('Nikola test blog ;) - with moré Ümläüts', context['BLOG_DESCRIPTION'])
self.assertEqual('http://some.blog', context['BLOG_URL'])
self.assertEqual('mail@some.blog', context['BLOG_EMAIL'])
self.assertEqual('Niko', context['BLOG_AUTHOR'])
def test_importing_posts_and_attachments(self):
channel = self.import_command.get_channel_from_file(
self.import_filename)
self.import_command.context = self.import_command.populate_context(
channel)
self.import_command.url_map = {} # For testing we use an empty one.
write_metadata = mock.MagicMock()
write_content = mock.MagicMock()
download_mock = mock.MagicMock()
with mock.patch('nikola.plugins.command_import_wordpress.CommandImportWordpress.write_content', write_content):
with mock.patch('nikola.plugins.command_import_wordpress.CommandImportWordpress.write_metadata', write_metadata):
with mock.patch('nikola.plugins.command_import_wordpress.CommandImportWordpress.download_url_content_to_file', download_mock):
with mock.patch('nikola.plugins.command_import_wordpress.os.makedirs'):
self.import_command.import_posts(channel)
self.assertTrue(download_mock.called)
download_mock.assert_any_call(u'http://some.blog/wp-content/uploads/2008/07/arzt_und_pfusch-sick-cover.png', u'new_site/files/wp-content/uploads/2008/07/arzt_und_pfusch-sick-cover.png')
self.assertTrue(write_metadata.called)
write_metadata.assert_any_call(u'new_site/stories/kontakt.meta', 'Kontakt', u'kontakt', '2009-07-16 20:20:32', None, [])
self.assertTrue(write_content.called)
write_content.assert_any_call(u'new_site/posts/200704hoert.wp', '...!\n\n\n\n[caption id="attachment_16" align="alignnone" width="739" caption="caption test"]
[/caption]\n\n\n\nNicht, dass daran jemals Zweifel bestanden.')
write_content.assert_any_call(u'new_site/posts/200807arzt-und-pfusch-s-i-c-k.wp', u'
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.\n\nDie 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.')
write_content.assert_any_call(u'new_site/stories/kontakt.wp', u'