The law, opposed by police organizations, was aimed by lawmakers at immigration agents and will test state powers over the conduct of federal officials.