Strange behavior while assigning the role programmatically
This is the roles element in $user object ( $user->roles )

roles =&gt; array(
  2 =&gt; "authenticated user",
  4 =&gt; "Site Administrator",
  6 =&gt; "Author",
),


You must set the user role like:

$user-&gt;roles[ROLE_ID] = 'ROLE_NAME'