# -*- coding: utf-8 -*- from __future__ import unicode_literals from context import nikola import os import unittest import mock class BasicCommandImportWordpress(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 class CommandImportWordpressRunTest(BasicCommandImportWordpress): def setUp(self): super(self.__class__, self).setUp() self.data_import = mock.MagicMock() self.site_generation = mock.MagicMock() self.write_urlmap = mock.MagicMock() self.write_configuration = mock.MagicMock() site_generation_patch = mock.patch( 'nikola.plugins.command_import_wordpress.CommandImportWordpress.generate_base_site', self.site_generation) data_import_patch = mock.patch( 'nikola.plugins.command_import_wordpress.CommandImportWordpress.import_posts', self.data_import) write_urlmap_patch = mock.patch( 'nikola.plugins.command_import_wordpress.CommandImportWordpress.write_urlmap_csv', self.write_urlmap) write_configuration_patch = mock.patch( 'nikola.plugins.command_import_wordpress.CommandImportWordpress.write_configuration', self.write_configuration) self.patches = [site_generation_patch, data_import_patch, write_urlmap_patch, write_configuration_patch] for patch in self.patches: patch.start() def tearDown(self): del self.data_import del self.site_generation del self.write_urlmap del self.write_configuration for patch in self.patches: patch.stop() del self.patches super(self.__class__, self).tearDown() def test_create_import(self): valid_import_arguments = ( ['--filename', self.import_filename], ['-f', self.import_filename, '-o', 'some_folder'], [self.import_filename], [self.import_filename, 'folder_argument'], ) for arguments in valid_import_arguments: self.import_command.run(*arguments) self.assertTrue(self.site_generation.called) self.assertTrue(self.data_import.called) self.assertTrue(self.write_urlmap.called) self.assertTrue(self.write_configuration.called) self.assertFalse(self.import_command.exclude_drafts) def test_ignoring_drafts(self): valid_import_arguments = ( ['--filename', self.import_filename, '--no-drafts'], ['-f', self.import_filename, '-o', 'some_folder', '-d'], ) for arguments in valid_import_arguments: self.import_command.run(*arguments) self.assertTrue(self.import_command.exclude_drafts) def test_getting_help(self): for arguments in (['-h'], ['--help']): self.assertRaises(SystemExit, self.import_command.run, *arguments) self.assertFalse(self.site_generation.called) self.assertFalse(self.data_import.called) self.assertFalse(self.write_urlmap.called) self.assertFalse(self.write_configuration.called) class CommandImportWordpressTest(BasicCommandImportWordpress): 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_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. self.import_command.output_folder = 'new_site' 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( 'http://some.blog/wp-content/uploads/2008/07/arzt_und_pfusch-sick-cover.png', 'new_site/files/wp-content/uploads/2008/07/arzt_und_pfusch-sick-cover.png') self.assertTrue(write_metadata.called) write_metadata.assert_any_call( 'new_site/stories/kontakt.meta', 'Kontakt', 'kontakt', '2009-07-16 20:20:32', None, []) self.assertTrue(write_content.called) write_content.assert_any_call('new_site/posts/200704hoert.wp', 'An image.\n\n\n\ncaption test\n\n\n\nSome source code.\n\n\n\n\n~~~~~~~~~~~~{.Python}\n\n\nimport sys\n\nprint sys.version\n\n\n~~~~~~~~~~~~\n\n\n\n\nThe end.\n\n') write_content.assert_any_call( 'new_site/posts/200807arzt-und-pfusch-s-i-c-k.wp', 'Arzt+Pfusch - S.I.C.K.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( 'new_site/stories/kontakt.wp', '

Datenschutz

