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_shipping_method(self):
        adapter = IShoppingSite(self.portal)
        self.assertIsNone(adapter.shipping_method())

        adapter.cart = mock.MagicMock(return_value={'shipping_method': 'SHIPPINIG_METHOD'})
        self.assertEqual(adapter.shipping_method(), 'SHIPPINIG_METHOD')
    def test_get_address(self):
        adapter = IShoppingSite(self.portal)
        self.assertIsNone(adapter.get_address('billing'))

        adapter.cart = mock.Mock(return_value={'billing': 'BILLING'})
        self.assertEqual(adapter.get_address('billing'), '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')