Magento API V2 Soap unit testing

In previous article, I wrote about setting up environment for Magento unit testing. Let’s expand our tests to test some Magento’s API calls with soap.

Let’s add first tests in our Tests/customerCustomerTest.php

<?php

require_once 'PHPUnit/Autoload.php';
require_once '../app/Mage.php';

class customerCustomerTest extends PHPUnit_Framework_TestCase {

private $local_url_v1 = "http://192.168.1.91/api/soap/?wsdl=1";
private $local_url_v2 = "http://192.168.1.91/api/v2_soap/?wsdl=1";
private $api_url_v1;
private $api_url_v2;

public function setUp() {
    Mage::app('default');
    $this->setApiUrlV2($this->local_url_v2);
}

public function getApiUrlV2() {
    return $this->api_url_v2;
}

public function setApiUrlV2($api_url_v2) {
    $this->api_url_v2 = $api_url_v2;
}

public function testLogin() {

   $cli = new SoapClient($this->api_url_v2);

   $username = 'mobile';
   $password = 'mobile123';

   $result = $cli->login($username, $password);
   $session_id = isset($result) ? $result : null;

   $this->assertNotNull($session_id);
   return $session_id;
}

public function testCoreCustomerList_V2() {

   $session_id = $this->testLogin();
   $cli = new SoapClient($this->api_url_v2);
   $result = $cli->customerCustomerList($session_id);

   $this->assertTrue(is_array($result));
   foreach ($result as $res) {
      $this->assertObjectHasAttribute('customer_id', $res);
   }
 }

}

Here we added two test methods: first one id login and it’s neccesary to return session Id for using in other API calls. Also it is neccesary to add mobile user with appropiate roles in administration of your Magento installation.

Hope that this article was useful to you.

Cheers.

Magento Enterprise – Discount for specific Customer segments group

In previous post we create Customer group based on company emails. Now when we have few customers registered with company emails we will create campaign and give them 50% discount for purchasing Laptops.

In Magento admin area open  Promotions -> Shopping Cart Price Rules

Now we will create a new rule

Fill up information regarding new rule

Using conditions we will use our previously created Customer Segment “Company Discount”

Under Action we will give 50% of discount on product price from Category “Laptops”

Now we will go to store and login using Company email.

Browse Category “Laptops” and choose one of the products, in my case it will be Apple MacBook Pro, normal price is displayed, continue to checkout.

And now we have finally applied our Company discount 50% off

This is only example you can create may other rules based on Customer Segments under Shopping Cart Price Rules, discount, free shipping, add reward points, etc.

Installation and configuration of local Mail server for Windows (hMail Server)

This is not exactly Magento related post, but it might be useful when you are setting up a dev environment on your local machine. There are many good and free mail servers out there which are relatively easy to install on your Windows machine, but the problems may occur when you try to configure all the bits and pieces of your system so you can actually use your local mail server for something other than producing system errors :) .

When I started working with PHP a long time ago, I was (naturally) looking for a mail server that could be used on my local machine. For some reason that is now long forgotten, I have decided to use hMail Server (freeware). Since it never failed me, it stayed in my standard set of must-have tools. In this post I’ll try to present a step-by-step manual on how to install and configure your system to use hMail Server.

Although this post might be a bit lengthy, please, don’t be scared :) . These are all simple steps and if you have any experience as a Windows user, you should be able to skip reading the most of them.

System properties (listed just as a reference)

  • Windows 7 Professional with the latest updates
  • MySQL 5.0.51b
  • Apache 2.2.17

Part 1 – Installation and Database configuration

  • Download hMail Server from their download page. My suggestion is to get the latest production release as it is the most stable one;
  •  Start the installer and pick “Full installation”. That should include Administrative tools which are very useful;
  • When asked, set MySQL as your mail database. You don’t need to create the database manually, installer will do that for you;
  • When you complete the installation wizard, database configuration wizard will appear where you should pick “Create new mail database” option;
  • Copy libmysql.dll to /hMailServer/bin directory;
  • Enter the MySQL server and user info.  You can use localhost as server name and whatever port you used for MySQL (default is 3306);
  • Pick the database service from the dropdown so that hMail server can hook to it. It’s usually listed as “MySQL” but  can be listed as “WAMP Mysql” (or whatever name you used) if you have WAMP installed;
  • Database configuration should be completed now and you can proceed to run the hMail Server Administration tool.

Part 2 – Configuring Windows DNS, port and firewall settings

  • The first thing you need to do is add a couple of entries to the Windows’ hosts file. Click on the Windows Start button and search for Notepad. Right click Notepad.exe and choose “Run as Administrator”. This will ensure that you will be able to save the “hosts” file once you’ve finished adding the changes, since the file is protected by the operating system by default;
  • Open the hosts file in Notepad. It’s located in Windows\System32\drivers\etc\ folder;
  • add the following at the end of the file (you can put anything instead of “localserver”):
127.0.0.1	mail.localserver.com
127.0.0.1	localserver.com
127.0.0.1	smtp.mail.localserver.com
127.0.0.1	pop3.mail.localserver.com
  • Open “Windows Firewall” form Control Panel or by searching for it in the Start menu;
  • Go to “Inbound rules” in the left bar and create a new rule for a Port. Type the port numbers for SMTP, POP3 and optionally IMAP protocols in TCP > Selected local ports field. You don’t need to create 3 rules for each port, because you can add all three of them with a comma separator (for example: 25, 110, 143);
  • Enter the name of the rule. Something like “hMail open ports” should do;
  • If you have any custom firewall installed, either stand-alone or as part of your AV software, you might want to look at your software’s documentation and see how to open ports. The ports you need to open are 25, 110 and optionally 143 if you plan to use IMAP.