\n\nIch erhebe und speichere automatisch in meine Server Log Files Informationen, die dein Browser an mich \xfcbermittelt. Dies sind:\n\n\n\nDiese Daten sind f\xfcr mich nicht bestimmten Personen zuordenbar. Eine Zusammenf\xfchrung dieser Daten mit anderen Datenquellen wird nicht vorgenommen, die Daten werden einzig zu statistischen Zwecken erhoben.') self.assertTrue(len(self.import_command.url_map) > 0) self.assertEqual( self.import_command.url_map['http://some.blog/2007/04/hoert/'], 'http://some.blog/posts/200704hoert.html') self.assertEqual( self.import_command.url_map[ 'http://some.blog/2008/07/arzt-und-pfusch-s-i-c-k/'], 'http://some.blog/posts/200807arzt-und-pfusch-s-i-c-k.html') self.assertEqual( self.import_command.url_map['http://some.blog/kontakt/'], 'http://some.blog/stories/kontakt.html') def test_transforming_content(self): """Applying markup conversions to content.""" transform_sourcecode = mock.MagicMock() transform_caption = mock.MagicMock() with mock.patch('nikola.plugins.command_import_wordpress.CommandImportWordpress.transform_sourcecode', transform_sourcecode): with mock.patch('nikola.plugins.command_import_wordpress.CommandImportWordpress.transform_caption', transform_caption): self.import_command.transform_content("random content") self.assertTrue(transform_sourcecode.called) self.assertTrue(transform_caption.called) def test_transforming_source_code(self): """ Tests the handling of sourcecode tags. """ content = """Hello World. [sourcecode language="Python"] import sys print sys.version [/sourcecode]""" content = self.import_command.transform_sourcecode(content) self.assertFalse('[/sourcecode]' in content) self.assertFalse('[sourcecode language=' in content) replaced_content = """Hello World. ~~~~~~~~~~~~{.Python} import sys print sys.version ~~~~~~~~~~~~ """ self.assertEqual(content, replaced_content) def test_transform_caption(self): caption = '[caption id="attachment_16" align="alignnone" width="739" caption="beautiful picture"]beautiful picture[/caption]' transformed_content = self.import_command.transform_caption(caption) expected_content = 'beautiful picture' self.assertEqual(transformed_content, expected_content) def test_transform_multiple_captions_in_a_post(self): content = """asdasdas [caption id="attachment_16" align="alignnone" width="739" caption="beautiful picture"]beautiful picture[/caption] asdasdas asdasdas [caption id="attachment_16" align="alignnone" width="739" caption="beautiful picture"]beautiful picture[/caption] asdasdas""" expected_content = """asdasdas beautiful picture asdasdas asdasdas beautiful picture asdasdas""" self.assertEqual( expected_content, self.import_command.transform_caption(content)) def test_transform_caption_with_link_inside(self): content = """[caption caption="Fehlermeldung"]Fehlermeldung[/caption]""" transformed_content = self.import_command.transform_caption(content) expected_content = """Fehlermeldung""" self.assertEqual(expected_content, transformed_content) def test_get_configuration_output_path(self): self.import_command.output_folder = 'new_site' default_config_path = os.path.join('new_site', 'conf.py') self.import_command.import_into_existing_site = False self.assertEqual(default_config_path, self.import_command.get_configuration_output_path()) self.import_command.import_into_existing_site = True config_path_with_timestamp = self.import_command.get_configuration_output_path( ) self.assertNotEqual(default_config_path, config_path_with_timestamp) self.assertTrue('wordpress_import' in config_path_with_timestamp) def test_write_content_does_not_detroy_text(self): content = b"""

Installation

Follow the instructions described here.

Plugins

There are many plugins.

Violations

You can use the Violations plugin.""" open_mock = mock.mock_open() with mock.patch('nikola.plugins.command_import_wordpress.open', open_mock, create=True): self.import_command.write_content('some_file', content) open_mock.assert_called_once_with('some_file', 'wb+') call_context = open_mock() call_context.write.assert_called_once_with( content.join([b'', b''])) if __name__ == '__main__': unittest.main()