def test_billing_same_as_shipping(self):
        adapter = IShoppingSite(self.portal)
        self.assertIsNone(adapter.billing_same_as_shipping())

        adapter.cart = mock.Mock(return_value={'billing_same_as_shipping': False})
        self.assertFalse(adapter.billing_same_as_shipping())

        adapter.cart = mock.Mock(return_value={'billing_same_as_shipping': True})
        self.assertTrue(adapter.billing_same_as_shipping())
    def test_create_cart(self, create_order):
        adapter = IShoppingSite(self.portal)

        create_order.return_value = None
        self.assertIsNone(adapter.create_order())

        order1 = self.create_content('collective.cart.core.Order', id='1')
        create_order.return_value = order1
        self.assertEqual(adapter.create_order(), order1)
        with self.assertRaises(KeyError):
            order1['shipping_method']
        with self.assertRaises(KeyError):
            order1['billing']
        with self.assertRaises(KeyError):
            order1['shipping']

        adapter.shipping_method = mock.Mock(return_value={'gross': self.money('10.00')})
        order2 = self.create_content('collective.cart.core.Order', id='2')
        create_order.return_value = order2
        self.assertEqual(adapter.create_order(), order2)
        self.assertIsNotNone(order2['shipping_method'])
        with self.assertRaises(KeyError):
            order2['billing']
        with self.assertRaises(KeyError):
            order2['shipping']

        names = ['city', 'last_name', 'first_name', 'email', 'phone', 'post', 'street']
        address = {}
        for name in names:
            address[name] = name.upper()
        adapter.get_address = mock.Mock(return_value=address)
        adapter.billing_same_as_shipping = mock.Mock(return_value=True)
        order3 = self.create_content('collective.cart.core.Order', id='3')
        create_order.return_value = order3
        self.assertEqual(adapter.create_order(), order3)
        self.assertIsNotNone(order3['shipping_method'])
        self.assertIsNotNone(order3['billing'])
        with self.assertRaises(KeyError):
            order3['shipping']

        adapter.billing_same_as_shipping = mock.Mock(return_value=False)
        order4 = self.create_content('collective.cart.core.Order', id='4')
        create_order.return_value = order4
        self.assertEqual(adapter.create_order(), order4)
        self.assertIsNotNone(order4['shipping_method'])
        self.assertIsNotNone(order4['billing'])
        self.assertIsNotNone(order4['shipping'])
    def test_is_addresses_filled(self):
        adapter = IShoppingSite(self.portal)
        self.assertIsNone(adapter.is_addresses_filled())

        adapter.is_address_filled = mock.Mock(return_value=True)
        self.assertTrue(adapter.is_addresses_filled())
        self.assertEqual(adapter.is_address_filled.call_args_list, [(('billing',),), (('shipping',),)])

        adapter.billing_same_as_shipping = mock.Mock(return_value=True)
        self.assertTrue(adapter.is_addresses_filled())
        self.assertEqual(adapter.is_address_filled.call_args_list, [(('billing',),), (('shipping',),), (('billing',),)])
def notify_ordered(context, event):
    if event.action == 'ordered':
        shopping_site = IShoppingSite(context)
        adapter = IOrderAdapter(context)
        portal = shopping_site.portal()
        email_from_address = getUtility(IRegistry)['collective.cart.shopping.notification_cc_email'] or portal.getProperty('email_from_address')

        billing = shopping_site.get_address('billing')
        default_charset = getattr(getattr(getToolByName(context, 'portal_properties'), 'site_properties'), 'default_charset', 'utf-8')
        email_charset = getUtility(ISiteRoot).getProperty('email_charset', 'utf-8')
        subject = context.translate(_(u'order-number', u'Order Number: ${number}', mapping={'number': context.id}))
        utility = getUtility(IUnicodeUtility)
        mfrom = u'"{}" <{}>'.format(utility.safe_unicode(shopping_site.shop().title), email_from_address)
        host = getToolByName(context, 'MailHost')

        underline = '=' * 28
        billing_address = utility.address(billing)
        if shopping_site.billing_same_as_shipping():
            shipping_address = billing_address
        else:
            shipping = shopping_site.get_address('shipping')
            shipping_address = utility.address(shipping)
        articles = shopping_site.cart_article_listing()
        for article in articles:
            subtotal = article['gross'] * article['quantity']
            article.update({'subtotal': shopping_site.format_money(subtotal)})
        shipping_method_title = hasattr(
            adapter.shipping_method(), 'Title') and adapter.shipping_method().Title.decode(default_charset) or u''

        items = shopping_site.cart().copy()
        for key in ['articles', 'billing_same_as_shipping', 'shipping_method', 'billing', 'shipping']:
            if key in items:
                del items[key]

        items.update({
            'number': context.id,
            'underline': underline,
            'billing_address': billing_address,
            'shipping_address': shipping_address,
            'articles': articles,
            'shipping_method_title': shipping_method_title,
            'is_shipping_free': shopping_site.shipping_gross_money().amount == 0.0,
            'shipping_gross': shopping_site.locale_shipping_gross(),
            'total': shopping_site.locale_total(),
        })

        message_to_customer = context.unrestrictedTraverse('@@to-customer-order-mail-template')(**items)
        mto_customer = u'"{}" <{}>'.format(utility.fullname(billing), billing['email'])
        subject_to_customer = subject

        message_to_shop = context.unrestrictedTraverse('@@to-shop-order-mail-template')(**items)
        mto_shop = mfrom
        subject_to_shop = subject

        try:
            host.send(message_to_customer, mto_customer, mfrom, subject=subject_to_customer, charset=email_charset)
            host.send(message_to_shop, mto_shop, mfrom, subject=subject_to_shop, charset=email_charset)

        except:
            message = _(u'order-processed-but',
                default=u'The order was processed but we could not send e-mail to you successfully. Please consult the shop owner.')
            IStatusMessage(context.REQUEST).addStatusMessage(message, type='warn')