Part 3 – Configuring hMail server

  • Create a new Domain in the hMail Administrator and put “mail.localserver.com” in the Domain field;
  • Create one administrator mail account by going to “Domains > mail.localserver.com > Accounts > Add” and choosing “Server” in the “Administration level” field. We will use “master@mail.localserver.com” as an example;
  • Go to the “Delivery of e-mail” tab in the “Settings > Protocols > SMTP” and enter  ”mail.localserver.com” in the “Local Host Name” field;
  • Go to “Settings > Advanced > TCP/IP Ports” and configure each of the sub-entries to use 127.0.0.1 as IP address.

That’s it! Your local mail server is now up and running. The only thing you need to do now is to connect your newly created local mail address to some e-mail application. I will post here a quick how-to tips for Thunderbird.

Configuring Thunderbird mail account

  • Go to “Tools > Account settings” in the Thunderbird main menu and create a new account (“Account actions” button, then “Add new account” option);
  • Use “master@mail.localserver.com” e-mail address (the one you have created in the hMail Administration) and set “Outgoing server” to “smtp.mail.localserver.com”;
  • Go to “Server settings” in your newly created mail account branch and use “pop3.mail.localserver.com” as “Server name”. “User name” field on this page should be the full name of your e-mail address, so, it would be “master@mail.localserver.com”

You are now ready to use your local e-mail address for your local Magento installation. If you run into any trouble, you can always use the Diagnostics tool from the Utilities menu  or check for error messages in the Status window of the hMail Administrator. I will also suggest you to explore the Administrator application and set up one of the many useful logging and debugging parameters as well as playing with the options for mail accounts. Of course, you should not forget to take a look at the security options too.

Have fun!

Consuming web service via SOAP in Magento

Consuming Web service via SOAP in Magento is a breeze. There are few steps for you to take in order to get access to SOAP web service. For those of you new to web services and SOAP, let me elaborate few things. SOAP is a protocol for exchanging XML-based messages over computer networks, normally using HTTP/HTTPS. Web services is basically just a set of Web APIs that can be accessed over a network, such as the Internet, and executed on a remote system. Magento Core API supports both SOAP and XML RPC protocols. In this example I will show you how to consume Web service using SOAP protocol.

Create a Web Service Role

Go to your Magento Admin > System > Web Services > Roles

  • Create a role
  • Assign resources to role
Create a web service role

Create a web service role

Create a web service role _screen2

Create a web service role _screen2

Create a web service role _screen3

Create a web service role _screen3

Create a Web Service User

Go to your Magento Admin > System > Web Services > Users

  • Create a user
  • Assign a role to user
Create a web service user

Create a web service user

Create a web service user _screen2

Create a web service user _screen2

Assign user role

Assign user role

And finally the real magic off connecting and retrieving the data from SOAP service. Here is the sample code.

$client = new SoapClient(‘http://somesite.domain/shop/index.php/api/?wsdl’);

$session = $client->login(‘sampleuser’, ’123456′);
$result= $client->call($session, ‘customer.list‘, array());

echo ‘<pre>’;
print_r($result);
echo ‘<pre>’;

One thing to keep in mind here. Notice the /shop part of the URL string? It assumes you have your Magento installed inside the /shop sub folder. Feel free to omit that or change it accordingly if that is not where you installed your Magento.

The tricky part here is the customer.list parameter passed to call() function. In this case our call() function receives three parameters, the second is customer.list. If you are like me, the mere mortal then you might ask your self, where does that parameter come from.

List of parameters for Web services is available from the Magento Core API. If you open the Magento Core API page and go to

Core API Calls > Customer API > Methods

then you will see the customer.list among the list of all the available methods. Basically the parameter we passed to $client->call(arg1, ‘customer.list‘, arg3) was the method name.

I used the above code from WordPress template file and it worked like a charm. I was able to get the customer list in a WordPress template file that I latter displayed on some page like a phone book.

Browsing trough Magento Core API you can see what kind of useful information you can retrieve from Web Service.

If you find yourself in a need of retrieving some other kind of information that is missing from Core API you are free to write your own API (that is if you are up to the task).

Hope someone finds this useful.

Magento product view page analysis (available in PDF)

One of the most edited file in Magento is the template file View.phtml file. Reason for this is that a lot of clients would like to rearrange it differently on their online stores. Here is the analysis of that file and the list of all the methods it uses.

Magento product view page analysis (ready for print PDF)

Document above reflects Magento 1.1.6. methods and classes.

One thing to keep in mind, this document shows you the View.phtml block file and shows you it’s inherited methods. All of the Magento block files have the similar inheritance principle. Therefore to find out the available methods all you need to do is open up the extended (inherited) classes.

If you are using some smart IDE solution like NetBeans 6.5 with PHP code completion support, you can simply do the Mage_Catalog_Block_Product_View-> and press the Ctrl + Space to get the dropdown list of all the available methods. If that’s not the case, then you will how to do the things the manual way.

Hope some of you would find this useful.